UNPKG

lifecycle-utils

Version:

A set of general utilities for the lifecycle of a JS/TS project/library

67 lines 2.64 kB
/** * `AsyncDisposeAggregator` is a utility class that allows you to add multiple items and then dispose them all at once. * The items are disposed one by one in the order they were added. * When the `parallel` option is enabled, then all the items are disposed in parallel, * triggered by the order in which they were added. * You can add a function to call, an object with a `dispose` method, an object with a `Symbol.dispose` method, * an object with a `Symbol.asyncDispose` method, or a Promise that resolves to one of the previous types. * To dispose all the items, call `dispose` or use the `Symbol.asyncDispose` symbol. * The difference between `AsyncDisposeAggregator` and `DisposeAggregator` is that `AsyncDisposeAggregator` can dispose async targets. * * For example, * ```typescript * import {AsyncDisposeAggregator, EventRelay} from "lifecycle-utils"; * * const disposeAggregator = new AsyncDisposeAggregator(); * * const eventRelay = new EventRelay<string>(); * disposeAggregator.add(eventRelay); * * disposeAggregator.add(async () => { * await new Promise(resolve => setTimeout(resolve, 0)); * // do some async work * }); * * disposeAggregator.dispose(); * ``` */ export declare class AsyncDisposeAggregator { constructor({ parallel }?: { /** * Whether to dispose all the targets in parallel when the aggregator is disposed. * * Defaults to `false`. */ parallel?: boolean; }); /** * Adds a target to be disposed. * You can wrap the target with a `WeakRef` to prevent this class from holding a strong reference to the target. */ add(target: AsyncDisposeAggregatorTarget): this; /** * Disposes all the targets that have been added and clears the list of targets. * * After all the targets have been disposed, if any throws an error or rejects, * then the error from the first such target will be thrown. */ dispose(): Promise<void>; [Symbol.asyncDispose](): Promise<void>; get targetCount(): number; private _disposeTarget; } export type AsyncDisposeAggregatorTarget = AsyncDisposeAggregatorWrappedTarget | Promise<AsyncDisposeAggregatorWrappedTarget>; export type AsyncDisposeAggregatorWrappedTarget = (() => void | Promise<void>) | { [Symbol.asyncDispose](): void | Promise<void>; } | { [Symbol.dispose](): void; } | { dispose(): void | Promise<void>; } | WeakRef<{ [Symbol.asyncDispose](): void | Promise<void>; } | { [Symbol.dispose](): void; } | { dispose(): void | Promise<void>; }>; //# sourceMappingURL=AsyncDisposeAggregator.d.ts.map