@5minds/processcube_engine
Version:
The ProcessCube Engine. Stores and executes BPMNs.
77 lines • 3.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ActivityExecutionStrategy = void 0;
const processcube_engine_sdk_1 = require("@5minds/processcube_engine_sdk");
const ActivityInstanceHandlerFactory_1 = require("../ActivityInstanceHandlerFactory");
class ActivityExecutionStrategy {
flowNodeInstanceDatabaseAdapter;
activityHandler;
processInstance;
activityInstanceHandlers = [];
_timeoutBetweenLoopIterations;
_maxLoopIterations;
constructor(flowNodeInstanceDatabaseAdapter, activityHander, processInstance) {
this.flowNodeInstanceDatabaseAdapter = flowNodeInstanceDatabaseAdapter;
this.activityHandler = activityHander;
this.processInstance = processInstance;
}
set timeoutBetweenLoopIterations(timeout) {
this._timeoutBetweenLoopIterations = timeout;
}
set maxLoopIterations(timeout) {
this._maxLoopIterations = timeout;
}
get flowNodeInstanceId() {
return this.activityInstanceHandlers.length > 0 ? this.activityInstanceHandlers[this.activityInstanceHandlers.length - 1].flowNodeInstanceId : undefined;
}
get activeFlowNodeInstances() {
return this.activityInstanceHandlers.filter((fni) => fni.state === processcube_engine_sdk_1.FlowNodeInstanceState.running || fni.state === processcube_engine_sdk_1.FlowNodeInstanceState.suspended);
}
async endWithTermination() {
return this.runOnAllFlowNodeInstances((fni) => fni.terminate());
}
async endWithError(error, force = false) {
return this.runOnAllFlowNodeInstances((fni) => fni.endWithError(error, force));
}
async endWithPreError(error, force = false) {
if (this.activityInstanceHandlers.length === 0) {
const newActivityInstanceHandler = this.createNewActivityInstanceHandler(this.activityHandler.previousFlowNodeInstanceId);
this.activityInstanceHandlers.push(newActivityInstanceHandler);
await newActivityInstanceHandler.persistOnEnter();
}
await this.endWithError(error, force);
}
async endWithPostError(error, force = false) {
if (this.activityInstanceHandlers.length === 0) {
return;
}
await this.endWithError(error, force);
}
async terminate() {
return this.runOnActiveFlowNodeInstances((fni) => fni.terminate());
}
async cancel() {
return this.runOnActiveFlowNodeInstances((fni) => fni.cancel());
}
createNewActivityInstanceHandler(previousFlowNodeInstanceId, instanceContext) {
this.activityHandler.abortSignal.throwIfAborted();
const flowNodeInstanceHandler = ActivityInstanceHandlerFactory_1.ActivityInstanceHandlerFactory.create(this.processInstance, this.activityHandler, previousFlowNodeInstanceId, instanceContext);
return flowNodeInstanceHandler;
}
async runOnAllFlowNodeInstances(executor) {
await Promise.all(this.activityInstanceHandlers.map(executor));
}
async runOnActiveFlowNodeInstances(executor) {
await Promise.all(this.activeFlowNodeInstances.map(executor));
}
async runOnLastFlowNodeInstance(executor) {
const hasFlowNodeInstance = this.activityInstanceHandlers.length > 0;
if (!hasFlowNodeInstance) {
return;
}
const lastFlowNodeInstance = this.activityInstanceHandlers[this.activityInstanceHandlers.length - 1];
await executor(lastFlowNodeInstance);
}
}
exports.ActivityExecutionStrategy = ActivityExecutionStrategy;
//# sourceMappingURL=ActivityExecutionStrategy.js.map