UNPKG

@akala/core

Version:
88 lines (87 loc) 4.41 kB
import type { AsyncEventBus, EventBus } from "./events/index.js"; import { type EventKeys, type IEvent, type IEventSink } from "./events/shared.js"; export type ResolveHandler<T, TResult> = (value: T) => TResult | PromiseLike<TResult>; export type RejectHandler<TResult> = (reason: unknown) => void | TResult | PromiseLike<TResult>; export { isPromiseLike } from './teardown-manager.js'; /** * Converts a Promise to an Event emitter that fires when the Promise resolves * @template T - The type of the Promise resolution value * @param {PromiseLike<T>} promise - The Promise to convert * @returns {IEventSink<[T], void, unknown>} Event emitter that will emit the resolved value */ export declare function toEvent<T>(promise: PromiseLike<T>): IEventSink<[T], void, unknown>; /** * Converts an Event emitter to a Promise that resolves when the event fires * @template T - The type of the event payload * @param {IEventSink<[T], void, unknown>} event - The event emitter to convert * @returns {PromiseLike<T>} Promise that resolves with the first event payload */ export declare function fromEvent<T>(event: IEventSink<[T], void, unknown>): PromiseLike<T>; /** * Converts an Event emitter to a Promise that resolves when the event fires * @template T - The type of the event payload * @param {IEventSink<[T], void, unknown>} event - The event emitter to convert * @returns {PromiseLike<T>} Promise that resolves with the first event payload */ export declare function fromEventBus<const TEvents extends { [key: PropertyKey]: IEvent<[T], void>; }, T>(bus: EventBus<TEvents>, eventName: EventKeys<TEvents>): PromiseLike<T>; /** * Converts an Event emitter to a Promise that resolves when the event fires * @template T - The type of the event payload * @param {IEventSink<[T], void, unknown>} event - The event emitter to convert * @returns {PromiseLike<T>} Promise that resolves with the first event payload */ export declare function fromAsyncEventBus<const TEvents extends { [key: PropertyKey]: IEvent<[T], Promise<void> | void>; }, T>(bus: AsyncEventBus<TEvents>, eventName: EventKeys<TEvents>): Promise<PromiseLike<T>>; /** * A Deferred Promise pattern implementation allowing external resolution control * @template T - The type of the resolved value * @template TError - The type of the rejection reason (defaults to Error) */ export declare class Deferred<T, TError = Error> implements PromiseLike<T> { private _resolve?; private _reject?; promise: Promise<T>; /** * Resolves the deferred Promise with a value * @param {T | PromiseLike<T> | undefined} _value - The resolution value * @throws {Error} If called before Promise initialization */ resolve(_value?: T | PromiseLike<T> | undefined): void; /** * Rejects the deferred Promise with a reason * @param {TError} _reason - The rejection reason * @throws {Error} If called before Promise initialization */ reject(_reason?: TError): void; constructor(); then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: TError) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<TResult1 | TResult2>; catch<TResult = never>(onrejected?: ((reason: TError) => TResult | PromiseLike<TResult>) | undefined | null): Promise<T | TResult>; finally(onfinally?: (() => void) | undefined | null): Promise<T>; get [Symbol.toStringTag](): string; } /** * Creates a Promise that resolves after a specified delay * @param {number} delay - Delay duration in milliseconds * @returns {Promise<void>} Promise that resolves after the delay */ export declare function delay(delay: number): Promise<unknown>; /** * Wraps a Promise with a timeout, rejecting if it doesn't resolve in time * @template T - The type of the original Promise resolution * @param {PromiseLike<T>} promise - The Promise to wrap * @param {number} timeoutInMs - Timeout duration in milliseconds * @returns {PromiseLike<T>} New Promise that either resolves with the original value or rejects with 'timeout' */ export declare function whenOrTimeout<T>(promise: PromiseLike<T>, timeoutInMs: number): PromiseLike<T>; /** * Enum representing the possible states of a Promise * @enum {number} */ export declare enum PromiseStatus { Pending = 0, Resolved = 1, Rejected = 2 }