EventBus

Source: events.ts:12

Singleton event bus behind sdk.on and sdk.off. Typed by EventMap, so a handler receives the payload its event name declares.


Constructors

new EventBus(): EventBus

Returns

EventBus


Methods

on< K extends keyof EventMap, >( event: K, handler: (data: EventMap[K]) => void, ): () => void

Registers handler for event and returns a function that removes it again.

Prefer the returned function over EventBus.off: off needs the exact handler reference, so an inline arrow passed straight to on would otherwise be impossible to remove. Calling the returned function twice is harmless, as is calling it after an equivalent off.

Inside an enhancer, use this.on(...) instead — it keeps this unsubscribe and runs it on destroy(). Registering here directly leaks the handler for the lifetime of the page, because the bus is a singleton.

Handlers are held in a Set, so registering the same function reference twice registers it once, and the first unsubscribe removes it for both callers.

Type Parameters

  • K extends keyof EventMap

Parameters

  • event (K)
  • handler ((data: EventMap[K]) => void)

Returns

  • () => void

Example

CODE
const stop = EventBus.getInstance().on('cart:item-added', ({ packageId }) => {
  console.log('added', packageId);
});
stop(); // handler removed

emit< K extends keyof EventMap, >( event: K, data: EventMap[K], ): void

Type Parameters

  • K extends keyof EventMap

Parameters

Returns

  • void

off<K extends keyof EventMap>(event: K, handler: Function): void

Type Parameters

  • K extends keyof EventMap

Parameters

  • event (K)
  • handler (Function)

Returns

  • void

removeAllListeners<K extends keyof EventMap>(event?: K): void

Type Parameters

  • K extends keyof EventMap

Parameters

  • event (K, optional)

Returns

  • void

Static Methods

getInstance(): EventBus

Returns