Use Event Manager Or create Listener and subscribe to it.
$ npm install --save @djosmer/event-manager
$ yarn add @djosmer/event-managerCreate Event Manager and add any events.
import {createEventManager} from "@djosmer/event-manager";
const handle = (message: string, code: number) => {
console.log('handle', message, code);
}
const eventManager = createEventManager<[string, number]>();
eventManager.on('eventName', handle);
const unsubscribe = eventManager.on('eventName', handle);
eventManager.addListener('eventName', handle);
eventManager.once('eventName', handle);
eventManager.emit('eventName', 'hello', 45);
//remove second listener;
unsubscribe();
//remove all "handle" listener
eventManager.off('eventName', handle);
// or
eventManager.removeListener('eventName', handle);Or create Listener and subscribe to it.
import {createListenerCollection} from "@djosmer/event-manager";
const handle = (message: string) => {
console.log('handle', message);
}
const listener = createListenerCollection<string>();
//Subscribe to the listener
const unsubscribe = listener.subscribe(handle);
//Notify subscribers
listener.notify('hello');
//Unsubscribe to the listener
unsubscribe();createEventManager<ARGS extends any[] = []>()new EventManager<ARGS extends any[], CB extends ListenerCallback<ARGS> = ListenerCallback<ARGS>>()
Adds the callback function to the end of the ListenerCollection for the event named eventName.
No checks are made to see if the callback has already been added.
Multiple calls passing the same combination of eventName and callback will result in the callback being added,
and called, multiple times.
Removes the specified callback from the ListenerCollection for the event named eventName.
Removes all ListenerCollection, or those of the specified eventName.
Synchronously calls each of the Listener registered for the event named eventName,
in the order they were registered, passing the supplied arguments to each.
Adds a one-time listener for the event named eventName. The next time eventName is triggered, this listener is removed and then invoked.
createListenerCollection<ARGS extends any[] = []>()new ListenerCollection<ARGS extends any[], CB extends ListenerCallback<ARGS> = ListenerCallback<ARGS>>()
Clear the ListenerCollection
Synchronously calls each of the listeners registered for the ListenerCollection,
in the order they were registered, passing the supplied arguments to each.
Subscribe from the listener.
Unsubscribe from the listener.