Skip to content

Instantly share code, notes, and snippets.

@chrisbolin
Last active August 29, 2015 14:08
Show Gist options
  • Save chrisbolin/742d8578daf6aa729629 to your computer and use it in GitHub Desktop.
Save chrisbolin/742d8578daf6aa729629 to your computer and use it in GitHub Desktop.
Firebase Circular Buffer

A circule buffer for firebase. Elements are dequeued and enqueue, and a callback function is run on their dataSnapshot.

var fbRef = new Firebase('https://yourapp.firebaseio.com');
var bufferRef = fbRef.child('path/to/buffer');
function getNext(callback){
// get single-element list, [x]
bufferRef.startAt().limit(1).once('value',function(listSnap){
// get element, x
listSnap.forEach(function(elementSnap){
// remove x
elementSnap.ref().remove(function(error){
// if no error, place back
if (!error){
// push x to end
bufferRef.push(elementSnap.val());
// exec callback functions
callback(elementSnap);
}
});
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment