Starting

Instances of a workflow are started by one or more messages. When one of these types of messages are received a new workflow state is created and execution of the workflow begins.

Declare a handler for the message that starts your workflow, and then map it using the WorkflowMapper.

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

export class FulfilmentWorkflow extends Workflow<FulfilmentWorkflowState> {
  configureWorkflow (
    mapper: WorkflowMapper<FulfilmentWorkflowState, FulfilmentWorkflow>
  ): void {
    mapper
      .withState(FulfilmentWorkflowState)
      // Start a new workflow when an `ItemPurchased` event is received
      .startedBy(ItemPurchased, 'shipItem')
  }
  
  // Handles an `ItemPurchased` event
  async shipItem (event: ItemPurchased) {
    // ...
  }
}

In this case, when a ItemPurchased event is received, it will start a new workflow and dispatch the message to the shipItem handler.

Last updated