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 EventMapBaseEvent map shape: ts type EventMapBase = Record<string, Record<string, unknown>>; // Example: type EM = { ui: { toggle: boolean }; data: { loaded: { items: string[] } }; };

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): 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].

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]) => 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]) => voidFunction invoked with the payload type EM[C][T].

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();