Skip to main content Link Search Menu Expand Document (external link)

Workflow.ts overview

Since v1.0.0


Exports Grouped by Category


Compensation

withCompensation

Add compensation logic to an effect inside a Workflow. The compensation finalizer will be called if the entire workflow fails, allowing you to perform cleanup or other actions based on the success value and the cause of the workflow failure.

NOTE: Compensation will not work for nested activities. Compensation finalizers are only registered for top-level effects in the workflow.

Signature

declare const withCompensation: {
  <A, R2>(
    compensation: (value: A, cause: Cause.Cause<unknown>) => Effect.Effect<void, never, R2>
  ): <E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R | R2 | WorkflowInstance | Scope.Scope>
  <A, E, R, R2>(
    effect: Effect.Effect<A, E, R>,
    compensation: (value: A, cause: Cause.Cause<unknown>) => Effect.Effect<void, never, R2>
  ): Effect.Effect<A, E, R | R2 | WorkflowInstance | Scope.Scope>
}

Source

Since v1.0.0

Constructors

fromTaggedRequest

Signature

declare const fromTaggedRequest: <S extends AnyTaggedRequestSchema>(
  schema: S,
  options?: { readonly suspendedRetrySchedule?: Schedule.Schedule<any, unknown> | undefined }
) => Workflow<S["_tag"], S, S["success"], S["failure"]>

Source

Since v1.0.0

make

Signature

declare const make: <
  const Name extends string,
  Payload extends Schema.Struct.Fields | AnyStructSchema,
  Success extends Schema.Schema.Any = typeof Schema.Void,
  Error extends Schema.Schema.All = typeof Schema.Never
>(options: {
  readonly name: Name
  readonly payload: Payload
  readonly idempotencyKey: (
    payload: Payload extends Schema.Struct.Fields ? Schema.Struct.Type<Payload> : Payload["Type"]
  ) => string
  readonly success?: Success
  readonly error?: Error
  readonly suspendedRetrySchedule?: Schedule.Schedule<any, unknown> | undefined
  readonly annotations?: Context.Context<never>
}) => Workflow<Name, Payload extends Schema.Struct.Fields ? Schema.Struct<Payload> : Payload, Success, Error>

Source

Since v1.0.0

Models

Any (interface)

Signature

export interface Any {
  readonly [TypeId]: TypeId
  readonly name: string
  readonly payloadSchema: AnyStructSchema
  readonly successSchema: Schema.Schema.Any
  readonly errorSchema: Schema.Schema.All
  readonly annotations: Context.Context<never>
  readonly executionId: (payload: any) => Effect.Effect<string>
}

Source

Since v1.0.0

Execution (interface)

Signature

export interface Execution<Name extends string> {
  readonly _: unique symbol
  readonly name: Name
}

Source

Since v1.0.0

Registration (interface)

Signature

export interface Registration<Name extends string> {
  readonly _: unique symbol
  readonly name: Name
}

Source

Since v1.0.0

Registrations (type alias)

Signature

type Registrations<Workflows> =
  Workflows extends Workflow<infer _Name, infer _Payload, infer _Success, infer _Error> ? Registration<_Name> : never

Source

Since v1.0.0

Requirements (type alias)

Signature

type Requirements<Workflows> =
  Workflows extends Workflow<infer _Name, infer _Payload, infer _Success, infer _Error>
    ? _Payload["Context"] | _Success["Context"] | _Error["Context"]
    : never

Source

Since v1.0.0

Workflow (interface)

Signature

export interface Workflow<
  Name extends string,
  Payload extends AnyStructSchema,
  Success extends Schema.Schema.Any,
  Error extends Schema.Schema.All
> {
  readonly [TypeId]: TypeId
  readonly name: Name
  readonly payloadSchema: Payload
  readonly successSchema: Success
  readonly errorSchema: Error
  readonly annotations: Context.Context<never>

  /**
   * Add an annotation to the workflow.
   */
  annotate<I, S>(tag: Context.Tag<I, S>, value: S): Workflow<Name, Payload, Success, Error>

  /**
   * Add the annotations from a Context object to the workflow.
   */
  annotateContext<I>(context: Context.Context<I>): Workflow<Name, Payload, Success, Error>

  /**
   * Execute the workflow with the given payload.
   */
  readonly execute: <const Discard extends boolean = false>(
    payload: [keyof Payload["fields"]] extends [never]
      ? void
      : Schema.Simplify<Schema.Struct.Constructor<Payload["fields"]>>,
    options?: {
      readonly discard?: Discard
    }
  ) => Effect.Effect<
    Discard extends true ? void : Success["Type"],
    Discard extends true ? never : Error["Type"],
    WorkflowEngine | Registration<Name> | Payload["Context"] | Success["Context"] | Error["Context"]
  >

  /**
   * Interrupt a workflow execution for the given execution ID.
   */
  readonly interrupt: (executionId: string) => Effect.Effect<void, never, WorkflowEngine | Registration<Name>>

  /**
   * Create a layer that registers the workflow and provides an effect to
   * execute it.
   */
  readonly toLayer: <R>(
    execute: (payload: Payload["Type"], executionId: string) => Effect.Effect<Success["Type"], Error["Type"], R>
  ) => Layer.Layer<
    Registration<Name> | WorkflowEngine,
    never,
    | WorkflowEngine
    | Exclude<R, WorkflowEngine | WorkflowInstance | Execution<Name> | Scope.Scope>
    | Payload["Context"]
    | Success["Context"]
    | Error["Context"]
  >

  /**
   * For the given payload, compute the deterministic execution ID.
   */
  readonly executionId: (
    payload: Schema.Simplify<Schema.Struct.Constructor<Payload["fields"]>>
  ) => Effect.Effect<string>

  /**
   * Add compensation logic to an effect inside a Workflow. The compensation finalizer will be
   * called if the entire workflow fails, allowing you to perform cleanup or
   * other actions based on the success value and the cause of the workflow failure.
   *
   * NOTE: Compensation will not work for nested activities. Compensation
   * finalizers are only registered for top-level effects in the workflow.
   */
  readonly withCompensation: {
    <A, R2>(
      compensation: (value: A, cause: Cause.Cause<Error["Type"]>) => Effect.Effect<void, never, R2>
    ): <E, R>(
      effect: Effect.Effect<A, E, R>
    ) => Effect.Effect<A, E, R | R2 | WorkflowInstance | Execution<Name> | Scope.Scope>
    <A, E, R, R2>(
      effect: Effect.Effect<A, E, R>,
      compensation: (value: A, cause: Cause.Cause<Error["Type"]>) => Effect.Effect<void, never, R2>
    ): Effect.Effect<A, E, R | R2 | WorkflowInstance | Execution<Name> | Scope.Scope>
  }
}

