Last active
December 20, 2015 10:20
-
-
Save katowulf/6115099 to your computer and use it in GitHub Desktop.
Using a simple update counter to maintain precise ordering of records in Firebase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sequenceRef = new Firebase(...+'/entries'); | |
var updateCounter = new Firebase(...+'/counter'); | |
updateCounter.transaction(function(currentValue) { | |
return currentValue+1; | |
}, function(error, committed, snap) { | |
if( error ) { ... } | |
else if( committed ) { | |
var updateCounter = snap.val(); | |
sequenceRef.push().setWithPriority({ | |
updateCounter: updateCounter | |
userId: "user123", | |
timestamp: Firebase.ServerValue.TIMESTAMP | |
}, updateCounter); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment