@riversun/event-emitter
v1.6.0
Published
Helper class for sending and receiving events. - Register a listener to receive events. - Also, when an event occurs, call the event to the Listener registered in advance using the emit method
Downloads
909
Readme
@riversun/event-emitter
Helper class for sending and receiving events.
- Register a listener to receive events.
- Also, when an event occurs, call the event to the Listener registered in advance using the emit method
MIT License
install
npm install @riversun/event-emitter
usage
on() method
on() method adds event listener functions that receives events。
const eventEmitter = new EventEmitter();
eventEmitter.on('testEvent', data => {
console.log(data);
});
emit() method
emit() method sends an event with the specified event name and data to all registered listener functions
eventEmitter.emit('testEvent', {testKey: 'testValue'});
only() method
only() method can limit the event listener function that receives events to only one.
eventEmitter.only('testEvent', 'unique-listener', callbackFunc);
Only one listener is registered per "listenerName" even if called multiple times. If the same listenerName is set for listener, the old listener will be removed.
onAny() method
eventEmitter.onAny(data => {});
eventEmitter.emit('testEvent1', {testKey: 'testValue1'});
eventEmitter.emit('testEvent2', {testKey: 'testValue2'});
Set onAny to catch all events that occur.
run tests
npm test
Classes
Functions
on(eventType, listenerFunc)
Set eventType you want to receive and the listener function to be callbacked from #emit method (This eventType will never fire unless called with emit)
Kind: global function
| Param | Type | | --- | --- | | eventType | string | | listenerFunc | function |
removeListener(eventType, listenerFunc)
Remove specified event listener
Kind: global function
| Param | | --- | | eventType | | listenerFunc |
only(eventType, listenerName, listenerFunc)
Only one listener is registered per "listenerName" even if called multiple times. If the same listenerName is set for listener, the old listener will be removed.
Kind: global function
| Param | Type | | --- | --- | | eventType | string | | listenerName | string | | listenerFunc | function |
pipe(eventEmitter)
Set the emitter that receives the callback of this emitter. When the specified emitter is received a callback, the specified emitter also emits it to its listener.
Kind: global function
| Param | | --- | | eventEmitter |
emit(eventType, data)
Emit data to listeners (callback functions) registered with the "on()" method.
Kind: global function
| Param | Type | | --- | --- | | eventType | string | | data | object |
getAllListeners() ⇒ Object
Returns all listeners like below. result={ testEvent: { listeners: [ [Function (anonymous)] ], childEventEmitters: [ { childEmitterIdx: 0, listeners: [Array] } ] } }
Kind: global function
hasListenerFuncs(eventType) ⇒ boolean
Returns true if at least one ListenerFunction that receives the event specified by "eventType" is registered
Kind: global function
| Param | Type | | --- | --- | | eventType | string |
clearAll()
Clear all related listeners
Kind: global function
addOnIntercepterFunc(funcName, func)
Add callback func(s) to notify when calling on() method.
Kind: global function
| Param | | --- | | funcName | | func |
removeOnIntercepterFunc(funcName)
Add callback func to notify when calling on() method.
Kind: global function
| Param | | --- | | funcName |
getAllOnIntercepterFuncs()
Returns callback func and func name to notify when calling on() method.
Kind: global function