Creating a workflow

A workflow is defined as a class that contains a number of steps using functions, and the workflow state modelled as a separate class.

A workflow can be started by one or more different types of messages. Each time a workflow is started, a new workflow state is created. This state will be available to each of the steps in the workflow, and each step can mutate the state by returning some of its properties.

Creating

Create a workflow state by declaring a class that extends WorkflowState.

The $name property should be unique among all of your workflows.

// fulfilment-workflow-state.ts
import { WorkflowState } from '@node-ts/bus-core'

export class FulfilmentWorkflowState extends WorkflowState {
  static NAME = 'FulfilmentWorkflowState'
  $name = FulfilmentWorkflowState.NAME
}

Create the workflow definition that extends Workflow, passing it a generic parameter of the workflow state.

By extending this class you'll need to provide aconfigureWorkflow(mapper: WorkflowMapper<TState, TWorkflow>): void implementation. This mapper is used to configure how messages are dispatched to functions in the workflow.

// example-workflow.ts
import { Workflow } from '@node-ts/bus-core'

export class FulfilmentWorkflow extends Workflow<FulfilmentWorkflowState> {
  configureWorkflow (
    mapper: WorkflowMapper<FulfilmentWorkflowState, FulfilmentWorkflow>
  ): void {
    mapper.withState(FulfilmentWorkflowState)
  }
}

Lastly register the workflow with the bus configuration on startup.

const bus = await Bus.configure()
  .withWorkflow(FulfilmentWorkflow)
  .initialize()

Last updated