Skip to content

A JavaScript observer implementation with subscription priority, and event cancelation features.

License

Notifications You must be signed in to change notification settings

gmetzker/Observer-JS

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Observer-JS

A JavaScript observer implementation with subscription priority, and event cancellation features.

Basic Pub-Sub

Live Example

  • 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');

Output

  Luke
  Vader
>

Unsubscribe

Live Example

  • 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');

Output

  Luke
  R2D2
>

Publish with arguments

Live Example

  • 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 ');

Output

  Hello Luke
  Hello Vader
>

this.channelId

Live Example

  • 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');

Output

  some.event - Luke
  some.event - Vader
>

Event Cancellation

Live Example

  • 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!'); }

Output

  Luke: Ouch!
  Ob1: Blocked
  force.push failed!
>

Subscriber Order

Live Example

  • 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');

Output

  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
>

About

A JavaScript observer implementation with subscription priority, and event cancelation features.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published