|
| 1 | +import { |
| 2 | + ActivityNodeBuilder, |
| 3 | + BuildingContext, |
| 4 | + MachineContext, |
| 5 | + ActivityNodeConfig, |
| 6 | + STATE_FAILED_TARGET, |
| 7 | + STATE_INTERRUPTED_TARGET |
| 8 | +} from '../../types'; |
| 9 | +import { ActivityStateProvider } from '../../core/activity-context-provider'; |
| 10 | +import { catchUnhandledError } from '../../core/catch-unhandled-error'; |
| 11 | +import { SequenceNodeBuilder } from '../../core/sequence-node-builder'; |
| 12 | +import { getBranchNodeId, getStepNodeId } from '../../core/safe-node-id'; |
| 13 | +import { BranchedStep } from 'sequential-workflow-model'; |
| 14 | +import { ParallelActivityConfig, ParallelActivityState } from './types'; |
| 15 | +import { isInterruptResult, isSkipResult } from '../results'; |
| 16 | + |
| 17 | +export class ParallelActivityNodeBuilder<TStep extends BranchedStep, TGlobalState, TActivityState extends object> |
| 18 | + implements ActivityNodeBuilder<TGlobalState> |
| 19 | +{ |
| 20 | + public constructor( |
| 21 | + private readonly sequenceNodeBuilder: SequenceNodeBuilder<TGlobalState>, |
| 22 | + private readonly config: ParallelActivityConfig<TStep, TGlobalState, TActivityState> |
| 23 | + ) {} |
| 24 | + |
| 25 | + public build(step: TStep, nextNodeTarget: string, buildingContext: BuildingContext): ActivityNodeConfig<TGlobalState> { |
| 26 | + const activityStateProvider = new ActivityStateProvider<TStep, TGlobalState, ParallelActivityState<TActivityState>>( |
| 27 | + step, |
| 28 | + (s, g) => ({ activityState: this.config.init(s, g) }) |
| 29 | + ); |
| 30 | + |
| 31 | + const nodeId = getStepNodeId(step.id); |
| 32 | + const branchNames = Object.keys(step.branches); |
| 33 | + const states: Record<string, ActivityNodeConfig<TGlobalState>> = {}; |
| 34 | + |
| 35 | + for (const branchName of branchNames) { |
| 36 | + const branchNodeId = getBranchNodeId(step.id, branchName); |
| 37 | + const leaveNodeId = branchNodeId + '_LEAVE'; |
| 38 | + const leaveNodeTarget = '#' + leaveNodeId; |
| 39 | + |
| 40 | + states[branchNodeId] = { |
| 41 | + initial: 'TRY_LEAVE', |
| 42 | + states: { |
| 43 | + TRY_LEAVE: { |
| 44 | + invoke: { |
| 45 | + src: catchUnhandledError(async () => { |
| 46 | + // TODO: emit on enter? |
| 47 | + }), |
| 48 | + onDone: [ |
| 49 | + { |
| 50 | + target: 'SEQUENCE', |
| 51 | + cond: (context: MachineContext<TGlobalState>) => { |
| 52 | + const activityState = activityStateProvider.get(context, nodeId); |
| 53 | + return activityState.branchNames?.includes(branchName) ?? false; |
| 54 | + } |
| 55 | + }, |
| 56 | + { |
| 57 | + target: 'LEAVE' |
| 58 | + } |
| 59 | + ], |
| 60 | + onError: STATE_FAILED_TARGET |
| 61 | + } |
| 62 | + }, |
| 63 | + SEQUENCE: this.sequenceNodeBuilder.build(buildingContext, step.branches[branchName], leaveNodeTarget), |
| 64 | + LEAVE: { |
| 65 | + id: leaveNodeId, |
| 66 | + type: 'final' |
| 67 | + } |
| 68 | + } |
| 69 | + }; |
| 70 | + } |
| 71 | + |
| 72 | + const CONDITION: ActivityNodeConfig<TGlobalState> = { |
| 73 | + invoke: { |
| 74 | + src: catchUnhandledError(async (context: MachineContext<TGlobalState>) => { |
| 75 | + const internalState = activityStateProvider.get(context, nodeId); |
| 76 | + const result = await this.config.handler(step, context.globalState, internalState.activityState); |
| 77 | + |
| 78 | + if (isInterruptResult(result)) { |
| 79 | + context.interrupted = nodeId; |
| 80 | + return; |
| 81 | + } |
| 82 | + if (isSkipResult(result)) { |
| 83 | + internalState.branchNames = []; |
| 84 | + return; |
| 85 | + } |
| 86 | + if (Array.isArray(result)) { |
| 87 | + internalState.branchNames = result.map(r => r.branchName); |
| 88 | + return; |
| 89 | + } |
| 90 | + throw new Error('Not supported result for parallel activity'); |
| 91 | + }), |
| 92 | + onDone: [ |
| 93 | + { |
| 94 | + target: STATE_INTERRUPTED_TARGET, |
| 95 | + cond: (context: MachineContext<TGlobalState>) => Boolean(context.interrupted) |
| 96 | + }, |
| 97 | + { |
| 98 | + target: 'PARALLEL' |
| 99 | + } |
| 100 | + ], |
| 101 | + onError: STATE_FAILED_TARGET |
| 102 | + } |
| 103 | + }; |
| 104 | + |
| 105 | + return { |
| 106 | + id: nodeId, |
| 107 | + initial: 'CONDITION', |
| 108 | + states: { |
| 109 | + CONDITION, |
| 110 | + PARALLEL: { |
| 111 | + type: 'parallel', |
| 112 | + states, |
| 113 | + onDone: 'JOIN' |
| 114 | + }, |
| 115 | + JOIN: { |
| 116 | + invoke: { |
| 117 | + src: catchUnhandledError(async (context: MachineContext<TGlobalState>) => { |
| 118 | + const internalState = activityStateProvider.get(context, nodeId); |
| 119 | + internalState.branchNames = undefined; |
| 120 | + }), |
| 121 | + onDone: nextNodeTarget, |
| 122 | + onError: STATE_FAILED_TARGET |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + }; |
| 127 | + } |
| 128 | +} |
0 commit comments