@pivanov/utils
Version:
A focused collection of TypeScript utilities for modern web development
38 lines (37 loc) • 1.31 kB
TypeScript
import type { IEventBus, IEventBusSubscribeOptions, TEventBusListener, TEventBusUnsubscribe } from "./types";
/**
* Dispatches a message to every subscriber on `topic`.
*
* @example
* ```ts
* busDispatch('user-updated', { id: 1, name: 'John' });
* ```
*/
export declare const busDispatch: <T extends IEventBus>(topic: T["topic"], message: T["message"]) => void;
/**
* Subscribes to messages on a specific topic. Returns an unsubscribe function.
*
* Pass `options.onError` to handle listener exceptions (default: `console.error`).
*
* @example
* ```ts
* const unsubscribe = busSubscribe('user-updated', (msg) => {
* console.log(msg);
* });
* unsubscribe();
*
* // Custom error handler
* busSubscribe('x', handler, { onError: (e) => reportBug(e) });
* ```
*/
export declare const busSubscribe: <T extends IEventBus>(topic: T["topic"], listener: TEventBusListener<T["message"]>, options?: IEventBusSubscribeOptions) => TEventBusUnsubscribe;
/**
* Subscribes to a topic and automatically unsubscribes after the first
* matching dispatch.
*
* @example
* ```ts
* busOnce('ready', () => startApp());
* ```
*/
export declare const busOnce: <T extends IEventBus>(topic: T["topic"], listener: TEventBusListener<T["message"]>, options?: IEventBusSubscribeOptions) => TEventBusUnsubscribe;