cycle-events
Provides event-based pub/sub to cycle.js applications.
Installation
npm i cycle-events --save
Scripts
NOTE: Make sure you've installed all dependencies using npm install
first.
To generate documentation: npm run doc
. This will create documentation in the
build/docs
folder.
To run unit tests: npm test
API
Broker
Kind: global class
- Broker
- new Broker()
- instance
- static
- inner
- ~Events :
Object
- ~Events :
new Broker()
Provides pub/sub functionality to Cycle.js applications.
Example
var broker = ; // create instancevar off = broker;broker; // fire event; // remove the event handler
Example
// add pub/sub functionality to a class and// automatically log any errors caused by// subscribers of the class: { Broker; // mixin pub/sub RxObservable ;} // now use the class:var myClass = ;myClass;
function
broker.on(event, callback) ⇒ Registers a listener for the specified event.
Kind: instance method of Broker
Returns: function
- A method to invoke to remove the listener
from the specified event.
Throws:
TypeError
Parameterevent
must be a non-empty string.TypeError
Parametercallback
must be a function.
Emits: listenerAdded
Param | Type | Description |
---|---|---|
event | String |
The event to subscribe to. |
callback | function |
The listener to invoke when the specified event is emitted. |
Example
// register an event handler:broker
function
broker.one(event, callback) ⇒ Registers a listener for the specified event, but ensures the listener will only be fired at most 1 time. Once a listener has been invoked, it will automatically be removed.
Kind: instance method of Broker
Returns: function
- A method to invoke to remove the listener
from the specified event.
Throws:
TypeError
Parameterevent
must be a non-empty string.TypeError
Parametercallback
must be a function.
Emits: listenerAdded
Param | Type | Description |
---|---|---|
event | String |
The event to subscribe to. |
callback | function |
The listener to invoke when the specified event is emitted. |
Example
// register a handler to only run once:broker
broker.off(event, callback)
Removes the specified listener for the specified event.
Kind: instance method of Broker
Throws:
TypeError
Parameterevent
must be a non-empty string.TypeError
Parametercallback
must be a function.
Emits: listenerRemoved
Param | Type | Description |
---|---|---|
event | String |
The event whose listener should be removed. |
callback | function |
The listener to remove. |
Example
{ ... }broker;broker;
broker.removeAllListeners(event)
Removes all listeners registered for the specified event.
Kind: instance method of Broker
Throws:
TypeError
Parameterevent
must be a non-empty string.TypeError
Parametercallback
must be a function.
Emits: listenerRemoved
Param | Type | Description |
---|---|---|
event | String |
The event whose listeners should all be removed. |
Example
broker;broker;broker; // both handlers invokedbroker;broker; // no handlers invoked
broker.emit(event, args)
Invokes any listeners for the specified event--in the order they were registered--and passes any provided arguments to those listeners. If a listener throws an exception, the error event will be emitted but subsequent listeners will still be invoked.
Kind: instance method of Broker
Throws:
TypeError
Parameterevent
must be a non-empty string.
Emits: error
Param | Type | Description |
---|---|---|
event | String |
The event to emit. |
args | * |
Any additional arguments to pass to listeners. |
Example
broker;broker; // handler fired
Example
broker;broker;
Example
broker;broker;
"error"
An error occurred in an event listener while firing an event.
Kind: event emitted by Broker
Properties
Name | Type | Description |
---|---|---|
event | String |
The event the listener was registered for. |
callback | function |
The listener that caused the error. |
error | Error |
The error that occurred while invoking the listener. |
Example
broker;
"listenerAdded"
A listener was added. Examine the event properties for details.
Kind: event emitted by Broker
Properties
Name | Type | Description |
---|---|---|
event | String |
The event the listener was registered for. |
callback | function |
The listener registered for the event. |
Example
broker;broker;
"listenerRemoved"
A listener was removed. Examine the event properties for details.
Kind: event emitted by Broker
Properties
Name | Type | Description |
---|---|---|
event | String |
The event the listener was removed from. |
callback | function |
The listener removed from the event. |
Example
broker;var off = broker;; // remove the event handler
Events
Broker.Events : An enumeration of event names used internally that external callers can also subscribe to.
Kind: static property of Broker
Example
broker;broker;
Object
Broker~Events : Kind: inner typedef of Broker
Properties
Name | Type | Description |
---|---|---|
ERROR | String |
'error' - An error occurred in an event listener. |
ADDED | String |
'listenerAdded' - An event listener was registered. |
REMOVED | String |
'listenerRemoved' - An event listener was removed. |