channel-emitter
v0.1.0
Published
An event emitter with channel and broadcast support
Downloads
2
Readme
channel-emitter
Installation
npm i --save channel-emitter
What is ChannelEmitter?
- A "singleton" EventEmitter - at its core it is a pseudo-singleton (it relies on the node module cache) EventEmitter. Every module within a process that requires ChannelEmitter will recieve the same cached object.
- A multi-channel emitter - ChannelEmitter supports channels as well as sub-channels. This allows listeners for an event on specific channels as well as emitting up to parent channels and broadcasting down to sub-channels.
Features
- Provides a "singleton" (a node module cached object).
- Allows name-spacing of events through "channels".
- Every channel can have multiple sub-channels.
- A channel can emit to all events on the channel as well as up to all parent channels.
- A channel can broadcast to all events on the channel as well as down to all sub-channels.
Examples
Basic Usage
As a singleton
/* a.js */
var channelEmitter = require('channel-emitter');
channelEmitter.on('foo', function () { console.log(arguments); });
/* b.js */
var channelEmitter = require('channel-emitter');
channelEmitter.emit('foo', true, {foo: []}, 123);
/* c.js */
require('./a');
require('./b');
// outputs: { '0': true, '1': { foo: [] }, '2': 123 }
With channels
/* a.js */
var channelEmitter = require('channel-emitter');
channelEmitter.on('foobar', function () { console.log('foobar: ', arguments); });
channelEmitter.on('foo.bar', function () { console.log('foo.bar: ', arguments); });
channelEmitter.emit('foobar', 'hi');
// returns true;
// outputs: foobar: { 0: 'hi' }
channelEmitter.emit('bar', 'hello');
// returns false;
// outputs:
channelEmitter.emit('foo.foobar', 'hi');
// returns true;
// outputs: foobar: { 0: 'hi' }
channelEmitter.emit('foo.bar', 'hello');
// returns true;
// outputs: foo.bar: { 0: 'hello' }
channelEmitter.broadcast('foobar', 'hi');
// returns true;
// outputs: foobar: { 0: 'hi' }
channelEmitter.broadcast('bar', 'hello');
// returns true;
// outputs: foo.bar: { 0: 'hello' }
channelEmitter.broadcast('foo.foobar', 'hi');
// returns false;
// outputs:
channelEmitter.broadcast('foo.bar', 'hello');
// returns true;
// outputs: foo.bar: { 0: 'hello' }
API Reference
Example
var channel_emitter = require('channel-emitter');
channel-emitter~addListener(eventName, listener) ⇒ ChannelEmitter
Wrapper for the EventEmitter.addListener
method that will auto-add channels
if the specified delimiter is used in the name.
Kind: inner method of channel-emitter
| Param | Type | Description | | --- | --- | --- | | eventName | string | the name for the event | | listener | function | the listener for the event |
channel-emitter~removeListener(eventName, listener) ⇒ ChannelEmitter
Wrapper for the EventEmitter.removeListener
method that will remove
events from a specified channl if the specified delimiter is used in the name.
Kind: inner method of channel-emitter
| Param | Type | Description | | --- | --- | --- | | eventName | string | the name for the event | | listener | function | the listener for the event |
channel-emitter~removeAllListeners([eventName]) ⇒ ChannelEmitter
Wrapper for the EventEmitter.removeAllListeners
method that will remove
if the specified delimiter is used in the name.
Kind: inner method of channel-emitter
| Param | Type | Description | | --- | --- | --- | | [eventName] | string | the name for the event |
channel-emitter~listenerCount(eventName) ⇒ ChannelEmitter
Wrapper for the EventEmitter.listenerCount
method that will return the
listener count on a channel (including name-spaced events).
Kind: inner method of channel-emitter
| Param | Type | Description | | --- | --- | --- | | eventName | string | the name for the event |
channel-emitter~listeners(eventName) ⇒ ChannelEmitter
Wrapper for the EventEmitter.listeners
method that will return the
listeners on a channel (including name-spaced events).
Kind: inner method of channel-emitter
| Param | Type | Description | | --- | --- | --- | | eventName | string | the name for the event |
channel-emitter~on(eventName, listener) ⇒ ChannelEmitter
Wrapper for the EventEmitter.on
method that will auto-add channels
if the specified delimiter is used in the name.
Kind: inner method of channel-emitter
| Param | Type | Description | | --- | --- | --- | | eventName | string | the name for the event | | listener | function | the listener for the event |
channel-emitter~addChannel(channelName) ⇒ ChannelEmitter
Adds a sub-channel to the current channel.
Kind: inner method of channel-emitter
| Param | Type | Description | | --- | --- | --- | | channelName | string | the name for the channel |
channel-emitter~removeChannel(channelName) ⇒ ChannelEmitter
Removes the sub-channel from the current channel.
Kind: inner method of channel-emitter
| Param | Type | Description | | --- | --- | --- | | channelName | string | the name for the channel |
channel-emitter~emit(eventName, [...args]) ⇒ Boolean
EventEmitter wrapper that emits an event to siblings and direct ancestor channels.
Kind: inner method of channel-emitter
| Param | Type | Description | | --- | --- | --- | | eventName | string | the name for the registered event | | [...args] | * | arguments to emit to the event |
channel-emitter~broadcast(eventName, [...args]) ⇒ Boolean
Broadcasts an event to siblings and descendent channels.
Kind: inner method of channel-emitter
| Param | Type | Description | | --- | --- | --- | | eventName | string | the name for the registered event | | [...args] | * | arguments to broadcast to the event |
License
Apache 2.0