A JavaScript observer implementation with subscription priority, and event cancellation features.
- Create a new instance:
var ob = new Observer();
- Add subscribers to a channel:
ob.subscribe(channelId, subscriberFunc);
- Publish messages to a channel:
ob.publish(channelId [, arg0, arg1, ...]);
var ob = new Observer();
ob.subscribe('some.event', function () { console.log('Luke'); });
ob.subscribe('some.event', function () { console.log('Vader'); });
ob.subscribe('some.event.other', function () { console.log('Yoda'); });
ob.publish('some.event');
Luke
Vader
>
- Unsubscribe to a channel:
ob.unsubscribe(channelId, subscriberFunc);
var ob = new Observer();
var vaderSub = function () { console.log('Vader'); };
ob.subscribe('some.event', function () { console.log('Luke'); });
ob.subscribe('some.event', vaderSub);
ob.subscribe('some.event', function () { console.log('R2D2'); });
ob.unsubscribe('some.event', vaderSub);
ob.publish('some.event');
Luke
R2D2
>
- You can publish with any arguments
- All arguments after the channel are passed to the subscribers.
var ob = new Observer();
ob.subscribe('some.event', function (greeting) { console.log(greeting + 'Luke'); });
ob.subscribe('some.event', function (greeting) { console.log(greeting + 'Vader'); });
ob.publish('some.event', 'Hello ');
Hello Luke
Hello Vader
>
- this.channelId in the subscriber method will indicate the current channel.
var ob = new Observer();
ob.subscribe('some.event', function () { console.log(this.channelId + ' - Luke'); });
ob.subscribe('some.event', function () { console.log(this.channelId + ' - Vader'); });
ob.subscribe('some.event.other', function () { console.log(this.channelId + ' - Yoda'); });
ob.publish('some.event');
some.event - Luke
some.event - Vader
>
- Any subscribers can cancel the event for all lower priority subscribers:
this.cancel = true;
- The result of the publish method indicates if the event was fully published (returns true), or canceled. (returns false)
var ob = new Observer();
ob.subscribe('force.push', function () { console.log('Luke: Ouch!'); });
ob.subscribe('force.push', function () {
console.log('Ob1: Blocked');
this.cancel = true;
});
ob.subscribe('force.push', function () { console.log('Solo: Arg!'); });
var complete = ob.publish('force.push');
if( !complete ) { console.log('force.push failed!'); }
Luke: Ouch!
Ob1: Blocked
force.push failed!
>
- Typically subscribers are executed in the order they were added.
- An optional priority value can be supplied to designate a specific order.
ob.subscribe(channelId, callback, priority)
. - Priority subscribers are executed in descending order from largest priority subscriber to the smallest.
- If a channel has subscribers with and without priority specified then the subscribers without a priority are executed last.
var ob = new Observer();
ob.subscribe('some.event', function () { console.log('Luke has no priority specified but was added first'); });
ob.subscribe('some.event', function () { console.log('Yoda has a priority of 10'); }, 10);
ob.subscribe('some.event', function () { console.log('Obi has no priority specified'); });
ob.subscribe('some.event', function () { console.log('Chewy has the highest priority and will go first - 99');}, 99);
ob.subscribe('some.event', function () { console.log('R2D2 has a priority of 5');}, 5);
ob.publish('some.event');
Chewy has the highest priority and will go first - 99
Yoda has a priority of 10
R2D2 has a priority of 5
Luke has no priority specified but was added first
Obi has no priority specified
>