- Create a reference
- Add a
value
listener - Wait two seconds to ensure the data loads
- Add a
value
listener on a child
In our script, childListener
is run synchronously. That means it runs before we increment the count
and the assertion fails. This releases the dark͝ tend̴r̡i҉ls of Zalgo.
The linked post does a decent job of explaining this, but let's get Firebase specific. If we comment out parent.on('value', function parentListener () {})
, the assertion will pass. By calling listeners sometimes synchronously and sometimes asynchronously, the prior state of the program leaks unexpectedly into user code.
Google Closure provides a good nextTick
implementation so this should be reasonably easy to resolve.
I've had some situations where I've needed to filter updates from the server vs local updates:
This helps eliminate a constant cycle of updates. Note this is not really a problem in most situations (calling
ref.set
twice with identical values won't trigger the callback twice).. As I recall my need for it was brought about by some weirdness with the GoogleMaps LatLng API, and in another situation floating point rounding errors, and in another situation where I was settingFirebase.TIMESTAMP
on every update.I see this as solvable in one of two ways:
ref.set()
is called.ref.set()
call, or by new data from the server.Option 1 certainly provides better backwards compatibility for some of my legacy code, but I do not know that it is the best way.