EventBus<EM extends EventMapBase>

class
eventBus/EventBus.ts:48

Minimal, synchronous pub/sub event bus keyed by **channel** and **type**.

- Handlers are stored per (channel, type) and invoked **synchronously** in subscription order. - Exceptions thrown by a handler are **caught and logged**, and do **not** stop other handlers. - Intended for in-memory, single-process usage (no cross-tab/process broadcasting).

Type parameters

NameTypeDescription
EMextends EventMapBaseEvent map shape: ts type EventMapBase = Record<string, Record<string, unknown>>; // Example: type EM = { ui: { toggle: boolean }; data: { loaded: { items: string[] } }; };

Constructor

constructor

signature
new<EM>(): EventBus<EM>

Type parameters

NameTypeDescription
EMextends EventMapBase

Returns

EventBus<EM>

Methods

clear

Clears **all** listeners across all channels/types. Useful for tests or during HMR teardown to avoid duplicate handlers.

signature
clear(): void

Clears **all** listeners across all channels/types. Useful for tests or during HMR teardown to avoid duplicate handlers.

Returns

void

Example

example
// In a test teardown:
afterEach(() => bus.clear());

emit

Emits an event to all subscribers of the exact (channel, type). Handlers are invoked **synchronously**. Any exception thrown by a handler is caught and logged, and other handlers still run.

signature
emit<C, T>(channel, type, payload, event?): void

Emits an event to all subscribers of the exact (channel, type). Handlers are invoked **synchronously**. Any exception thrown by a handler is caught and logged, and other handlers still run.

Type parameters

NameTypeDescription
Cextends stringChannel key (string key of EM).
Textends stringType key within channel C (string key of EM[C]).

Parameters

NameTypeDescription
channelCChannel name to emit on.
NameTypeDescription
typeTEvent type to emit.
NameTypeDescription
payloadEM[C][T]Payload matching EM[C][T].
NameTypeDescription
event?Event<EM, C, T, EM[C][T]>Optional **source event**, forwarded to handlers as a second argument. Supply it whenever the caller already holds the real event so subscribers observe its true id rather than reconstructing one; omitting it keeps the original behaviour.

Returns

void

Example

example
bus.emit('ui', 'toggle', false);

off

Removes a specific handler previously added with `on`.

signature
off<C, T>(channel, type, handler): void

Removes a specific handler previously added with `on`.

Type parameters

NameTypeDescription
Cextends stringChannel key (string key of EM).
Textends stringType key within channel C (string key of EM[C]).

Parameters

NameTypeDescription
channelCChannel name of the subscription to remove.
NameTypeDescription
typeTEvent type of the subscription to remove.
NameTypeDescription
handler(payload: EM[C][T], event?: Event<EM, C, T, EM[C][T]>) => voidThe same handler reference that was passed to on.

Returns

void

Example

example
const h = (n: number) => console.log('inc', n);
bus.on('math', 'inc', h);

// Explicitly remove this handler:
bus.off('math', 'inc', h);

on

Subscribes a handler to an exact (channel, type).

signature
on<C, T>(channel, type, handler): () => void

Subscribes a handler to an exact (channel, type).

Type parameters

NameTypeDescription
Cextends stringChannel key (must be a string key of EM).
Textends stringType key within channel C (must be a string key of EM[C]).

Parameters

NameTypeDescription
channelCChannel name to subscribe to.
NameTypeDescription
typeTEvent type within the channel.
NameTypeDescription
handler(payload: EM[C][T], event?: Event<EM, C, T, EM[C][T]>) => voidFunction invoked with the payload type EM[C][T]. It optionally receives the **source event** as a second argument when the emitter supplies one, so subscribers can read the true id (and any meta) instead of reconstructing an event from the payload alone. Handlers that declare only payload remain valid.

Returns

() => voidAn **unsubscribe** function that removes this handler.

Example

example
const off = bus.on('data', 'loaded', ({ items }) => {
  console.log('Loaded', items.length, 'items');
});

// Later, stop listening:
off();

Example

example
bus.on('data', 'loaded', (payload, event) => {
  console.log('event id:', event?.id);
});