Skip to content

Completing ​

A workflow that has finished its work should be marked as complete, after which no more messages are dispatched to it. This page shows how.

Return ctx.complete() from a function workflow handler, or this.completeWorkflow() from a class workflow handler. Either can be given final changes to the state, which are saved with it.

ts
export const fulfilmentWorkflow = defineWorkflow(FulfilmentWorkflowState)
  .startedBy(ItemPurchased, ({ itemId, customerId }) => ({
    itemId,
    customerId
  }))
  // Complete the workflow, saving one last change to its state
  .when(ReceiptEmailed, (_event, _state, ctx) =>
    ctx.complete({ status: 'complete' })
  )
ts
export class FulfilmentWorkflow extends Workflow<FulfilmentWorkflowState> {
  configureWorkflow(
    mapper: WorkflowMapper<FulfilmentWorkflowState, FulfilmentWorkflow>
  ): void {
    mapper
      .withState(FulfilmentWorkflowState)
      .startedBy(ItemPurchased, 'start')
      .when(ReceiptEmailed, 'complete')
  }

  start({ itemId, customerId }: ItemPurchased) {
    return { itemId, customerId }
  }

  complete() {
    // Complete the workflow, saving one last change to its state
    return this.completeWorkflow({ status: 'complete' })
  }
}

The completed state stays in the persistence, with its $status set to complete, but isn't found by later messages.

See also ​

Released under the MIT License.