Generic Javascript Proxy for Firebase

I am using a proxy class since the data I have is a reference to the Firebase location where my object is stored, but I want to act as if I have the object itself. I have something that works great, but I would like to improve it, the key criteria are reduction in repetition. I suspect something is possible by looking at the map class and using apply (), but I don't know how to do this (or if there is a better solution).

I think it would be helpful if the solution could be generalized to support any class, not just the Map class.

var Map = function() {
  ...
};

var MapProxy = function(mapRef) {
  this.mapRef = mapRef;
};

Map.prototype.addToken = function(portrait, newLocation) {
  ...
};

Map.prototype.removeToken = function(token) {
  ...
};

Map.prototype.moveToken = function(token, newLocation) {
  ...
};

MapProxy.prototype.addToken = function(portrait, newLocation) {
  var mapRef = this.mapRef;

  mapRef.once('value', function(data) {
    var map = new Map();
    map.init(mapRef, data.val());

    map.addToken(portrait, newLocation);
  });
};

MapProxy.prototype.removeToken = function(token) {
  var mapRef = this.mapRef;

  mapRef.once('value', function(data) {
    var map = new Map();
    map.init(mapRef, data.val());

    map.removeToken(token);
  });
};

MapProxy.prototype.moveToken = function(token, newLocation) {
  var mapRef = this.mapRef;

  mapRef.once('value', function(data) {
    var map = new Map();
    map.init(mapRef, data.val());

    map.moveToken(token, newLocation);
  });
};

var mapProxy = new MapProxy(mapRef);    

      

+3


source to share


2 answers


I guess I solved it myself at the end.



var FirebaseProxy = function(classToProxy, firebaseRef) {
  var key,
      self = this;

  self.proxy = classToProxy;
  self.firebaseRef = firebaseRef;

  for (key in self.proxy.prototype) {
    if (typeof self.proxy.prototype[key] === 'function') {
      (function(inner_key) {
        self[inner_key] = function ()
        {
          var args = arguments;

          self.firebaseRef.once('value', function(data) {
            var proxiedInstance = new self.proxy();

            if (typeof proxiedInstance.init === 'function') {
              proxiedInstance.init(self.firebaseRef, data.val());
            }

            proxiedInstance[inner_key].apply(proxiedInstance, args);
          });
        } 
      })(key);        
    }
  }
}

      

+2


source


I don't think I fully follow what you are trying to accomplish. Could you please drop the proxy and just use something like this?

var Map = function(mapRef) {
  mapRef.on('value', function(snap) {
    this.init(snap.val());
  });
};

Map.prototype.init = function(data) {
  // update internal state with new data from Firebase ...
};

...

      



Since "value" will fire every time the data changes to mapRef, your map object will always have the most recent data.

It's worth noting that if you need the latest map data on a regular basis you should probably use .on () rather than .once () .. once () will get and fetch data from the servers every time you request it and .on () will always have the latest cached data (since it subscribes to updates). Thus, it will be faster and use less bandwidth.

+1


source







All Articles