EventBus
EventBus is a simple event management library for TypeScript.
Installation
npm install @ai-zen/event-bus
Usage
import EventBus from "@ai-zen/event-bus";
const eventBus = new EventBus();
eventBus.on("eventName", (arg1, arg2) => {
});
eventBus.off("eventName", handler);
eventBus.offAll("eventName");
eventBus.destroy();
eventBus.emit("eventName", arg1, arg2);
const results = eventBus.gather < T > ("eventName", arg1, arg2);
const resultsMap = eventBus.gatherMap < T > ("eventName", arg1, arg2);
eventBus.once("eventName", (arg1, arg2) => {
});
const resultPromise = eventBus.promise("eventName");
API
on(name: string, handler: EventHandler): Disposable
Subscribe to an event.
name
(required): The name of the event to subscribe to.handler
(required): The event handler function.- Returns: A Disposable object with a
dispose
method that can be used to unsubscribe from the event.
Example
const disposable = eventBus.on("eventName", (arg1, arg2) => {
});
disposable.dispose();
off(name: string, handler: EventHandler): boolean
Unsubscribe from an event.
name
(required): The name of the event to unsubscribe from.handler
(required): The event handler function.- Returns:
true
if the handler was successfully removed, false
otherwise.
offAll(name: string): void
Unsubscribe from all handlers of an event.
name
(required): The name of the event to unsubscribe from.
destroy(): void
Destroy the event bus and remove all event subscriptions.
emit(name: string, ...args: any[]): void
Emit an event.
name
(required): The name of the event to emit.args
(optional): Arguments to pass to the event handlers.
gather<T>(name: string, ...args: any[]): T[]
Gather the results returned by all handlers of an event.
name
(required): The name of the event to gather results from.args
(optional): Arguments to pass to the event handlers.- Returns: An array containing the results returned by the event handlers.
gatherMap<T>(name: string, ...args: any[]): Map<EventHandler, T>
Gather the results returned by all handlers of an event as a map.
name
(required): The name of the event to gather results from.args
(optional): Arguments to pass to the event handlers.- Returns: A map containing the event handler functions as keys and the results returned by the event handlers as values.
once(name: string, handler: EventHandler): Disposable
Subscribe to an event and automatically unsubscribe after the first invocation.
name
(required): The name of the event to subscribe to.handler
(required): The event handler function.- Returns: A Disposable object with a
dispose
method that can be used to unsubscribe from the event.
Example
const disposable = eventBus.once("eventName", (arg1, arg2) => {
});
promise(name: string): Promise<any>
Subscribe to an event and return a promise that resolves when the event is emitted.
name
(required): The name of the event to subscribe to.- Returns: A promise that resolves with the result of the event handler when the event is emitted.
Example
const resultPromise = eventBus.promise("eventName");
resultPromise.then((result) => {
});
subscribe(name: string, handler: EventHandler): void
Alias for on(name, handler)
.
unsubscribe(name: string, handler: EventHandler): boolean
Alias for off(name, handler)
.
publish(name: string, ...args: any[]): void
Alias for emit(name, ...args)
.
License
MIT