examples/endpointcon-client/build/endpointcon.js
// handle a message from the server
handle a message from the server
handle a message from the server
_message(data) {
    var self = this;
    data = EJSON.parse(data);
    if (!data.msg) {
      return;
    } else if (data.msg === "failed") {
      if (self.supportedDdpVersions.indexOf(data.version) !== -1) {
        self.ddpVersion = data.version;
        self.connect();
      } else {
        self.autoReconnect = false;
        self.emit("failed", "Cannot negotiate DDP version");
      }
    } else if (data.msg === "connected") {
      self.session = data.session;
      self.emit("connected");
    } else if (data.msg === "result") {
      var cb = self._callbacks[data.id];
      if (cb) {
        cb(data.error, data.result);
        delete self._callbacks[data.id];
      }
    } else if (data.msg === "updated") {
      _.each(data.methods, function (method) {
        var cb = self._updatedCallbacks[method];
        if (cb) {
          cb();
          delete self._updatedCallbacks[method];
        }
      });
    } else if (data.msg === "nosub") {
      var cb = self._callbacks[data.id];
      if (cb) {
        cb(data.error);
        delete self._callbacks[data.id];
      }
    } else if (data.msg === "added") {
      if (self.maintainCollections && data.collection) {
        var name = data.collection, id = data.id;
        if (! self.collections[name])     { self.collections[name] = {}; }
        if (! self.collections[name][id]) { self.collections[name][id] = {}; }
        self.collections[name][id]._id = id;
        if (data.fields) {
          _.each(data.fields, function(value, key) {
            self.collections[name][id][key] = value;
          });
        }
        if (self._observers[name]) {
          _.each(self._observers[name], function(observer) {
            observer.added(id);
          });
        }
      }
    } else if (data.msg === "removed") {
      if (self.maintainCollections && data.collection) {
        var name = data.collection, id = data.id;
        if (! self.collections[name][id]) {
          return;
        }
        var oldValue = self.collections[name][id];
        delete self.collections[name][id];
        if (self._observers[name]) {
          _.each(self._observers[name], function(observer) {
            observer.removed(id, oldValue);
          });
        }
      }
    } else if (data.msg === "changed") {
      if (self.maintainCollections && data.collection) {
        var name = data.collection, id = data.id;
        if (! self.collections[name])     { return; }
        if (! self.collections[name][id]) { return; }
        var oldFields     = {},
            clearedFields = data.cleared || [],
            newFields = {};
        if (data.fields) {
          _.each(data.fields, function(value, key) {
              oldFields[key] = self.collections[name][id][key];
              newFields[key] = value;
              self.collections[name][id][key] = value;
          });
        }
        if (data.cleared) {
          _.each(data.cleared, function(value) {
              delete self.collections[name][id][value];
          });
        }
        if (self._observers[name]) {
          _.each(self._observers[name], function(observer) {
            observer.changed(id, oldFields, clearedFields, newFields);
          });
        }
      }
    } else if (data.msg === "ready") {
      _.each(data.subs, function(id) {
        var cb = self._callbacks[id];
        if (cb) {
          cb();
          delete self._callbacks[id];
        }
      });
    } else if (data.msg === "ping") {
      self._send(
        _.has(data, "id") ? { msg : "pong", id : data.id } : { msg : "pong" }
      );
    }
  }
_message ( data ) { var self = this ; data = EJSON . parse ( data ) ; if ( ! data . msg ) { return ; } else if ( data . msg === "failed" ) { if ( self . supportedDdpVersions . indexOf ( data . version ) !== - 1 ) { self . ddpVersion = data . version ; self . connect ( ) ; } else { self . autoReconnect = false ; self . emit ( "failed" , "Cannot negotiate DDP version" ) ; } } else if ( data . msg === "connected" ) { self . session = data . session ; self . emit ( "connected" ) ; } else if ( data . msg === "result" ) { var cb = self . _callbacks [ data . id ] ; if ( cb ) { cb ( data . error , data . result ) ; delete self . _callbacks [ data . id ] ; } } else if ( data . msg === "updated" ) { _ . each ( data . methods , function ( method ) { var cb = self . _updatedCallbacks [ method ] ; if ( cb ) { cb ( ) ; delete self . _updatedCallbacks [ method ] ; } } ) ; } else if ( data . msg === "nosub" ) { var cb = self . _callbacks [ data . id ] ; if ( cb ) { cb ( data . error ) ; delete self . _callbacks [ data . id ] ; } } else if ( data . msg === "added" ) { if ( self . maintainCollections && data . collection ) { var name = data . collection , id = data . id ; if ( ! self . collections [ name ] ) { self . collections [ name ] = { } ; } if ( ! self . collections [ name ] [ id ] ) { self . collections [ name ] [ id ] = { } ; } self . collections [ name ] [ id ] . _id = id ; if ( data . fields ) { _ . each ( data . fields , function ( value , key ) { self . collections [ name ] [ id ] [ key ] = value ; } ) ; } if ( self . _observers [ name ] ) { _ . each ( self . _observers [ name ] , function ( observer ) { observer . added ( id ) ; } ) ; } } } else if ( data . msg === "removed" ) { if ( self . maintainCollections && data . collection ) { var name = data . collection , id = data . id ; if ( ! self . collections [ name ] [ id ] ) { return ; } var oldValue = self . collections [ name ] [ id ] ; delete self . collections [ name ] [ id ] ; if ( self . _observers [ name ] ) { _ . each ( self . _observers [ name ] , function ( observer ) { observer . removed ( id , oldValue ) ; } ) ; } } } else if ( data . msg === "changed" ) { if ( self . maintainCollections && data . collection ) { var name = data . collection , id = data . id ; if ( ! self . collections [ name ] ) { return ; } if ( ! self . collections [ name ] [ id ] ) { return ; } var oldFields = { } , clearedFields = data . cleared || [ ] , newFields = { } ; if ( data . fields ) { _ . each ( data . fields , function ( value , key ) { oldFields [ key ] = self . collections [ name ] [ id ] [ key ] ; newFields [ key ] = value ; self . collections [ name ] [ id ] [ key ] = value ; } ) ; } if ( data . cleared ) { _ . each ( data . cleared , function ( value ) { delete self . collections [ name ] [ id ] [ value ] ; } ) ; } if ( self . _observers [ name ] ) { _ . each ( self . _observers [ name ] , function ( observer ) { observer . changed ( id , oldFields , clearedFields , newFields ) ; } ) ; } } } else if ( data . msg === "ready" ) { _ . each ( data . subs , function ( id ) { var cb = self . _callbacks [ id ] ; if ( cb ) { cb ( ) ; delete self . _callbacks [ id ] ; } } ) ; } else if ( data . msg === "ping" ) { self . _send ( _ . has ( data , "id" ) ? { msg : "pong" , id : data . id } : { msg : "pong" } ) ; } }
handle a message from the server
handle a message from the server
// TODO: 'addedBefore' -- not yet implemented in Meteor // TODO: 'movedBefore' -- not yet implemented in Meteor // method result // method updated // missing subscription // add document to collection // remove document from collection // change document in collection // subscriptions ready // minimal heartbeat response for ddp pre2