Skip to content
forked from thiagodp/pubsub

A simple publish-subscribe pattern implementation (in JavaScript).

Notifications You must be signed in to change notification settings

alebagran/pubsub

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

pubsub

A simple publish-subscribe pattern implementation (in JavaScript).

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 simple publish-subscribe pattern implementation (in JavaScript).

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • HTML 55.7%
  • JavaScript 44.3%