QEventBus
@quik/events / QEventBus
Class: QEventBus
Defined in: QEventBus.ts:24
A typed in-process domain event bus that decouples event producers from consumers.
Handlers are called in registration order. Errors thrown by individual handlers are caught and logged without interrupting the remaining handlers.
Example
const bus = new QEventBus();
const off = bus.on("user.created", (payload) => console.log(payload));
await bus.emit("user.created", { id: 1 });
off(); // unsubscribe
Constructors
Constructor
new QEventBus():
QEventBus
Defined in: QEventBus.ts:27
Returns
QEventBus
Methods
emit()
emit<
T>(event,payload):Promise<void>
Defined in: QEventBus.ts:88
Emit an event, invoking all registered handlers in registration order.
Errors thrown by individual handlers are caught and logged; they do not prevent remaining handlers from executing.
Type Parameters
T
T = unknown
Parameters
event
string
The event name to emit.
payload
T
The data to pass to each handler.
Returns
Promise<void>
events()
events():
string[]
Defined in: QEventBus.ts:121
Return the names of all events that currently have at least one handler.
Returns
string[]
off()
off(
event?):void
Defined in: QEventBus.ts:109
Remove all handlers for a specific event, or all handlers across all events when no event name is provided.
Parameters
event?
string
The event name to clear. Omit to clear all events.
Returns
void
on()
on<
T>(event,handler): () =>void
Defined in: QEventBus.ts:38
Subscribe to an event.
Type Parameters
T
T = unknown
Parameters
event
string
The event name to listen on.
handler
The function invoked when the event fires.
Returns
A function that removes this subscription when called.
() => void
once()
once<
T>(event,handler): () =>void
Defined in: QEventBus.ts:67
Subscribe to an event but fire the handler only once.
The subscription is removed before the handler is invoked, preventing double-fires under re-entrant emits.
Type Parameters
T
T = unknown
Parameters
event
string
The event name to listen on.
handler
The function invoked when the event fires.
Returns
A function that removes this subscription when called.
() => void