1.6.0 • Published 2 years ago

@riversun/event-emitter v1.6.0

Weekly downloads
45
License
MIT
Repository
github
Last release
2 years ago

@riversun/event-emitter

npm version CircleCI codecov

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

ParamType
eventTypestring
listenerFuncfunction

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

ParamType
eventTypestring
listenerNamestring
listenerFuncfunction

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

ParamType
eventTypestring
dataobject

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

ParamType
eventTypestring

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

1.6.0

2 years ago

1.5.2

4 years ago

1.5.1

5 years ago

1.5.0

5 years ago

1.4.0

5 years ago

1.3.0

5 years ago

1.2.2

5 years ago

1.2.0

5 years ago

1.2.1

5 years ago

1.1.0

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago