listen-up
A fast and lightweight event emitter. Supports regular expression event subscriptions and group listener removal.
Inspired strongly by https://github.com/HenrikJoreteg/wildemitter (especially the concept of "groups") so thanks to @HenrikJoreteg and @andyet.
- Very lightweight (and fast)
- Browser and node.js support (browser support with Browserify or similar tool)
- Standard event emitter functionality (i.e.
on
,off
,emit
) - Single event handler registration with
once
- Handler grouping and bulk removal with
releaseGroup
- *Supports factory and mixin patterns for greater flexibility
- *Support for regex event matching
- *Consistent arguments passed to handlers regardless of registration method
*features not in WildEmitter
Install
npm install listen-up
Creating emitters
require('listen-up')([subjectObject])
Returns a new emitter or the subject object with emitter functionality mixed in.
var makeEmitter = ; // Create an emitter objectvar emitter = ; // Create an object as an emittervar jim = ; // Make an existing object an emittervar jake = name:'Jake' ;; // Mixin in a constructor { thisname = name; ;}
Usage
// People can be emitters too :) { thisname = name; this;} // Emit events when changes occurPersonprototype { thisname = name; this;} var jim = 'Jim'; // Listen for a specific eventjim; // Listen for all 'change:*' eventsjim; // Only listen for the first '*:name' eventjim; // This call will trigger all three handlersjim; // Remove handlers for the 'change:name' event // (removes only the first of the above handlers)jim; // Use groups when adding listeners...jim ; // ...and they can all be removed with a single call.jim;
Emitter API
All emitter methods return the object on which they were called so that you can chain multiple method calls.
emitter.on(event[, group], handler)
Adds an event listener to the emitter. If the event
argument is a regular expression then it will be used to match against event names.
Arguments:
- event: (String|RegExp) used to identify which events should trigger this handler
- group: (Optional String) can be used to remove many related handlers at once
- handler: (Function) the function to call when a matching event is emitted
// Listen for 'test' events // Listen for 'foo:*' events // Logs 'test' // Logs 'test' // Logs 'foo:bar' // Logs 'foo:bat' ; // Logs nothing
emitter.once(event[, group], handler)
Adds an event listener but immediately removes the handler the first time it is triggered.
Arguments:
- event: (String|RegExp) used to identify which events should trigger this handler
- group: (Optional String) can be used to remove many related handlers at once
- handler: (Function) the function to call when a matching event is emitted
// Listen for one 'test' event // Listen for one 'foo:*' event // Logs 'test' // Logs nothing // Logs 'foo:bar' ; // Logs nothing
emitter.off(event[, handler])
Removes handlers for the given event, optionally specifying a specific handler to remove. If handler
is not passed then all handlers are removed. If a regular expression is passed as event
, only listeners added with an identical regular expression are removed. The regex is not executed to match events for removal.
Arguments:
- event: (String|RegExp) used to identify which event's listeners to remove
- handler: (Optional: Function) if specified then only listeners with this function as a handler will be removed
{ return log; } // Listen for 'foo:bar' events // Another listener for 'foo:bar' events // Listen for 'foo:*' events // Logs 'foo:bar' three times // Remove the first handler // Logs 'foo:bar' twice // Remove the last handler // Logs 'foo:bar' once // Remove the remaining handler ; // Logs nothing!
emitter.releaseGroup(group)
Removes all event handlers added with the specified group.
Arguments:
- group: (String) remove all listens associated with this group
// Add 3 listeners in the same group // Remove all three handlers ; // Logs nothing
emitter.emit(event[, data...])
Emits an event and calls the handlers on all matching listeners. Additional data can be passed as arguments and they will be forwarded to handlers.
Arguments:
- event: (String) the event
// Listen for 'foo:*' events // Logs 'foo:bar' // Logs 'foo:bar', 'bag' // Logs 'foo:bar', 'baz', 'bat' ; // Logs nothing