UNPKG

faye

Version:

Simple pub/sub messaging for the web

45 lines (34 loc) 1.05 kB
'use strict'; var Class = require('../util/class'), assign = require('../util/assign'), Deferrable = require('../mixins/deferrable'); var Subscription = Class({ initialize: function(client, channels, callback, context) { this._client = client; this._channels = channels; this._callback = callback; this._context = context; this._cancelled = false; }, withChannel: function(callback, context) { this._withChannel = [callback, context]; return this; }, apply: function(context, args) { var message = args[0]; if (this._callback) this._callback.call(this._context, message.data); if (this._withChannel) this._withChannel[0].call(this._withChannel[1], message.channel, message.data); }, cancel: function() { if (this._cancelled) return; this._client.unsubscribe(this._channels, this); this._cancelled = true; }, unsubscribe: function() { this.cancel(); } }); assign(Subscription.prototype, Deferrable); module.exports = Subscription;