Publish-Subscribe Pattern
Aka: Observer or Event Emitter.
The newPubSub()
function will create an object that has the capability of registering subscribers and publishing events to them.
You can then inhert from the object created with newPubSub()
.
var obj = ; // browser// var newPubSub = require('pubsub-ma'); // node obj; obj;
Methods:
// register an event handler to an event name. // alternative signature 1 // call all registered event handlers of an event name. // unregister an event handler from an event name. // register an event handler to be called only once. // get a list of all subscribers.
Basic usage:
var brain = Object; brain;
Another example:
var door = ; door; door; door;door;
newPubSub().on()
:
Different argument signatures of var t = ;var log = consolelog; // basic:t; // alternative 1 - one object argument:// keys of the object are event names.// values of the object are event handlers.t; // alternative 2 - one object argument:// keys of the object are event names.// values of the object are object themselves with three properties:// fn: a function which is the event handler.// par: a value to pass to the event handler.// once: a boolean value indicating whether the event handler should be called only once.t; t; t;t;t;t;t; // deleted after one publisht; // deleted after one publish
.on()
or .emit()
:
Two ways of passing data to event handlers: when calling var t = ;t;t; // 'bar' undefined undefined undefinedt; // 1 2 3 'bar't; // [1, 2, 3] 'bar' undefined undefined var t = ;t;t; // [true, false, true] undefined undefined undefinedt; // 1 2 3 [true, false, true] { console;}