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.
1
import { Workflow } from '@node-ts/bus-core'
2
​
3
export class FulfilmentWorkflow extends Workflow<FulfilmentWorkflowState> {
4
configureWorkflow (
5
mapper: WorkflowMapper<FulfilmentWorkflowState, FulfilmentWorkflow>
6
): void {
7
mapper
8
.withState(FulfilmentWorkflowState)
9
// Start a new workflow when an `ItemPurchased` event is received
10
.startedBy(ItemPurchased, 'shipItem')
11
}
12
13
// Handles an `ItemPurchased` event
14
async shipItem (event: ItemPurchased) {
15
// ...
16
}
17
}
Copied!
In this case, when a ItemPurchased event is received, it will start a new workflow and dispatch the message to the shipItem handler.
​
Copy link