Skip to content
This repository has been archived by the owner on Jun 21, 2019. It is now read-only.
/ pubsub Public archive

A publish-subscribe pattern implementation that adds methods to a given object instead of using a single global

Notifications You must be signed in to change notification settings

thiagodp/pubsub

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

pubsub

A JavaScript publish-subscribe pattern implementation that adds the pattern methods to your object. Just use the empower method passing your object as an argument and it will have pub, sub, and unsub methods. Thus you don't need to deal with a global PubSub object, as many libraries do out there. Use it just where you need it! ;)

Example on how to use it:

 var myObj = {}; // your object
 
 ( new PubSub() ).empower( myObj ); // add methods "pub", "sub", and "unsub" to your object
 
 var sth = function() { console.log( 'something happened' ); };
 var sth2 = function() { console.log( 'something happened again' ); };
 
 myObj.sub( 'onSomething', sth );
 myObj.sub( 'onSomething', sth2 );
 myObj.pub( 'onSomething' ); // prints 'something happened' and 'something happened again'
 
 myObj.sub( 'onOtherThing', function( value ) { console.log( value ); } );
 myObj.pub( 'onOtherThing', [ 10 ] ); // prints 10
 
 myObj.sub( 'onAnotherThing', function( a, b ) { console.log( a + b ); } );
 myObj.pub( 'onAnotherThing', [ 'hello', ' world' ] ); // prints hello world
 
 myObj.unsub( 'onSomething', sth );
 myObj.pub( 'onSomething' ); // prints only 'something happened again'
 
 myObj.unsub( 'onSomething', sth2 );
 myObj.pub( 'onSomething' ); // does nothing!
 
 myObj.sub( 'onSomething', sth );
 myObj.sub( 'onSomething', sth2 );
 myObj.unsub( 'onSomething' ); // remove all subscriptions to onSomething
 myObj.pub( 'onSomething' ); // does nothing!

About

A publish-subscribe pattern implementation that adds methods to a given object instead of using a single global

Resources

Stars

Watchers

Forks

Packages

No packages published