From 59c85115c333676dd17ea7a9840f622ded6feecb Mon Sep 17 00:00:00 2001 From: Nico Rehwaldt Date: Thu, 29 Aug 2024 14:44:34 +0200 Subject: [PATCH] chore: refactor behavior --- .../behaviors/ParallelGatewayBehavior.js | 49 +++++++++++++------ 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/lib/simulator/behaviors/ParallelGatewayBehavior.js b/lib/simulator/behaviors/ParallelGatewayBehavior.js index 6253584..3398855 100644 --- a/lib/simulator/behaviors/ParallelGatewayBehavior.js +++ b/lib/simulator/behaviors/ParallelGatewayBehavior.js @@ -15,10 +15,39 @@ export default function ParallelGatewayBehavior( ParallelGatewayBehavior.prototype.enter = function(context) { + const { + scope + } = context; + + const joiningScopes = this._findJoiningScopes(context); + + if (joiningScopes.length) { + + for (const childScope of joiningScopes) { + + if (childScope !== scope) { + + // complete joining child scope + this._simulator.destroyScope(childScope.complete(), scope); + } + } + + this._simulator.exit(context); + } +}; + +/** + * Find scopes that will be joined by this transition. + * + * @param {Object} enterContext + * @return {Scope[]} scopes joined by this transition + */ +ParallelGatewayBehavior.prototype._findJoiningScopes = function(enterContext) { + const { scope, element - } = context; + } = enterContext; const sequenceFlows = filterSequenceFlows(element.incoming); @@ -31,25 +60,17 @@ ParallelGatewayBehavior.prototype.enter = function(context) { element: element }); - const finishedScopes = sequenceFlows + const matchingScopes = sequenceFlows .map( flow => elementScopes .find(scope => scope.initiator.element === flow) ) .filter(scope => scope); - if (finishedScopes.length === sequenceFlows.length) { - - for (const childScope of finishedScopes) { - - if (childScope !== scope) { - - // complete joining child scope - this._simulator.destroyScope(childScope.complete(), scope); - } - } - - this._simulator.exit(context); + if (matchingScopes.length === sequenceFlows.length) { + return matchingScopes; + } else { + return []; } };