Firebase pop () as the opposite of push ()?

I would like to implement a stack (queue) that many users can push (), and many can pop () using FILO.

Is there a method equivalent pop()

to retrieve / remove the last item in a list?

For example:

var popRef = firebaseRef.pop();

      

+3


source to share


2 answers


This is very doable, but a little tricky. Since you will have multiple users trying to delete items, you will have to deal with the case where multiple users try to delete the same item (this is indeed a problem with distributed systems). Presumably you only need one user. The solution is to use a transaction to ensure that only one user can successfully delete a specific item.

We have an example of a work queue on github that is very similar to what you are looking for: https://github.com/firebase/firebase-work-queue

The big difference between it and what you asked for is that it is FIFO instead of FILO. To change it as FILO, you will want to change this line with workqueue.js:



queueRef.startAt().limit(1).on("child_added", function(snap) {

      

..startAt () there tells it to grab the first element from the beginning of the list (i.e. the oldest element). To get the last item in the list (ie New item), you can change "startAt ()" to "endAt ()", or just omit "startAt ()" entirely (by default, we'll get the last item).

+6


source


You need "first-come-first-serve", which is a FIFO, so you don't need one pop()

, but unshift()

which is available in JavaScript:

// Performs a dequeue: gets the 0-index item and removes it
// from the original array
var popRef = firebaseRef.unshift();

      

To execute FILO, you have pop()

.



FireBase update:

  // Get a reference to the root of the chat data.
  var messagesRef = new Firebase('https://example37192028.firebaseio-demo.com/');
  var tokens = [];
  // When the user presses enter on the message input, write the message to firebase.
  $('#messageInput').keypress(function (e) {
      if (e.keyCode == 13) {
          var name = $('#nameInput').val();
          var text = $('#messageInput').val();
          tokens.push(messagesRef.push({name:name, text:text}));
          $('#messageInput').val('');
      } else if (e.keyCode == 32) {
          // Removing from the stack on spacebar
          var token = tokens.pop();
          token.remove();
          $('#messageInput').val('');
      }
  });

  // Add a callback that is triggered for each chat message.
  messagesRef.on('child_added', function (snapshot) {
      var message = snapshot.val();
      $('<div/>').text(message.text).prepend($('<em/>').text(message.name+': ')).appendTo($('#messagesDiv'));
      $('#messagesDiv')[0].scrollTop = $('#messagesDiv')[0].scrollHeight;
  });
  messagesRef.on('child_removed', function (snapshot) {
      var message = snapshot.val();
      $('<div/>').text('removed:' + message.text).prepend($('<em/>').text(message.name+': ')).appendTo($('#messagesDiv'));
      $('#messagesDiv')[0].scrollTop = $('#messagesDiv')[0].scrollHeight;
  });

      

0


source







All Articles