Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/future.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767/// <reference lib="dom" />import type { FutureOperation } from "./types.ts";import type { StatusEnum, StatusEventDetailMap, StatusEventMap,} from "./status.ts";
import { createStatusEventDispatcher, waitForEvent } from "./events.ts";import { Status, StatusEvent } from "./status.ts";import { CancellationError } from "./errors.ts";
import { timeout, AsyncDisposableStack } from "./disposal.ts";import { GENERATOR_RETURN_TIMEOUT } from "./constants.ts";
/** * The `Future` class is a more powerful and flexible alternative to JavaScript's native `Promise`, * designed to address several weaknesses of Promises such as lack of cancellation, limited concurrency control, and * inability to pause/resume tasks. `Future` instances are cancellable, pausable, compositional, and structured for * concurrency management. * * ## Key Features * * - **Pausable**: Execution can be paused and resumed at will, giving precise control over flow. * - **Cancellable**: Futures can be cancelled at any point. * - **Background Execution**: You can prepare Futures for background execution with the `inBackground` method. * - **Compositional**: Chain, sequence, and compose multiple futures with ease. * - **Advanced Concurrency Control**: Supports throttling, limiting concurrency, and structured concurrency. * * ## Usage Examples * * ### Simple Future Creation * ```typescript * import * as Future from "./mod.ts"; * * const future = Future.from(async function* () { * yield 42; // Pauses and returns 42 * return 100; // Completes and returns 100 * }); * * console.log(await future.toPromise()); // Logs 100 * ``` * * ### Pausing and Resuming * ```typescript * import * as Future from "./mod.ts"; * * const future = Future.from(async function* () { * yield 1; * yield 2; * return 3; * }); * * future.pause(); * setTimeout(() => future.resume(), 1000); * * for await (const value of future) { * console.log(value); // Logs 1, 2, then 3 * } * ``` * * ### Running in the Background * ```typescript * import * as Future from "./mod.ts"; * * const backgroundFuture = Future.inBackground(Future.from(async function* () { * yield 1; * return 2; * })); * * console.log(await backgroundFuture.toPromise()); // Executes in idle time, returns 2 * ``` * * @template T - The type of the value that the future will resolve to. */export class Future<T, TReturn = unknown, TNext = unknown> implements // @ts-ignore Iterator is defined but typescript doesn't recognize it yet // extends globalThis.Iterator PromiseLike<T | TReturn> { #generator?: AsyncGenerator<T, T | TReturn, TNext> | null; #operation?: FutureOperation<T, TReturn, TNext> | null; #abort: AbortController | null = null;
#current: T | null = null; #resolved: T | TReturn | undefined | null = null; #status: StatusEnum = Status.Idle; readonly #disposables = new AsyncDisposableStack(); readonly #events = createStatusEventDispatcher();
/** * Creates a new `Future` instance. * @param operation - A function that returns an async generator to define the asynchronous task. */ constructor( operation: FutureOperation<T, TReturn, TNext>, abort: AbortController | undefined | null = new AbortController(), ) { // super();
this.#abort = abort; this.#operation = operation; this.#generator = this.#wrapper( this.#operation, this.#disposables ); }
is(id: StatusEnum): boolean { return this.#status === id; }
getStatus(): StatusEnum { return this.#status; }
#setStatus<K extends keyof StatusEventMap>( status: K, details?: StatusEventDetailMap[K], ) { this.#status = status; this.#events.dispatch(new StatusEvent(status, details)); }
#handleEvent(event: Event) { if (event.type === "abort") { this.#setStatus(Status.Cancelled, this.#reason); } }
get #reason() { return this.#abort?.signal?.reason; }
/** * Pauses the execution of the future. * @example * ```typescript * import * as Future from "./mod.ts"; * const future = Future.from(async function* () { * yield 42; * return 100; * }); * future.pause(); // Pauses future execution * ``` */ pause(): Future<T, TReturn, TNext> { if (!this.is(Status.Paused)) { this.#setStatus(Status.Paused); } return this; }
/** * Resumes a paused future. * @example * ```typescript * import * as Future from "./mod.ts"; * const future = Future.from(async function* () { * yield 42; * return 100; * }); * future.resume(); // Resumes future execution * ``` */ resume(): Future<T, TReturn, TNext> { if (this.is(Status.Paused)) { this.#setStatus(Status.Running); } return this; }
/** * Resets the future for re-execution, allowing it to run from the beginning. * This can only be done if the future is complete. * @throws Error if the future is not complete. * @example * ```typescript * import * as Future from "./mod.ts"; * const future = Future.from(async function* () { * yield 42; * return 100; * }); * await future.toPromise(); // Completes the future * future.reset(); // Resets the future for reuse * ``` */ reset(): Future<T, TReturn, TNext> { if (this.is(Status.Destroyed)) { throw new Error("Cannot reset a destroyed future"); }
if (!( this.is(Status.Completed) || this.is(Status.Cancelled) )) { throw new Error("Cannot reset a running or incomplete future"); }
this.#generator = null; this.#current = null; this.#resolved = null;
this.#setStatus(Status.Idle); this.#generator = this.#wrapper( this.#operation!, this.#disposables ); return this; }
complete(value: T | TReturn | undefined): Future<T, TReturn, TNext> { this.#setStatus(Status.Completed, { value }); return this; }
/** * Cancels the future, preventing further execution. * @example * ```typescript * import * as Future from "./mod.ts"; * const future = Future.from(async function* () { * yield 42; * return 100; * }); * future.cancel(); // Aborts future execution * ``` */ async cancel(reason: unknown = new CancellationError()): Promise< | StatusEvent<StatusEnum, unknown> | StatusEvent<"idle", unknown> | Error | undefined > { using events = this.#events.events; const eventPromise = waitForEvent(events, Status.Cancelled);
await Promise.all([ Promise.race([ eventPromise, timeout(GENERATOR_RETURN_TIMEOUT), ]), this.#abort?.abort?.(reason), ]);
return await eventPromise; }
async dispose(): Promise<void> { await this.cancel(); await this.#disposables?.disposeAsync?.();
this.#abort?.signal?.removeEventListener?.("abort", this.#eventhandler); await this.#eventhandler?.[Symbol.asyncDispose]?.(); await this.#events?.[Symbol.asyncDispose]?.();
// @ts-ignore Resetting private properties this.#eventhandler = null as unknown;
this.#abort = null; this.#operation = null; this.#generator = null;
this.#current = null; this.#resolved = null;
this.#status = Status.Destroyed; }
[Symbol.asyncDispose](): Promise<void> { return this.dispose(); }
/** * Implements the async iterator protocol, allowing futures to be used in `for await...of` loops. * * This method is designed to support both **push-based** and **pull-based** workflows, * where values can either be automatically yielded by the generator or "pulled" from it via external input. * * ### Key Concepts to Understand: * * **Iterators and Iterables**: * - An **iterator** is an object that defines a sequence of values, typically with a `.next()` method. * - An **iterable** is an object that implements the `Symbol.iterator` method, returning an iterator. * - **Async iterators** are similar to iterators but involve asynchronous operations (using `Promise`). * - **for await...of loops** allow you to consume async iterators just like regular iterators but in an asynchronous context. * * **next() and yield**: * - The `yield` keyword pauses a generator and returns a value to the outside world. * - The `next()` method resumes the generator and can **both send a value in** and **receive the next yielded value**. * - The confusing part: `yield` can be the value that `next()` returns, and the input passed into `next()` can be used as the value of the last `yield`. * * In essence, each call to `next()` resumes the generator from where it last left off, and the value passed to `next()` can be accessed within the generator. * * ### Push-Based Workflow: * The generator automatically yields values in a push-based workflow, so the consumer doesn't need to send any input. * * @example Push-Based Workflow * ```typescript * import * as Future from "./mod.ts"; * * // Push-based async generator example * const future = Future.from(async function* () { * yield 1; * yield 2; * yield 3; * return 4; * }); * * for await (const value of future) { * console.log(value); // Logs 1, 2, 3 * } * ``` * * ### Pull-Based Workflow: * In a pull-based workflow, the generator waits for input via `next()`. The generator will only proceed when `next()` is called. * * @example Pull-Based Workflow * ```typescript * import * as Future from "./mod.ts"; * * // Pull-based async generator example * const future = Future.from(async function* () { * let result = { value: 1, done: false }; * * // Wait for external input before proceeding * while (!result.done) { * result = await (yield result.value); * } * * return result.value; * }); * * const iterator = future[Symbol.asyncIterator](); * console.log(await iterator.next()); // { value: 1, done: false } * console.log(await iterator.next({ value: 2, done: false })); // { value: 2, done: false } * console.log(await iterator.next({ value: 3, done: true })); // { value: 3, done: true } * ``` * * ### Breakdown of the Code: * * **Push-Based**: * - The generator automatically yields values, and the consumer just needs to await those values. * - Example: A generator yielding values without expecting input. * * **Pull-Based**: * - The generator waits for input from the consumer using the `yield` keyword. * - The consumer controls when the next value is processed by passing in data via `next()`. * * ### Advanced Concepts: * * - **Priming the Generator**: * - We prime the generator by calling `next()` once before the loop starts. This ensures the generator is ready to receive input. * - **Handling Pauses and Cancellation**: * - If the future is paused, the generator will wait until it's resumed. * - If the future is canceled, an error will be thrown. * * ### Method Breakdown: * * This method is responsible for managing the generator's lifecycle, including handling: * - **Initialization**: Sets up the generator and ensures it's ready for iteration. * - **Event Handling**: Manages the status events, such as handling pauses, cancellations, and completion. * - **Error Handling**: Catches and propagates errors both within the generator and from external sources. * - **Finalization**: Ensures that the generator is properly cleaned up after completion or error. * * #### Note on `generator.return`: * - The `generator.return` method is called to ensure that any cleanup logic within the generator (e.g., releasing resources, closing connections) is executed if the generator is prematurely terminated. * - While this may not always be strictly necessary (e.g., if the generator has no cleanup tasks), it is considered good practice to call it when manually terminating a generator to avoid potential memory leaks or incomplete operations. * * @param operation - The async generator function representing the future's operation. * @returns An async generator that yields values generated by the `operation`. * @yields The values generated by the future. */ async *#wrapper( operation: FutureOperation<T, TReturn, TNext>, disposables: AsyncDisposableStack = this.#disposables, ): AsyncGenerator<T, T | TReturn, TNext> { let result: IteratorResult<T, T | TReturn> | undefined;
try { // Create generator. const generator = this.#createGenerator( operation, disposables );
// Continue yielding values until the generator completes. do { try { // If the future is aborted, propagate the abort reason. this.#abort?.signal?.throwIfAborted();
// Handle pausing of the future. await this.#handlePause();
// Handle completion and return final value if needed. if (this.is(Status.Completed)) { return await this.#handleCompletion(generator, result); }
// Proceed with the generator's next result. result = await this.#getNextResult( generator, result ? yield result?.value! as T : undefined, );
if (!result?.done) { this.#current = result?.value; } } catch (error) { // Handle error and attempt early return from generator. const returnedResult = await this.#handleError(generator); if (returnedResult) result = returnedResult;
// Rethrow error if it wasn't handled. throw error; } } while (!result?.done);
// Return final value once generator completes. return result?.value; } catch (error) { // If the future wasn't already aborted, abort it now. this.#abortFuture(error);
// Rethrow the error to ensure it's handled upstream throw error; } finally { this.#resolved = result?.value;
// Dispatch the completed event. this.#events.dispatch( new StatusEvent( Status.Completed, { value: result?.value }, ), ); } }
/** Helper method to initialize the generator. */ #createGenerator( operation: FutureOperation<T, TReturn, TNext>, disposables: AsyncDisposableStack = this.#disposables, ): AsyncGenerator<T, T | TReturn, TNext> | Generator<T, T | TReturn, TNext> { // Reset abort controller if already aborted. if (this.#abort?.signal?.aborted) { this.#abort?.signal?.removeEventListener?.("abort", this.#eventhandler); this.#abort = new AbortController(); }
// Attach event handler for abort signal. this.#abort?.signal?.addEventListener?.("abort", this.#eventhandler, { once: true, });
const generator = operation?.(this.#abort!, disposables); if (!generator || typeof generator?.next !== "function") { throw new Error("Generator not defined"); }
return generator; }
/** Helper method to handle pausing of the future. */ async #handlePause(): Promise<void> { if (this.is(Status.Paused)) { using events = this.#events.events; for await (const event of events) { if ( event.type === Status.Running || event.type === Status.Idle ) break; } } }
/** Helper method to handle the completion of the future. */ async #handleCompletion( generator: | AsyncGenerator<T, T | TReturn, TNext> | Generator<T, T | TReturn, TNext>, result: IteratorResult<T, T | TReturn> | undefined, ): Promise<T | TReturn> { const value = result?.value;
const finished = await generator?.return?.(value!); if (finished) return finished?.value;
return value!; }
/** Helper method to get the next result from the generator. */ async #getNextResult( generator: | AsyncGenerator<T, T | TReturn, TNext> | Generator<T, T | TReturn, TNext>, input?: TNext, ): Promise<IteratorResult<T, T | TReturn>> { // Advance the generator by calling `.next` with the provided input const iteratorResult = input !== undefined ? generator?.next?.(input as TNext) : generator?.next?.();
// Wait for either the generator's next result or a cancellation event using events = this.#events.events; const event = await Promise.race([ iteratorResult, // Continue with the generator waitForEvent(events, Status.Cancelled, { signal: this.#abort?.signal }), // Listen for cancellation ]);
// If a cancellation event occurs or the abort signal is triggered, throw an error if ( (event as StatusEvent)?.type === Status.Cancelled || this.#abort?.signal?.aborted ) { throw this.#reason; }
// Await and return the resolved iterator result return await iteratorResult; }
/** * Handles errors within the generator, attempting to return early if possible. * * ### Purpose of `generator.return`: * - The `generator.return` method is called during error handling to allow the generator to perform any necessary cleanup. * - This is crucial in scenarios where the generator may need to release resources or save state before being discarded. * - Even though not all generators require `return` (e.g., those without cleanup logic), it is recommended to call `return` to avoid potential memory leaks or incomplete operations. * * @param generator - The async generator being managed. * @param error - The error that occurred during the generator's execution. * @returns A promise resolving to the result of the generator's return, if applicable. */ async #handleError( generator: | AsyncGenerator<T, T | TReturn, TNext> | Generator<T, T | TReturn, TNext>, ): Promise<IteratorResult<T, T | TReturn> | undefined> { try { // If an error occurs, attempt to return early from the generator if (generator?.return) { const returnedResult = generator.return(undefined as TReturn); using timeoutPromise = timeout(GENERATOR_RETURN_TIMEOUT);
// Attempt to return early if there's an error await Promise.race([ returnedResult, timeoutPromise, ]);
return await returnedResult; } } catch { console.warn("[Future] Generator return timed out"); } }
/** Helper method to abort the future if it hasn't been aborted already. */ #abortFuture(err: unknown): void { // If the future wasn't already aborted, abort it now if (!this.#abort?.signal?.aborted) { this.#abort?.abort?.(err); } }
[Symbol.asyncIterator](): AsyncGenerator<T, T | TReturn, TNext> { if (this.is(Status.Destroyed)) { throw new Error("Cannot iterate over a destroyed future"); }
if (!this.#generator) { throw new Error("Generator not defined"); }
return this.#generator!; }
next(...args: [] | [TNext]): PromiseLike<IteratorResult<T, T | TReturn>> { return this.#generator!.next?.(...args); }
return( value?: T | TReturn | PromiseLike<T | TReturn>, ): PromiseLike<IteratorResult<T, T | TReturn>> { return this.#generator!.return?.(value!); }
throw(e: unknown): PromiseLike<IteratorResult<T, T | TReturn>> { return this.#generator!.throw?.(e); }
/** * Clones the current Future, creating a new instance with the same operation. * The cloned Future starts in the idle state, allowing it to run independently of the original. * * @returns A new Future instance with the same operation. * * @example * ```typescript * const originalFuture = Future.from(async function* () { * yield 1; * return 2; * }); * * const clonedFuture = originalFuture.clone(); * console.log(await clonedFuture.toPromise()); // Logs 2 * ``` */ clone(): Future<T, TReturn, TNext> { if (!this.#operation) { throw new Error("Cannot clone a Future without an operation."); }
// Create a new Future with the same operation return new Future<T, TReturn, TNext>(this.#operation); }
/** * Converts the future into a promise and waits for its resolution. * Includes support for control flow operations including pausing, cancelling, and more... * * @returns A promise that resolves to the final result of the future. * If the generator does not have an explicit return, it will return undefined. * @example * ```typescript * import * as Future from "./mod.ts"; * * const future = Future.from(async function* () { * yield 42; * // No explicit return, so the final result will be undefined * }); * const result = await future.toPromise(); // result is undefined * ``` */ async toPromise(): Promise<T | TReturn> { let result: IteratorResult<T, T | TReturn>;
if (this.is(Status.Completed) && this.#resolved !== null) { return this.#resolved as T | TReturn; }
// Iterate through the generator until completion do { result = await this.next() as IteratorResult< T, T | TReturn >; } while (!result.done);
// If the generator has no explicit return, result.value will be undefined return (result!.value ?? this.#resolved ?? undefined) as T | TReturn; }
/** * `then` method to allow Future to be used with `await` and promise chaining. * @param onfulfilled Called when the future resolves successfully. * @param onrejected Called when the future is rejected. * @returns A promise that resolves with the result of the future. */ then<TResult1 = T | TReturn, TResult2 = never>( onfulfilled?: | ((value: T | TReturn) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: | ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | undefined | null, ): Promise<TResult1 | TResult2> { return this.toPromise().then(onfulfilled, onrejected); }
/** * `catch` method to handle rejections. * @param onrejected A callback that handles the rejection reason. * @returns A promise that resolves or rejects based on the future's outcome. */ catch<TResult = never>( onrejected?: | ((reason: unknown) => TResult | PromiseLike<TResult>) | undefined | null, ): Promise<T | TReturn | TResult> { return this.toPromise().catch(onrejected); }
/** * `finally` method to allow adding a cleanup step after the future resolves or rejects. * @param onfinally Called when the future is complete. * @returns A promise that resolves to the final result. */ finally(onfinally?: (() => void) | undefined | null): Promise<T | TReturn> { return this.toPromise().finally(onfinally); }
// /** // * Converts the Future instance to a JSON representation. // * This is useful for logging or debugging, providing insight into the Future's status and values. // * // * @returns An object with the Future's status, current value, and resolved value. // */ // toJSON(): object { // return { // FutureState: this.getStatus(), // FutureResult: this.#resolved ?? '<pending>', // FutureCurrent: this.#current, // }; // }
// /** // * Returns the primitive value representation of the Future. // * If the Future is resolved, returns the resolved value; otherwise, returns null. // * // * @returns The resolved value of the Future, or null if not resolved. // */ // valueOf(): T | TReturn | null { // return this.#resolved ?? this.#current ?? null; // }
// /** // * Returns a string representation of the Future. // * This is useful for debugging or logging, providing a human-readable description of the Future's state. // * // * @returns A string describing the Future's status, current value, and resolved value. // */ // toString(): string { // const state = this.#status; // const result = this.#resolved ?? '<pending>'; // return `Future { [${state}]: ${result}, [Current]: ${this.#current} }`; // // return `Future { status: ${this.getStatus()}, current: ${this.#current}, resolved: ${this.#resolved} }`; // }
static #PrivateEventHandler = class PrivateEventHandler<T, TReturn, TNext> { #delegate: WeakRef<Future<T, TReturn, TNext>> | undefined | null; constructor(delegate?: Future<T, TReturn, TNext>) { if (delegate) { this.#delegate = new WeakRef(delegate); } }
handleEvent(event: Event): void { if (this.#delegate) { const future = this.#delegate?.deref(); if (future) future.#handleEvent(event); } }
dispose(): void { this.#delegate = null; }
[Symbol.dispose](): void { this.dispose(); }
[Symbol.asyncDispose](): Promise<void> { return Promise.resolve(this.dispose()); } };
#eventhandler = new Future.#PrivateEventHandler(this);}
export default Future;