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 = ;channelEmitter; /* b.js */var channelEmitter = ;channelEmitter; /* c.js */;;// outputs: { '0': true, '1': { foo: [] }, '2': 123 }
With channels
/* a.js */var channelEmitter = ;channelEmitter;channelEmitter; channelEmitter;// returns true;// outputs: foobar: { 0: 'hi' } channelEmitter;// returns false;// outputs: channelEmitter;// returns true;// outputs: foobar: { 0: 'hi' } channelEmitter;// returns true;// outputs: foo.bar: { 0: 'hello' } channelEmitter;// returns true;// outputs: foobar: { 0: 'hi' } channelEmitter;// returns true;// outputs: foo.bar: { 0: 'hello' } channelEmitter;// returns false;// outputs: channelEmitter;// returns true;// outputs: foo.bar: { 0: 'hello' }
API Reference
Example
var channel_emitter = ;
ChannelEmitter
channel-emitter~addListener(eventName, listener) ⇒ 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 |
ChannelEmitter
channel-emitter~removeListener(eventName, listener) ⇒ 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 |
ChannelEmitter
channel-emitter~removeAllListeners([eventName]) ⇒ 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 |
ChannelEmitter
channel-emitter~listenerCount(eventName) ⇒ 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 |
ChannelEmitter
channel-emitter~listeners(eventName) ⇒ 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 |
ChannelEmitter
channel-emitter~on(eventName, listener) ⇒ 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 |
ChannelEmitter
channel-emitter~addChannel(channelName) ⇒ Adds a sub-channel to the current channel.
Kind: inner method of channel-emitter
Param | Type | Description |
---|---|---|
channelName | string |
the name for the channel |
ChannelEmitter
channel-emitter~removeChannel(channelName) ⇒ Removes the sub-channel from the current channel.
Kind: inner method of channel-emitter
Param | Type | Description |
---|---|---|
channelName | string |
the name for the channel |
Boolean
channel-emitter~emit(eventName, [...args]) ⇒ 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 |
Boolean
channel-emitter~broadcast(eventName, [...args]) ⇒ 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