import { CancellationScope } from './cancellation-scope'; import { untrackPromise } from './stack-helpers'; /** * A `PromiseLike` helper which exposes its `resolve` and `reject` methods. * * Trigger is CancellationScope-aware: it is linked to the current scope on * construction and throws when that scope is cancelled. * * Useful for e.g. waiting for unblocking a Workflow from a Signal. * * @example * ```ts * import { defineSignal, setHandler, sleep, Trigger } from '@temporalio/workflow'; * * const completeUserInteraction = defineSignal('completeUserInteraction'); * * export async function waitOnUser(userId: string): Promise { * const userInteraction = new Trigger(); * * // programmatically resolve Trigger * setHandler(completeUserInteraction, () => userInteraction.resolve(true)); * * const userInteracted = await Promise.race([userInteraction, sleep('30 days')]); * if (!userInteracted) { * await sendReminderEmail(userId); * } * } * ``` */ export class Trigger implements PromiseLike { // Typescript does not realize that the promise executor is run synchronously in the constructor // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore public readonly resolve: (value: T | PromiseLike) => void; // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore public readonly reject: (reason?: any) => void; protected readonly promise: Promise; constructor() { this.promise = new Promise((resolve, reject) => { const scope = CancellationScope.current(); if (scope.cancellable) { untrackPromise(scope.cancelRequested.catch(reject)); } // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore this.resolve = resolve; // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore this.reject = reject; }); // Avoid unhandled rejections untrackPromise(this.promise.catch(() => undefined)); } then( onfulfilled?: ((value: T) => TResult1 | PromiseLike) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike) | undefined | null ): PromiseLike { return this.promise.then(onfulfilled, onrejected); } }