UNPKG

@soil/arch

Version:

Architectural constructs for web applications.

31 lines (30 loc) 978 B
/** * Signature for the listener functions called when an event is sent. */ declare type Listener<Msg> = (msg: Msg) => void; /** * Type-safe event mini-bus, or publisher/subscriber system. Useful for * communicating distant components. */ export declare function chan<Msg>(): { /** * Subscribe a listener to the channel. A function is returned which can * be called to unsubscribe that same listener. An optional second * argument may be passed to specify the maximum number of times the * listener will be notified before automatically unsubscribing it. */ sub(listener: Listener<Msg>, times?: number | undefined): () => void; /** * Send an event to all listeners, with a payload. */ pub<M extends Msg = Msg>(msg: M): void; /** * Return the number of listeners on the channel. */ readonly size: number; /** * Unsubscribe all listeners from the channel. */ clear(): void; }; export {};