Parallel Workflow Activity
The parallel
activity is used to execute multiple activities in parallel. You can select the branches to execute based on the workflow state.
import { BranchedStep } from 'sequential-workflow-model';
import { createParallelActivity, interrupt, branchName, BranchNameResult } from 'sequential-workflow-machine';
interface ParallelStep extends BranchedStep {
type: 'parallel';
componentType: 'switch';
/* ... */
}
const parallelActivity = createParallelActivity<ParallelStep, MyGlobalState, MyActivityState>('parallel', {
init: () => {
return {}; // Initial activity state
},
handler: async (step: ParallelStep, globalState: MyGlobalState, activityState: MyActivityState) => {
if (globalState.temperature < 0) {
return interrupt();
}
const branches: BranchNameResult[] = [
branchName('branch1')
];
if (globalState.temperature > 10 && globalState.temperature < 20) {
branches.push(branchName('branch2'));
}
return branches;
}
});