namespace-emitter
A small event emitter with namespaces.
Not meant as a replacement for node's events module, but as a small component for browser js.
Install
npm install --save namespace-emitter
Example
var emitter = emitter emitter emitter// -> example event emitted// -> all events emitted example emitter emitter// -> all events emitted demo:cool// -> multiple events with `demo` namespace emitted demo:cool emitter// -> all events emitted demo:awesome// -> multiple events with `demo` namespace emitted demo:awesome emitter// -> all events emitted demo:great// -> multiple events with `demo` namespace emitted demo:great
API
createNamespaceEmitter
Create an event emitter with namespaces
Examples
var emitter = emitter emitter
emit
Emit an event. Optionally namespace the event. Handlers are fired in the order in which they were added with exact matches taking precedence. Separate the namespace and event with a :
Parameters
event
String – the name of the event, with optional namespacedata
...Any – data variables that will be passed as arguments to the event listener
Examples
emitteremitteremitter
off
Stop listening to an event. Stop all listeners on an event by only passing the event name. Stop a single listener by passing that event handler as a callback.
You must be explicit about what will be unsubscribed: emitter.off('demo')
will unsubscribe an emitter.on('demo')
listener,
emitter.off('demo:example')
will unsubscribe an emitter.on('demo:example')
listener
Parameters
event
Stringfn
[Function] – the specific handler
Examples
emitteremitter
on
Create en event listener.
Parameters
event
Stringfn
Function
Examples
emitteremitter
once
Create en event listener that fires once.
Parameters
event
Stringfn
Function
Examples
emitteremitter