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

class
eventBus/LooseEventBus.ts:44

Flexible, synchronous pub/sub bus that supports **exact** and **pattern** event subscriptions.

- **Exact handlers** subscribe to a specific (channel, type) pair. Type keys are **normalized** by stripping a single leading dot (".foo""foo"). - **Pattern handlers** subscribe using wildcards over dot-separated segments: - * matches **one** segment. - ** matches **zero or more** segments (greedy). - On `emit`, exact handlers fire first, then any matching pattern handlers. - Handlers are **de-duplicated**: if the same function is both exact and pattern-registered, it is called **once**. - Handler invocation is **synchronous**. Exceptions are caught and logged; remaining handlers still run.

Type parameters

NameTypeDescription
Cextends stringChannel name type (defaults to string).
Textends stringEvent type name type (defaults to string). Types are treated as **dot-separated paths** (e.g. "a.b.c").
PPayload type for all events (defaults to any).

Constructor

constructor

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

Type parameters

NameTypeDescription
Cextends stringChannel name type (defaults to string).
Textends stringEvent type name type (defaults to string). Types are treated as **dot-separated paths** (e.g. "a.b.c").
PPayload type for all events (defaults to any).

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'

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.**', () => {});