// deno-lint-ignore-file no-explicit-any import { ResolvablePromise } from 'https://ghuc.cc/worker-tools/resolvable-promise/index.ts'; // const queueMicrotask = typeof globalThis.queueMicrotask === "function" // ? globalThis.queueMicrotask // : (callback: VoidFunction) => Promise.resolve().then(callback).catch(e => setTimeout(() => { throw e })); export class ExtendablePromise implements Promise[]> { #values: PromiseSettledResult[] = []; #promise: ResolvablePromise[]>; #numAdded = 0; #numSettled = 0; constructor(f?: T | PromiseLike) { // super(_ => _(void 0 as any)); this.#promise = new ResolvablePromise(); this.waitUntil(f); // queueMicrotask(() => { // if (this.#numAdded === 0) { // this.#promise.resolve([]); // } // }); } #fulfill(i: number, value: T) { this.#values[i] = { status: 'fulfilled', value }; if (++this.#numSettled === this.#numAdded) { this.#promise.resolve(this.#values); } } #reject(i: number, reason: any) { this.#values[i] = { status: 'rejected', reason }; if (++this.#numSettled === this.#numAdded) { this.#promise.resolve(this.#values); } } waitUntil(f?: T | PromiseLike) { if (f != null) { const i = this.#numAdded; Promise.resolve(f) .then(v => this.#fulfill(i, v), r => this.#reject(i, r)) this.#numAdded++; } } /** @deprecated Name of this property might change */ get settled() { return this.#promise.settled } then[], TResult2 = never>(onfulfilled?: ((value: PromiseSettledResult[]) => TResult1 | PromiseLike) | null, onrejected?: ((reason: any) => TResult2 | PromiseLike) | null): Promise { return this.#promise.then(onfulfilled, onrejected); } catch(onrejected?: ((reason: any) => TResult | PromiseLike) | null): Promise[] | TResult> { return this.#promise.catch(onrejected); } finally(onfinally?: (() => void) | null): Promise[]> { return this.#promise.finally(onfinally); } readonly [Symbol.toStringTag] = 'ExtendablePromise' } export interface PromiseFulfilledResult { status: "fulfilled"; value: T; } export interface PromiseRejectedResult { status: "rejected"; reason: any; } export type PromiseSettledResult = PromiseFulfilledResult | PromiseRejectedResult;