@temporalio/workflow
Version:
Temporal.io SDK Workflow sub-package
110 lines (107 loc) • 5.23 kB
TypeScript
/**
* This library provides tools required for authoring workflows.
*
* ## Usage
* See the {@link https://docs.temporal.io/typescript/hello-world#workflows | tutorial} for writing your first workflow.
*
* ### Timers
*
* The recommended way of scheduling timers is by using the {@link sleep} function. We've replaced `setTimeout` and
* `clearTimeout` with deterministic versions so these are also usable but have a limitation that they don't play well
* with {@link https://docs.temporal.io/typescript/cancellation-scopes | cancellation scopes}.
*
* ```ts
* import { sleep } from '@temporalio/workflow';
*
* export async function sleeper(ms = 100): Promise<void> {
* await sleep(ms);
* console.log('slept');
* }
* ```
*
* ### Activities
*
* To schedule Activities, use {@link proxyActivities} to obtain an Activity function and call.
*
* ```ts
* import { proxyActivities } from '@temporalio/workflow';
* import type * as activities from './activities';
*
* const { sendEmail } = proxyActivities<typeof activities>({
* startToCloseTimeout: '1 minute',
* });
export async function sampleWorkflow(): Promise<string> {
await sendEmail("to@example.com","Hello, Temporal!");
}
* ```
*
* ### Updates, Signals and Queries
*
* Use {@link setHandler} to set handlers for Updates, Signals, and Queries.
*
* Update and Signal handlers can be either async or non-async functions. Update handlers may return a value, but signal
* handlers may not (return `void` or `Promise<void>`). You may use Activities, Timers, child Workflows, etc in Update
* and Signal handlers, but this should be done cautiously: for example, note that if you await async operations such as
* these in an Update or Signal handler, then you are responsible for ensuring that the workflow does not complete first.
*
* Query handlers may **not** be async functions, and may **not** mutate any variables or use Activities, Timers,
* child Workflows, etc.
*
* #### Implementation
*
* ```ts
* export const incrementSignal = wf.defineSignal<[number]>('increment');
* export const getValueQuery = wf.defineQuery<number>('getValue');
* export const incrementAndGetValueUpdate = wf.defineUpdate<number, [number]>('incrementAndGetValue');
*
* export async function counterWorkflow(initialValue: number): Promise<void> {
* let count = initialValue;
* wf.setHandler(incrementSignal, (arg: number) => {
* count += arg;
* });
* wf.setHandler(getValueQuery, () => count);
* wf.setHandler(incrementAndGetValueUpdate, (arg: number): number => {
* count += arg;
* return count;
* });
* await wf.condition(() => false);
* }
* ```
*
* ### More
*
* - [Deterministic built-ins](https://docs.temporal.io/typescript/determinism#sources-of-non-determinism)
* - [Cancellation and scopes](https://docs.temporal.io/typescript/cancellation-scopes)
* - {@link CancellationScope}
* - {@link Trigger}
* - [Sinks](https://docs.temporal.io/application-development/observability/?lang=ts#logging)
* - {@link Sinks}
*
* @module
*/
export { ActivityCancellationType, ActivityFailure, ActivityOptions, ApplicationFailure, CancelledFailure, ChildWorkflowFailure, defaultPayloadConverter, PayloadConverter, RetryPolicy, rootCause, SearchAttributes, // eslint-disable-line @typescript-eslint/no-deprecated
SearchAttributeValue, // eslint-disable-line @typescript-eslint/no-deprecated
ServerFailure, TemporalFailure, TerminatedFailure, TimeoutFailure, } from '@temporalio/common';
export * from '@temporalio/common/lib/errors';
export { ActivityFunction, ActivityInterface, // eslint-disable-line @typescript-eslint/no-deprecated
Payload, QueryDefinition, SignalDefinition, UntypedActivities, Workflow, WorkflowQueryType, WorkflowResultType, WorkflowReturnType, WorkflowSignalType, } from '@temporalio/common/lib/interfaces';
export * from '@temporalio/common/lib/workflow-handle';
export * from '@temporalio/common/lib/workflow-options';
export { AsyncLocalStorage, CancellationScope, CancellationScopeOptions } from './cancellation-scope';
export * from './errors';
export * from './interceptors';
export { ChildWorkflowCancellationType, ChildWorkflowOptions, ContinueAsNew, ContinueAsNewOptions, EnhancedStackTrace, StackTraceFileLocation, StackTraceFileSlice, ParentClosePolicy, ParentWorkflowInfo, RootWorkflowInfo, StackTraceSDKInfo, StackTrace, UnsafeWorkflowInfo, WorkflowInfo, } from './interfaces';
export { proxySinks, Sink, SinkCall, SinkFunction, Sinks } from './sinks';
export { log } from './logs';
export { Trigger } from './trigger';
export * from './workflow';
export { ChildWorkflowHandle, ExternalWorkflowHandle } from './workflow-handle';
export { metricMeter } from './metrics';
export { createNexusServiceClient, NexusServiceClientOptions, NexusServiceClient, NexusOperationHandle, NexusOperationCancellationType, } from './nexus';
export {
/**
* @deprecated Do not use LoggerSinks directly. To log from Workflow code, use the `log` object
* exported by the `@temporalio/workflow` package. To capture log messages emitted
* by Workflow code, set the {@link Runtime.logger} property.
*/
LoggerSinksDeprecated as LoggerSinks, } from './logs';