channel.js

'use strict';


Telepat Channel Class

Use Channels to create, update and remove Telepat objects. You can create new Channels using the subscribe function of the main Telepat object.


var EventObject = require('./event');


Channel Constructor

This is generally invoked by the main Telepat object, when calling the subscribe function.

Params
api Object The API connection object. This is injected by Telepat.
log Object The logging-handling object. This is injected by Telepat.
error Object The error-handling object. This is injected by Telepat.
options Object The object describing the required subscription (context, channel, filters)
interval integer The time interval in miliseconds between two object-monitoring jobs. Defaults to 150.
var Channel = function (tapi, tlog, terror, tmonitor, toptions) {
  var api = tapi;
  var error = terror;
  var monitor = tmonitor;
  var options = toptions;
  var log = tlog;
  var event = new EventObject(log);
  var self = this;


Channel.objects

You can access a hash of all the objects on the current channel using this property. Each object is stored on a key named after the object id.

  this.objects = {};


Channel.subscribe

Call this function to actually subscribe to the configured channel. This is usually invoked by the subscribe method on the main Telepat object.

There are two possible events emitted by the Channel as a result:

  • subscribe, after a successful channel subscribe
  • error
  this.subscribe = function() {
    api.call('object/subscribe',
    options,
    function (err, res) {
      if (err) {
        event.emit('error', error('Subscribe failed with error: ' + err));
      } else {
        var i;
        for (i=0; i<res.body.content.length; i++) {
          self.objects[res.body.content[i].id] = res.body.content[i];
        }
        var objectKeys = Object.keys(self.objects);
        for (i=0; i<objectKeys.length; i++) {
          self.objects[objectKeys[i]].$$event = new EventObject(log);
        }
        monitor.add(options, self.objects, event, self.add, self.remove, self.update);
        event.emit('subscribe');
      }
    });
  };


Channel.unsubscribe

Call this function to unsubscribe from the configured channel.

There are two possible events emitted by the Channel as a result:

  • unsubscribe, after a successful channel unsubscribe
  • error
  this.unsubscribe = function() {
    api.call('object/unsubscribe',
      options,
      function (err) {
        if (err) {
          event.emit('error', error('Unsubscribe failed with error: ' + err));
        } else {
          self.objects = {};
          event.emit('unsubscribe');
          event.emit('_unsubscribe');
        }
      });
  };


Channel.add

Add a new Telepat object to the current channel. The channel might emit an error if the operation fails.

Params
object Object The new object to add
  this.add = function(object) {
    api.call('object/create',
      {
        model: options.channel.model,
        context: options.channel.context,
        content: object
      },
      function (err) {
        if (err) {
          event.emit('error', error('Adding object failed with error: ' + err));
        } else {
        }
      });
  };


Channel.remove

Remove a Telepat object from the current channel. The channel might emit an error if the operation fails.

Instead of using this function, you can also delete the object from Channel.objects.

Params
id integer The id of the object to delete
  this.remove = function(id) {
    api.del('object/delete',
      {
        model: options.channel.model,
        context: options.channel.context,
        id: id
      },
      function (err) {
        if (err) {
          event.emit('error', error('Removing object failed with error: ' + err));
        } else {
        }
      });
  };


Channel.update

Updates a Telepat object from the current channel. The channel might emit an error if the operation fails.

To call this function, you need to create an array containing 'patch' objects, representing the modifications that need to be persisted. The structure of a patch object is:

{'op': 'replace', 'path': channel + '/' + object_id + '/' + object_property, 'value': modified_value}

Instead of using this function, you can also update the object directly from Channel.objects.

Params
id integer The id of the object to update
patch Array The array of patches representing the modifications that need to be persisted.
  this.update = function(id, patch) {
    api.call('object/update',
      {
        model: options.channel.model,
        context: options.channel.context,
        id: id,
        patches: patch
      },
      function (err) {
        if (err) {
          event.emit('error', error('Updating object failed with error: ' + err));
        } else {
        }
      });
  };


Channel.on

Call this function to add callbacks to be invoked on event triggers.

Params
name string The name of the event to associate the callback with
callback function The callback to be executed
  this.on = function(name, callback) {
    return event.on(name, callback);
  };
};

module.exports = Channel;