channel-dispatcher
v3.1.0
Published
A tiny channel subscribe/publish library.
Readme
channel-dispatcher
A tiny channel subscribe/publish library.
Basic usage.
// require library
var dispatcher = require('channel-dispatcher');
// subscribe callback
// sub is needed to unsubscribe later
var sub = dispatcher.subscribe('test', function(data) {
console.log(data);
});
// pass data to channel
// in this case 'Hello to channel' will be printed
dispatcher.publish('test', 'Hello to channel');
// unsubscribe callback from channel
sub.unsubscribe();Using standalone channel object.
// require library
var dispatcher = require('channel-dispatcher');
// get channel object
var channel = dispatcher.getChannel('test');
// subscribe callback
// sub is needed to unsubscribe later
var sub = channel.subscribe(function(data) {
console.log(data);
});
// pass data to channel
// in this case 'Hello to standalone channel' will be printed
channel.publish('Hello to standalone channel');
// unsubscribe callback from channel
sub.unsubscribe();Specifying context
You can pass context along with callback
// context object
var context = {foo: "context data"};
// callback
var callback = function(bar) {
var foo = this.foo;
console.log(foo + " " + bar);
};
// subscribe callback with context
dispatcher.subscribe('test', callback, context);
// or using channel object
// channel.subscribe(callback, context)
// pass data
// 'context data passed data' will be printed
dispatcher.publish('test', "passed data");Show warnings
Warnings is disabled by default.
// show warnings
dispatcher.setLogging(true);