/** * A reference to a created signal handler. */ export interface SignalsHandler { /** * Remove the handler from the signal. * * Any ongoing {@link Promise}s started by the handler will continue to run to completion. */ dispose: () => void; } export type SignalHandlerFunction = (payload: Payload) => Promise | void; export declare function signal(name: string): Signal; export declare class Signal { readonly id: string; constructor(id: string); /** * Listens for signals sent to the current workflow. * * When the signal is received, the handler is invoked. * If the handler return a promise, the handler is added a {@link Promise} * and progressed until completion. * * ```ts * const mySignal = signal("MySignal"); * * workflow("wf", () => { * let done = false; * mySignal.onSignal(async () => { * await duration(10, "seconds"); * done = true; * }); * * await condition(() => done); * }); * ``` * * To remove the handler, call the dispose method. * * ```ts * const handler = mySignal.onSignal(() => {}); * * await duration(10, "seconds"); * * handler.dispose(); * ``` */ onSignal(handler: SignalHandlerFunction): SignalsHandler; /** * Waits for a signal to be received by the workflow. * * The first signal received will resolve the Promise with the payload of the promise. * * ```ts * const mySignal = signal("MySignal"); * workflow("wf", async () => { * const payload = await mySignal.expectSignal(); * * return payload; * }); * ``` * * Use `opts.timeout` to stop waiting after the provided time. The Promise will reject * when the provided time has elapsed. * * ```ts * const mySignal = signal("MySignal"); * workflow("wf", async () => { * try { * const payload = await mySignal.expectSignal({ timeout: duration(10, "minutes) }); * * return payload; * } catch { * return "nothing!"; * } * }); * ``` */ expectSignal(opts?: ExpectSignalOptions): Promise; /** * Allows a {@link workflow} to send this signal to any workflow {@link Execution} by executionId. * * ```ts * const mySignal = signal("MySignal"); * workflow("wf", async () => { * mySignal.sendSignal("payload"); * }) * ``` */ sendSignal(executionId: string, ...args: SendSignalProps): Promise; } export type SignalPayload> = E extends Signal ? P : never; export interface ExpectSignalOptions { /** * Optional. A promise that determines when to timeout a signal. * * Can be used together with {@link time} or {@link duration} or any other promise. * * ```ts * await expectSignal(signal, { timeout: duration(10, "seconds") }) * ``` * * After the provided promise resolves or rejects, the {@link expectSignal} will reject. * * You can also chain an expect signal with other promises. * * ```ts * const abortSignal = expectSignal(abortSignal); * expectSignal(signal, { timeout: abortSignal }); * ``` */ timeout: Promise; } /** * Waits for a signal to be received by the workflow. * * The first signal received will resolve the Promise with the payload of the promise. * * ```ts * workflow("wf", () => { * const payload = await expectSignal("MySignal"); * * return payload; * }); * ``` * * Use `opts.timeout` to stop waiting after some condition. The Promise will reject * when the provided promise resolves. * * ```ts * // timeout after 10 seconds * await expectSignal(signal, { timeout: duration(10, "seconds") }) * ``` * * ```ts * // timeout after receiving a signal * const abortSignal = expectSignal(abortSignal); * await expectSignal(signal, { timeout: abortSignal }); * ``` */ export declare function expectSignal(signal: Signal | string, opts?: ExpectSignalOptions): Promise; /** * Listens for a signal matching the signalId provided. * * When the signal is received, the handler is invoked. * If the handler return a promise, the handler is added as a {@link Promise} * and progressed until completion. * * ```ts * workflow("wf", () => { * let done = false; * onSignal("MySignal", async () => { * await duration(10, "seconds"); * done = true; * }); * * await condition(() => done); * }); * ``` * * To remove the handler, call the dispose method. * * ```ts * const handler = onSignal("MySignal", () => {}); * * await duration(10, "seconds"); * * handler.dispose(); * ``` */ export declare function onSignal(signal: Signal | string, handler: SignalHandlerFunction): SignalsHandler; export type SendSignalProps = [SignalPayload] extends [undefined] | [void] ? [] : [payload: SignalPayload]; /** * Allows a {@link workflow} to send a signal to any workflow {@link Execution} by executionId. * * ```ts * const mySignal = signal("MySignal"); * workflow("wf", async () => { * sendSignal("mySignal", "payload"); * sendSignal(mySignal, "payload"); * }) * ``` * * @param id an optional, execution unique ID, will be used to de-dupe the signal at the target execution. */ export declare function sendSignal(executionId: string, signal: string | Signal, ...args: SendSignalProps): Promise; //# sourceMappingURL=signals.d.ts.map