Source

Since v1.0.0

Result

Complete (class)

Signature

declare class Complete<A, E>

Source

Since v1.0.0

SchemaFromSelf (static method)

Signature

declare const SchemaFromSelf: <Success extends Schema.Schema.Any, Error extends Schema.Schema.All>(_options: {
  readonly success: Success
  readonly error: Error
}) => Schema.Schema<Complete<Success["Type"], Error["Type"]>>

Source

Since v1.0.0

SchemaEncoded (static method)

Signature

declare const SchemaEncoded: <Success extends Schema.Schema.Any, Error extends Schema.Schema.All>(options: {
  readonly success: Success
  readonly error: Error
}) => Schema.Struct<{ _tag: Schema.tag<"Complete">; exit: Schema.Exit<Success, Error, typeof Schema.Defect> }>

Source

Since v1.0.0

Schema (static method)

Signature

declare const Schema: <Success extends Schema.Schema.Any, Error extends Schema.Schema.All>(options: {
  readonly success: Success
  readonly error: Error
}) => Schema.Schema<Complete<Success["Type"], Error["Type"]>, CompleteEncoded<Success["Encoded"], Error["Encoded"]>>

Source

Since v1.0.0

[ResultTypeId] (property)

Signature

readonly [ResultTypeId]: unique symbol

Source

Since v1.0.0

CompleteEncoded (interface)

Signature

export interface CompleteEncoded<A, E> {
  readonly _tag: "Complete"
  readonly exit: Schema.ExitEncoded<A, E, unknown>
}

Source

Since v1.0.0

Result

Signature

declare const Result: <Success extends Schema.Schema.Any, Error extends Schema.Schema.All>(options: {
  readonly success: Success
  readonly error: Error
}) => Schema.Schema<
  Result<Success["Type"], Error["Type"]>,
  ResultEncoded<Success["Encoded"], Error["Encoded"]>,
  Success["Context"] | Error["Context"]
>

Source

Since v1.0.0

Result (type alias)

Signature

type Result<A, E> = Complete<A, E> | Suspended

Source

Since v1.0.0

ResultEncoded (type alias)

Signature

type ResultEncoded<A, E> = CompleteEncoded<A, E> | typeof Suspended.Encoded

Source

Since v1.0.0

ResultTypeId

Signature

declare const ResultTypeId: unique symbol

Source

Since v1.0.0

ResultTypeId (type alias)

Signature

type ResultTypeId = typeof ResultTypeId

Source

Since v1.0.0

Suspended (class)

Signature

declare class Suspended

Source

Since v1.0.0

[ResultTypeId] (property)

Signature

readonly [ResultTypeId]: unique symbol

Source

Since v1.0.0

intoResult

Signature

declare const intoResult: <A, E, R>(
  effect: Effect.Effect<A, E, R>
) => Effect.Effect<Result<A, E>, never, R | WorkflowInstance>

Source

Since v1.0.0

isResult

Signature

declare const isResult: <A = unknown, E = unknown>(u: unknown) => u is Result<A, E>

Source

Since v1.0.0

Symbols

TypeId

Signature

declare const TypeId: unique symbol

Source

Since v1.0.0

TypeId (type alias)

Signature

type TypeId = typeof TypeId

Source

Since v1.0.0

constructors

AnyTaggedRequestSchema (interface)

Signature

export interface AnyTaggedRequestSchema extends AnyStructSchema {
  readonly _tag: string
  readonly Type: PrimaryKey.PrimaryKey
  readonly success: Schema.Schema.Any
  readonly failure: Schema.Schema.All
}

Source

Since v1.0.0

utils

AnyStructSchema (interface)

Signature

export interface AnyStructSchema extends Pipeable {
  readonly [Schema.TypeId]: any
  readonly make: any
  readonly Type: any
  readonly Encoded: any
  readonly Context: any
  readonly ast: AST.AST
  readonly fields: Schema.Struct.Fields
  readonly annotations: any
}

Source

Since v1.0.0