fn atomic_update<F>(data: &Database, at: &[&str], default: i64, mut update: F)
where F: FnMut(i64) -> i64 {
    // Until the write succeeds: read, then write updated value.
    let maybe_node = data.at(at).ok();
    maybe_node.map(|node| {
        loop {
            let old = node.get().ok().map(|r| from_str::<i64>(&r.body).ok()).flatten().unwrap_or(default);
            let new = update(old);
            if new == old { break };
            let body = to_string(&new).ok();
            match body {
                Some(string) => {
                    let r = node.set(&string);
                    if r.is_ok() && r.unwrap().code / 100 == 2 { break };
                },
                None => break,
            }
        }
    });
}
/// Atomic counters? In Firebase? Too modern for firebase_rs (and probably the REST API).
/// Manually-implemented transactions? *Perfection*.
/// Each counter has to have a rule in the DB which checks that the update is possible.
Atomic counters. In Firebase. Too modern for firebase_rs (and probably the REST API).
Manually-implemented transactions. *Perfection*.
Each counter has to have a rule in the DB which checks that the update is possible.
Atomic counters . In Firebase . Too modern for firebase_rs ( and probably the REST API ) . Manually - implemented transactions . * Perfection * . Each counter has to have a rule in the DB which checks that the update is possible .
fn atomic_update<F>(data: &Database, at: &[&str], default: i64, mut update: F)
where F: FnMut(i64) -> i64 {
    // Until the write succeeds: read, then write updated value.
    let maybe_node = data.at(at).ok();
    maybe_node.map(|node| {
        loop {
            let old = node.get().ok().map(|r| from_str::<i64>(&r.body).ok()).flatten().unwrap_or(default);
            let new = update(old);
            if new == old { break };
            let body = to_string(&new).ok();
            match body {
                Some(string) => {
                    let r = node.set(&string);
                    if r.is_ok() && r.unwrap().code / 100 == 2 { break };
                },
                None => break,
            }
        }
    });
}
fn atomic_update < F > ( data : & Database , at : & [ & str ] , default : i64 , mut update : F ) where F : FnMut ( i64 ) -> i64 { // Until the write succeeds: read, then write updated value. let maybe_node = data . at ( at ) . ok ( ) ; maybe_node . map ( | node | { loop { let old = node . get ( ) . ok ( ) . map ( | r | from_str :: < i64 > ( & r . body ) . ok ( ) ) . flatten ( ) . unwrap_or ( default ) ; let new = update ( old ) ; if new == old { break } ; let body = to_string ( & new ) . ok ( ) ; match body { Some ( string ) => { let r = node . set ( & string ) ; if r . is_ok ( ) && r . unwrap ( ) . code / 100 == 2 { break } ; } , None => break , } } } ) ; }
Atomic counters?
Atomic counters?
data
&Database
at
&[&str]
default
i64
update
F
data
&Database
at
&[&str]
default
i64
update
F