// deno-lint-ignore-file no-explicit-any import { ResolvablePromise } from 'https://ghuc.cc/worker-tools/resolvable-promise/index.ts'; import { pipe } from 'https://cdn.skypack.dev/ts-functional-pipe@3.1.2?dts'; const id = (_: T) => _; type Awaitable = T | PromiseLike; export type TaskState = 'idle' | 'pending' | 'fulfilled' | 'rejected'; class Task { #task; #promise; #state: TaskState = 'idle' constructor(task: () => Awaitable) { this.#task = task; this.#promise = new ResolvablePromise(); } execute() { if (this.#state === 'idle') { this.#state = 'pending' this.#promise.resolve(this.#task()) this.#promise.then( () => { this.#state = 'fulfilled' }, () => { this.#state = 'rejected' }, ); } } get state(): TaskState { return this.#state } get promise(): Promise { return this.#promise } } const lock = Symbol('key'); // TODO: Make own module? // TODO: Add abort signal? // TODO: use executor instead of task functions? // TODO: Remove TT type?? export class TaskPromise implements Promise { #task: Task; #mapFn; #mappedPromise; static from(task: () => Awaitable) { return new TaskPromise(lock, new Task(task)) } private constructor( key: symbol, task: Task, mapFn?: ((value: TT, i?: 0, p?: TaskPromise) => Awaitable) | undefined | null, thisArg?: any, ) { if (key !== lock) throw Error('Illegal constructor'); this.#task = task; this.#mapFn = mapFn; this.#mappedPromise = this.#task.promise.then(mapFn && (x => mapFn.call(thisArg, x, 0, this))); } get state() { return this.#task.state; } /** * Starts the execution of the task associated with this task promise. * If you don't want to start the task at this moment, use `.map` instead. */ then( onfulfilled?: ((value: T) => Awaitable) | undefined | null, onrejected?: ((reason: any) => Awaitable) | undefined | null ): Promise { this.#task.execute(); return this.#mappedPromise.then(onfulfilled, onrejected) } /** * Applies transformations to the resolved value without triggering execution. * Returns another task promise that triggers execution via `.then` */ map( mapFn?: ((value: T, i?: 0, p?: TaskPromise) => Awaitable) | undefined | null, thisArg?: any ): TaskPromise { // @ts-ignore: types of id function (x => x) not correctly inferred... return new TaskPromise(lock, this.#task, pipe(this.#mapFn??id, mapFn??id), thisArg); } catch(onrejected?: ((reason: any) => Awaitable) | null): Promise { // FIXME: should this also trigger execution? return this.#mappedPromise.catch(onrejected) } finally(onfinally?: (() => void) | null): Promise { // FIXME: should this also trigger execution? return this.#mappedPromise.finally(onfinally) } readonly [Symbol.toStringTag] = 'TaskPromise' }