dispatch.js | |
---|---|
Dispatch messages to multiple subscribers. | var toddick = require('./toddick'); |
Toddick: DistributorAllows data to be sent to a set of subscribers.
name - The name the distributor will be registed using. | toddick( 'Distributor', module,
{
|
Message: INITInitializes the toddick with arguments from the constructor. | INIT: function(name) {
this.subscriptions = {};
if(name) {
this.register(name);
}
}, |
Message: SUBSCRIBERegisters a message to be sent when distribute message is recieved.
MSG - The message that will be sent. The subscriber is automatically unsubscribed when it exits. | SUBSCRIBE: function(MSG) {
this.subscriptions[MSG.id] = MSG;
this.monitor(MSG.toddick, this.UNSUBSCRIBE.withArgs(MSG));
}, |
Message: UNSUBSCRIBEDeregisters a previsouly subscribed message.
MSG - The message to be unsubscribed. | UNSUBSCRIBE: function(MSG) {
delete this.subscriptions[MSG.id];
this.unmonitor(MSG.toddick, this.UNSUBSCRIBE);
}, |
Message: DISTRIBUTESends provided data to all subscribers.
data - The data to send to all subscribers. | DISTRIBUTE: function() {
for(var id in this.subscriptions) {
var MSG = this.subscriptions[id];
MSG.apply(null, arguments);
}
}
}
); |
Toddick: ReceiverSubscribes to messages from a Dispatcher.
source - The name used to register the distributor or the distributor itself. MSG - The message to be sent by the distributor. | toddick( 'Receiver', module,
{
|
Message: INITInitializes the toddick with arguments from the constructor. | INIT: function(source, MSG) {
var distributor = this.monitor(source);
distributor.SUBSCRIBE(MSG);
}
}
);
|