javascript - callback called even after off? -
i have situation if call off
on reference keeps calling callbacks?
var ref = new firebase('https://stackoverflow.firebaseio.com/31069762'); (var n = 0; n < 1024; ++n) { ref.push(n) } ref.on('child_added', function(snapshot) { if (snapshot.val() > 10) { console.log('off') // printed multiple times. ref.off(); } });
when call off()
stop firebase client firing child_added
events new data comes in. not prevent firing child_added
events data client received.
you can interesting behavior way. example, snippet:
var ref = new firebase('https://stackoverflow.firebaseio.com/31069762'); ref.remove(); (var n = 0; n < 1024; ++n) { ref.push(n) } ref.on('child_added', function(snapshot) { console.log(snapshot.val()); if (snapshot.val() > 10) { console.log('off') // printed multiple times. ref.off(); } });
will print:
1 2 . . . 11 "off" 12 "off" . . . 1024 "off"
the data ref download "one initial packet" when register listener , there on child_added
events fired.
now let's change code around:
var ref = new firebase('https://stackoverflow.firebaseio.com/31069762'); ref.remove(); ref.on('child_added', function(snapshot) { console.log(snapshot.val()); if (snapshot.val() > 10) { console.log('off') // printed multiple times. ref.off(); } }); (var n = 0; n < 1024; ++n) { ref.push(n) }
so in case first register listener , start pushing values. output:
1 2 . . . 11 "off"
so ordering stops straight away. since started listening child_added
before there data, initial payload empty , each child after separate update.
note depends on internals of firebase client , haven't checked how works there. "one initial packet" mentioned above how i visualize it.
if don't want process data after 10
, there 2 solutions can think off.
the 1 do: keep local "i done" flag
use
query
limit data firebase downloads, instead of filtering client side:ref.orderbyvalue().endat(10).on('child_added'...
note
orderbyvalue
introduced in version 2.2.7 of firebase javascript sdk. took me while figure out why couldn't use in jsbin. :-/
Comments
Post a Comment