EventBus is a simple event management library for TypeScript.
npm install @ai-zen/event-bus
import EventBus from "@ai-zen/event-bus";
// Create a new instance of EventBus
const eventBus = new EventBus();
// Subscribe to an event
eventBus.on("eventName", (arg1, arg2) => {
// Handle the event
});
// Unsubscribe from an event
eventBus.off("eventName", handler);
// Unsubscribe from all handlers of an event
eventBus.offAll("eventName");
// Destroy the event bus and remove all event subscriptions
eventBus.destroy();
// Emit an event
eventBus.emit("eventName", arg1, arg2);
// Gather the results returned by all handlers of an event
const results = eventBus.gather < T > ("eventName", arg1, arg2);
// Gather the results returned by all handlers of an event as a map
const resultsMap = eventBus.gatherMap < T > ("eventName", arg1, arg2);
// Subscribe to an event and automatically unsubscribe after the first invocation
eventBus.once("eventName", (arg1, arg2) => {
// Handle the event
});
// Subscribe to an event and return a promise that resolves when the event is emitted
const resultPromise = eventBus.promise("eventName");
Subscribes to an event.
name
(required): The name of the event to subscribe to.handler
(required): The event handler function.error
(optional): The error handler function.- Returns: A Disposable object that can be used to unsubscribe from the event.
const disposable = eventBus.on("eventName", (arg1, arg2) => {
// Handle the event
});
// Unsubscribe from the event
disposable.dispose();
Unsubscribes from an event.
name
(required): The name of the event to unsubscribe from.handler
(required): The event handler function.- Returns: True if the unsubscribing was successful, otherwise false.
Unsubscribes all event handlers from an event.
name
(required): The name of the event to unsubscribe all handlers from.
Destroys the event bus by clearing all event subscribers.
Emits an event.
name
(required): The name of the event to emit.args
(optional): The arguments to pass to the event handlers.
This method is used to send an error event through the event bus.
name
(required): The name of the error event to be sent.reason
(required): The reason for the error.
try {
// Do something that might throw an error
const data = await getData();
eventBus.emit("fooEvent", data);
} catch (error) {
eventBus.error("fooEvent", error); // Send an error event
}
// Use the error handler function for eventBus.on
eventBus.on(
"fooEvent",
(data) => {
// Handle the data
},
(error) => {
// Handle the error
}
);
// You can also use a promise catch to handle the error
eventBus.promise("fooEvent").catch((error) => {
// Handle the error
});
Gathers the results from all event handlers of the specified event.
name
(required): The name of the event to gather results from.args
(optional): The arguments to pass to the event handlers.- Returns: An array of results from all event handlers.
Gathers the results from all event handlers of the specified event with a map of the handlers.
name
(required): The name of the event to gather results from.args
(optional): The arguments to pass to the event handlers.- Returns: A map of event handlers and their corresponding results.
Subscribes to an event and unsubscribes automatically after the event is emitted once.
name
(required): The name of the event to subscribe to.handler
(required): The event handler function.error
(optional): The error handler function.- Returns: A Disposable object that can be used to unsubscribe from the event.
const disposable = eventBus.once("eventName", (arg1, arg2) => {
// Handle the event
});
// The handler will be automatically unsubscribed after the first invocation
Returns a promise that resolves when the specified event is emitted.
name
(required): The name of the event to create a promise for.- Returns: A promise that resolves when the event is emitted.
const resultPromise = eventBus.promise("eventName");
resultPromise.then((result) => {
// Handle the result
});
Alias for the on
method.
Alias for the off
method.
Alias for the emit
method.