UNPKG

contexify

Version:

A TypeScript library providing a powerful dependency injection container with context-based IoC capabilities, inspired by LoopBack's Context system.

89 lines (87 loc) 2.62 kB
/** * This module contains utilities to convert events to promises and async iterators. * It's a simplified version of the p-event package (https://github.com/sindresorhus/p-event) * adapted for our specific needs. */ /** * Error thrown when a promise times out */ declare class TimeoutError extends Error { constructor(message: string); } /** * Options for pEventMultiple */ interface MultipleOptions<T = unknown> { /** * Events that will reject the promise. * @default ['error'] */ rejectionEvents?: string[]; /** * Whether to return all arguments from the event callback. * @default false */ multiArgs?: boolean; /** * Whether to resolve the promise immediately. * @default false */ resolveImmediately?: boolean; /** * The number of times the event needs to be emitted before the promise resolves. */ count: number; /** * The time in milliseconds before timing out. * @default Infinity */ timeout?: number; /** * A filter function for accepting an event. */ filter?: (value: T) => boolean; /** * An AbortSignal to abort waiting for the event. */ signal?: AbortSignal; } /** * Options for pEvent */ interface Options<T = unknown> extends Partial<Omit<MultipleOptions<T>, 'count'>> { } /** * A promise with a cancel method */ interface CancelablePromise<T> extends Promise<T> { cancel: () => void; } /** * Wait for multiple event emissions */ declare function pEventMultiple<T = unknown>(emitter: any, event: string | symbol | (string | symbol)[], options: MultipleOptions<T>): CancelablePromise<T[]>; /** * Wait for a single event emission */ declare function pEvent<T = unknown>(emitter: any, event: string | symbol | (string | symbol)[], options?: Options<T> | ((value: T) => boolean)): CancelablePromise<T>; /** * Options for pEventIterator */ interface IteratorOptions<T = unknown> extends Omit<Options<T>, 'timeout'> { /** * Events that will end the iterator. * @default [] */ resolutionEvents?: string[]; /** * The maximum number of events for the iterator before it ends. * @default Infinity */ limit?: number; } /** * Create an async iterator for events */ declare function pEventIterator<T = unknown>(emitter: any, event: string | symbol | (string | symbol)[], options?: IteratorOptions<T> | ((value: T) => boolean)): AsyncIterableIterator<T>; export { type CancelablePromise, type IteratorOptions, type MultipleOptions, type Options, TimeoutError, pEvent, pEventIterator, pEventMultiple };