parallel-universe
Version:
The set of async flow control structures and promise utils.
53 lines (52 loc) • 2.16 kB
TypeScript
import { AbortablePromise } from './AbortablePromise';
/**
* Publish–subscribe pattern implementation that guarantees the delivery of published messages even if any of listeners
* would throw an error.
*
* @template T The published message.
*/
export declare class PubSub<T = void> {
private _listeners;
/**
* The number of subscribed listeners.
*/
get listenerCount(): number;
/**
* Synchronously invokes listeners with the published message.
*
* @param message The published message.
*/
publish(message: T): void;
/**
* Waits for a message that satisfies the given predicate to be published and resolves with that message.
*
* @template R A subtype of T that the predicate function identifies as satisfying the condition.
* @param predicate A function that takes the message as a parameter and returns true if the message satisfies the condition, otherwise false.
* @returns An {@link AbortablePromise} that resolves with the published message that satisfies the predicate.
*/
waitFor<R extends T>(predicate: (message: T) => message is R): AbortablePromise<R>;
/**
* Waits for a message that satisfies the given predicate to be published and resolves with that message.
*
* @param predicate A function that takes the message as a parameter and returns true if the message satisfies the condition, otherwise false.
* @returns An {@link AbortablePromise} that resolves with the published message that satisfies the predicate.
*/
waitFor(predicate: (message: T) => boolean): AbortablePromise<T>;
/**
* Adds a listener that would receive all messages published via {@link publish}.
*
* @param listener The callback.
* @returns The callback that unsubscribes the listener.
*/
subscribe(listener: (message: T) => any): () => void;
/**
* Unsubscribes a listener.
*
* @param listener The callback that was previously {@link subscribe subscribed}.
*/
unsubscribe(listener: (message: T) => any): void;
/**
* Unsubscribes all listeners.
*/
unsubscribeAll(): void;
}