EventSignal
EventSignal is a lightweight, browser-safe event messaging controller inspired by js-signals.
Installing
bower
bower install event-signal
npm
npm install event-signal
Examples
{ console;} var resource = {}; // add support for `created` and `updated` eventsresourcecreated = ;resourceupdated = ; // add a listener to the `updated` eventresourceupdated; // emit the `updated` event to all listeners,// passing an optional `data` objectresourceupdated; // remove the listener from the `updated` event-signalresourceupdated;
Constructor
var object = {};objectupdated = ; { thisupdated = ;}
addListener(listener)
Add a listener
to the event signal instance.
Returns the EventSignal
.
Throws TypeError
if listener
was already previously added.
Param | Type | Description |
---|---|---|
listener | Function | The listener function |
objectsaved; // alternatively, use alias `then`objectsaved;
addListener(listener, scope)
Add a listener
to the event signal instance, passing a scope
object that will be this
from inside the listener function. If scope
is not provided, listener
will use an anonymous {}
for scope
.
Returns the EventSignal
.
Throws TypeError
if listener
was already previously added.
Param | Type | Description |
---|---|---|
listener | Function | The listener function |
scope | Object | Optional; scope that will be this inside the listener function |
var scope = {}; objectsaved;
addListener(listener, once)
Add a listener
to the event signal instance. Passing true
for once
will automatically remove the listener after one call.
Returns the EventSignal
.
Throws TypeError
if listener
was already previously added.
Param | Type | Description |
---|---|---|
listener | Function | The listener function |
once | boolean | Optional; if true , listener will be removed after one call |
objectsaved;
addListener(listener, scope, once)
The trifecta — add a listener
to the event signal instance, passing a scope
object that will be this
from inside the listener function, and boolean true
for once
to automatically remove the listener after one call.
Returns the EventSignal
.
Throws TypeError
if listener
was already previously added.
Param | Type | Description |
---|---|---|
listener | Function | The listener function |
scope | Object | Optional; scope that will be this inside the listener function |
once | boolean | Optional; if true , listener will be removed after one call |
var scope = {}; objectsaved;
emit(data)
Emit the signal to all listeners. Optionally pass data
to listeners.
Returns the EventSignal
.
Param | Type | Description |
---|---|---|
data | * | Optional data to be passed to listener |
objectsaved; // examples of passing optional dataobjectsaved;objectsaved;
removeListener(listener)
Removes the provided listener
function.
Returns the EventSignal
.
Param | Type | Description |
---|---|---|
listener | Function | The listener function to be removed |
var {};objectsaved;objectsaved;
removeAllListeners()
Removes all registered listeners.
Returns the EventSignal
.
objectsaved;
listeners()
Returns a cloned array of registered listeners.
objectsaved;//-> [{callback:function, scope:Object, once:boolean}]
listenerCount();
Returns the number of registered listeners.
objectsaved//-> number
Naming Conventions
It's recommended that EventSignal instances be named using the past-tense :
- foo.created
- foo.saved
- foo.updated
- foo.ended
are preferred over :
- foo.create
- foo.onSave
- foo.afterUpdate
- foo.ending
- foo.end
Browser Support
- Chrome
- Firefox
- IE 9+
- IE 8 with es5-shim
- Safari
Module Support
- AMD
- CommonJS
- Browser global
License
EventSignal is free to use under the open-source MIT license.