LooseEventBus<C extends string = string, T extends string = string, P = any>

class
eventBus/LooseEventBus.ts:64

Type parameters

NameTypeDescription
Cextends string
Textends string
P

Constructor

constructor

signature
new<C, T, P>(): LooseEventBus<C, T, P>

Type parameters

NameTypeDescription
Cextends string
Textends string
P

Returns

LooseEventBus<C, T, P>

Methods

clear

Removes **all** listeners (exact and pattern). Useful for tests/HMR teardown.

signature
clear(): void

Removes **all** listeners (exact and pattern). Useful for tests/HMR teardown.

Returns

void

Example

example
afterEach(() => bus.clear());

emit

Emits an event to all exact subscribers first, then to **matching pattern** subscribers. Duplicate handler references are called **once** (de-duped).

signature
emit(channel, type, payload): void

Emits an event to all exact subscribers first, then to **matching pattern** subscribers. Duplicate handler references are called **once** (de-duped).

Parameters

NameTypeDescription
channelCChannel to emit on.
NameTypeDescription
typeTEvent type (subject). A leading dot is ignored for matching.
NameTypeDescription
payloadPPayload delivered to handlers.

Returns

void

Example

example
// Suppose:
//  - on('ui', 'panel.open', h)
//  - on('ui', 'panel.*', h)       // same handler ref!
//  - on('ui', 'panel.**', other)
bus.emit('ui', 'panel.open', { id: 1 });
// => 'h' runs once (de-duped), then 'other'

emitWith

Emits a payload that is only built if somebody is listening.

signature
emitWith(channel, type, make): void

Emits a payload that is only built if somebody is listening.

Parameters

NameTypeDescription
channelCChannel to emit on.
NameTypeDescription
typeTConcrete event type.
NameTypeDescription
make() => PBuilds the payload. Called at most once, and only when a handler matched.

Returns

void

off

Unsubscribes an **exact** handler. The type key is normalized internally, so callers can pass "foo" or ".foo" interchangeably.

signature
off(channel, type, handler): void

Unsubscribes an **exact** handler. The type key is normalized internally, so callers can pass "foo" or ".foo" interchangeably.

Parameters

NameTypeDescription
channelCChannel name.
NameTypeDescription
typeTExact event type key to remove (normalization applied).
NameTypeDescription
handler(payload: P) => voidThe same handler reference previously passed to `on`.

Returns

void

Example

example
const h = () => {};
bus.on('ui', 'panel.open', h);
// Remove it (with or without leading dot)
bus.off('ui', '.panel.open', h);

on

Subscribes a handler to either an **exact** type or a **pattern**.

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

Subscribes a handler to either an **exact** type or a **pattern**.

Parameters

NameTypeDescription
channelCChannel to subscribe on.
NameTypeDescription
typeTExact event type (e.g. "a.b") or pattern (contains */**).
NameTypeDescription
handler(payload: P) => voidFunction invoked with the emitted payload.

Returns

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

Example

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

Example

example
// Match any single sub-event: 'panel.open', 'panel.close', etc.
const offStar = bus.on('ui', 'panel.*', () => {});

// Match any depth: 'panel.open', 'panel.items.add', 'panel', etc.
const offGlob = bus.on('ui', 'panel.**', () => {});