UNPKG

@5minds/processcube_engine

Version:

The ProcessCube Engine. Stores and executes BPMNs.

78 lines 4.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ParallelActivityExecutionStrategy = void 0; const processcube_engine_sdk_1 = require("@5minds/processcube_engine_sdk"); const ActivityExecutionStrategy_1 = require("./ActivityExecutionStrategy"); class ParallelActivityExecutionStrategy extends ActivityExecutionStrategy_1.ActivityExecutionStrategy { get flowNodeInstanceId() { return this.activityInstanceHandlers.map((instance) => instance.flowNodeInstanceId).join(';'); } async execute(startToken) { this.activityHandler.abortSignal.throwIfAborted(); const data = Array.isArray(startToken) ? startToken : [startToken]; const promises = []; for (const dataSet of data) { const instanceContext = { currentLoopIndex: promises.length, totalLoopElements: data.length, }; const newActivityInstanceHandler = this.createNewActivityInstanceHandler(this.activityHandler.previousFlowNodeInstanceId, instanceContext); this.activityInstanceHandlers.push(newActivityInstanceHandler); const promise = newActivityInstanceHandler.execute(dataSet); promises.push(promise); } return Promise.all(promises); } async resume(flowNodeInstancesToResume, startToken) { this.activityHandler.abortSignal.throwIfAborted(); const hasNoFlowNodeInstance = !flowNodeInstancesToResume || flowNodeInstancesToResume.length <= 0; if (hasNoFlowNodeInstance) { throw new processcube_engine_sdk_1.InternalServerError('No flowNodeInstances for resuming were supplied.', 'process'); } const promises = []; const persistedFlowNodeInstances = await this.flowNodeInstanceDatabaseAdapter.loadFlowNodeInstancesForMultiInstance(this.activityHandler.multiInstanceMetadataId); const persistedFlowNodeInstancesWhichAreAlreadyFinished = persistedFlowNodeInstances.filter((fni) => { return fni.state === processcube_engine_sdk_1.FlowNodeInstanceState.finished; }); const persistedFlowNodeInstancesForResuming = persistedFlowNodeInstances.filter((fni) => { return fni.state === processcube_engine_sdk_1.FlowNodeInstanceState.running || fni.state === processcube_engine_sdk_1.FlowNodeInstanceState.suspended; }); for (const fni of persistedFlowNodeInstancesWhichAreAlreadyFinished) { const newActivityInstanceHandler = this.createNewActivityInstanceHandler(fni.previousFlowNodeInstanceId); newActivityInstanceHandler.initFromFlowNodeInstance(fni); this.activityInstanceHandlers.push(newActivityInstanceHandler); } for (const flowNodeInstance of persistedFlowNodeInstancesForResuming) { const newActivityInstanceHandler = this.createNewActivityInstanceHandler(flowNodeInstance.previousFlowNodeInstanceId); this.activityInstanceHandlers.push(newActivityInstanceHandler); const promise = newActivityInstanceHandler.resume(flowNodeInstance); promises.push(promise); } const allStartedFlowNodeInstances = [...persistedFlowNodeInstancesWhichAreAlreadyFinished, ...persistedFlowNodeInstancesForResuming]; const data = Array.isArray(startToken) ? startToken : [startToken]; const deltaData = this.getDeltaOfAlreadyStartedAndToBeStartedFlowNodeInstances(allStartedFlowNodeInstances, data); for (const dataSet of deltaData) { const newActivityInstanceHandler = this.createNewActivityInstanceHandler(this.activityHandler.previousFlowNodeInstanceId); this.activityInstanceHandlers.push(newActivityInstanceHandler); const promise = newActivityInstanceHandler.execute(dataSet); promises.push(promise); } return Promise.all(promises); } getDeltaOfAlreadyStartedAndToBeStartedFlowNodeInstances(flowNodeInstancesToResume, data) { let deltaData = data.map((val) => { return { value: val, stringified: JSON.stringify(val) }; }); for (const fni of flowNodeInstancesToResume) { const stringifiedStartToken = JSON.stringify(fni.startToken); const indexOfUsedStartToken = deltaData.findIndex((set) => { set.stringified === stringifiedStartToken; }); deltaData.splice(indexOfUsedStartToken); } deltaData = deltaData.map((set) => set.value); return deltaData; } } exports.ParallelActivityExecutionStrategy = ParallelActivityExecutionStrategy; //# sourceMappingURL=ParallelActivityExecutionStrategy.js.map