Event Dispatcher
Event dispatcher enabled classes to listen to and fire events.
you can inherit from EventDispatcher or use as instance.
Global Event Dispatcher instance in available on app context or can be injected.
Usage#
Inherit#
Instance#
Global Dispatcher#
API#
on#
on(event,callback,[scope],[options])#
add an event listener
- event - event name routing key also supported.
- callback - callback function that will triggered on event name.
- scope - optional, the scope of the callback function default: this.
- options
- once?: boolean - remove the handler after the first call.
- await?: boolean - await the handler promise default false
- parallel?: boolean - await the handler in parallel default true
- timeout?: number - one timeout in await mode.
- order?: number - handler order in callbacks lower will execute first
once#
once(event,[callback],[scope],[options])#
add an event listener will be called only once if no callback passed a promise will be returned
- event - event name.
- callback - callback function that will triggered on event name.
- scope - optional, the scope of the callback function default: this.
- options - same as
on
un#
un(event,callback,[scope])#
remove an event listener. All the arguments must be === to the onces used in the on method, or else it won`t be removed.
- event - event name.
- callback - callback function.
- scope - optional, the scope of the callback function.
fireEvent#
fireEvent(event,[arguments])#
fireEvent - triggers the callback functions of a given event name
- eventName - name of the event
- arguments - all other arguments will be passed to the callback function
fireEventAsync#
fireEventAsync(event,[arguments])#
fireEvent - triggers the callback functions of a given event name and wait for handlers callback promise to resolve.
- eventName - name of the event
- arguments - all other arguments will be passed to the callback function
removeAllListeners#
removeAllListeners()#
removes all event listeners
removeListenersByScope#
removeListenersByScope(scope)#
removes all event listeners by given scope
hasListener#
hasListener(event,callback,[scope]):boolean#
return true if listener exists
bubble#
bubble(event: string, scope: IEventDispatcher)#
fire event given event on given scope
listenerCount#
(event: string): number#
return number of listener for given event
iterator#
iterator<T>(event: string | string[], options?: { limit?: number }): AsyncIterableIterator<T>#
return AsyncIterableIterator for given event