Class: EntityEventBus
Defined in: src/events/EntityEventBus.ts:27
High-performance, zero-dependency async event bus for entity domain and lifecycle events.
Supports exact event names ('Account:created'), table-level events ('accounts:created'),
wildcard patterns ('*:created', 'Account:*', '*'), and domain event types ('EntityCreated').
Example
db.on('Account:created', async (entity) => {
await eventBus.publish(new AccountCreatedEvent(entity));
});
Constructors
Constructor
new EntityEventBus():
EntityEventBus
Returns
EntityEventBus
Methods
on()
on<
T>(event,handler):this
Defined in: src/events/EntityEventBus.ts:51
Registers a persistent event listener for matching events.
Type Parameters
| Type Parameter | Default type |
|---|---|
T | any |
Parameters
| Parameter | Type | Description |
|---|---|---|
event | string | The event name or pattern (e.g. 'Account:created', '*:deleted', 'EntityCreated'). |
handler | EventHandler<T> | The asynchronous or synchronous callback function. |
Returns
this
this instance for chaining.
once()
once<
T>(event,handler):this
Defined in: src/events/EntityEventBus.ts:68
Registers a one-time event listener that auto-unregisters after its first invocation.
Type Parameters
| Type Parameter | Default type |
|---|---|
T | any |
Parameters
| Parameter | Type | Description |
|---|---|---|
event | string | The event name or pattern. |
handler | EventHandler<T> | The callback function. |
Returns
this
this instance for chaining.
off()
off(
event,handler?):this
Defined in: src/events/EntityEventBus.ts:85
Unregisters an event listener. If handler is omitted, all listeners for event are removed.
Parameters
| Parameter | Type | Description |
|---|---|---|
event | string | The event name or pattern. |
handler? | EventHandler<any> | The specific callback function to remove. |
Returns
this
this instance for chaining.
emit()
emit(
event,payload,alias?):Promise<void>
Defined in: src/events/EntityEventBus.ts:103
Emits an event with the given payload, invoking all matching listeners.
Parameters
| Parameter | Type | Description |
|---|---|---|
event | string | The concrete event name being emitted (e.g. 'Account:created'). |
payload | any | The entity or event object to pass to handlers. |
alias? | string | Optional alias event name (e.g. table name 'accounts:created'). |
Returns
Promise<void>
publish()
publish(
event,payload,alias?):Promise<void>
Defined in: src/events/EntityEventBus.ts:129
Publishes an event to listeners (alias for emit).
Parameters
| Parameter | Type |
|---|---|
event | string |
payload | any |
alias? | string |
Returns
Promise<void>
listenerCount()
listenerCount(
event?):number
Defined in: src/events/EntityEventBus.ts:136
Returns the count of listeners registered for a specific pattern or in total.
Parameters
| Parameter | Type |
|---|---|
event? | string |
Returns
number
removeAllListeners()
removeAllListeners(
event?):this
Defined in: src/events/EntityEventBus.ts:144
Removes all registered event listeners, or all listeners for a given pattern.
Parameters
| Parameter | Type |
|---|---|
event? | string |
Returns
this