Stats

 Latest version : 0.1.0

 Last updated on 2014-04-17T17:08:40.101Z

 Keywords : router

 Downloads :

  • 3 in Last Month

 Links :

 Examples

¯\_(ツ)_/¯
No examples found for this package

 Readme

Mediator

Mediator (pub/sub) implemented on top of accu-router.

Getting started

npm install accu-mediator --save

.instance, new, .create()

Instantiate the mediator:

var Mediator = require( 'accu-mediator' );
var mediator = new Mediator();

//or use the default instance
var mediator = require( 'accu-mediator' ).instance;

//or
var mediator = require( 'accu-mediator' ).create();

.subscribe( topic, callback[, receiver] );

Subscribe to topics

//topics are route paths, see accu-router for route examples
mediator.subscribe( 'my/topic/with/:tokens', myCallbackFn );

//add a receiver context for your callback
mediator.subscribe( 'my/topic/without/tokens', myObj.method, myObj );

The callback will be invoked in the context of the receiver with arguments of [topic, ...arg]. This is how your callback knows which event triggered it when a token or wildcard was used to register it.

.publish( topic[, ...arg] )

Publish topics. Data arguments are optional.

mediator.publish( 'my/topic' );

The publish method returns false if no callbacks were registered or a Promise for the resolution of all callbacks. The Promise will resolve to an array of all callback values.

mediator.publish( 'call/server' ).then( doSomething );

.unsubscribe( topic, callback[, receiver] );

Remove all callbacks subscribed with the same parameters.

mediator.unsubscribe( 'my/topic', myCallback );
mediator.unsubscribe( 'my/topic', myObj.method, myObj );

 Comments