bpmn-elements 16.1.0 → 16.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/dist/Api.js +1 -1
  2. package/dist/Context.js +2 -4
  3. package/dist/Environment.js +3 -3
  4. package/dist/EventBroker.js +1 -1
  5. package/dist/MessageFormatter.js +2 -2
  6. package/dist/Tracker.js +1 -0
  7. package/dist/activity/Activity.js +67 -163
  8. package/dist/activity/ActivityExecution.js +2 -2
  9. package/dist/activity/outbound-evaluator.js +131 -0
  10. package/dist/definition/Definition.js +4 -4
  11. package/dist/definition/DefinitionExecution.js +6 -6
  12. package/dist/error/Errors.js +7 -10
  13. package/dist/eventDefinitions/CancelEventDefinition.js +1 -2
  14. package/dist/eventDefinitions/CompensateEventDefinition.js +2 -3
  15. package/dist/eventDefinitions/ConditionalEventDefinition.js +2 -3
  16. package/dist/eventDefinitions/ErrorEventDefinition.js +3 -4
  17. package/dist/eventDefinitions/EscalationEventDefinition.js +1 -2
  18. package/dist/eventDefinitions/LinkEventDefinition.js +1 -2
  19. package/dist/eventDefinitions/MessageEventDefinition.js +2 -3
  20. package/dist/eventDefinitions/SignalEventDefinition.js +2 -3
  21. package/dist/eventDefinitions/TimerEventDefinition.js +2 -3
  22. package/dist/events/BoundaryEvent.js +2 -3
  23. package/dist/events/StartEvent.js +1 -2
  24. package/dist/flows/Association.js +2 -2
  25. package/dist/flows/SequenceFlow.js +3 -7
  26. package/dist/gateways/EventBasedGateway.js +7 -5
  27. package/dist/io/EnvironmentDataObject.js +1 -1
  28. package/dist/io/EnvironmentDataStore.js +1 -1
  29. package/dist/io/EnvironmentDataStoreReference.js +1 -1
  30. package/dist/io/InputOutputSpecification.js +2 -2
  31. package/dist/io/Properties.js +14 -14
  32. package/dist/process/Process.js +5 -7
  33. package/dist/process/ProcessExecution.js +13 -13
  34. package/dist/tasks/ServiceTask.js +1 -1
  35. package/package.json +2 -2
  36. package/src/Api.js +1 -1
  37. package/src/Context.js +2 -1
  38. package/src/Environment.js +3 -3
  39. package/src/EventBroker.js +1 -1
  40. package/src/MessageFormatter.js +2 -2
  41. package/src/Tracker.js +1 -0
  42. package/src/activity/Activity.js +65 -159
  43. package/src/activity/ActivityExecution.js +2 -2
  44. package/src/activity/outbound-evaluator.js +127 -0
  45. package/src/definition/Definition.js +4 -4
  46. package/src/definition/DefinitionExecution.js +6 -6
  47. package/src/error/Errors.js +7 -11
  48. package/src/eventDefinitions/CancelEventDefinition.js +1 -2
  49. package/src/eventDefinitions/CompensateEventDefinition.js +2 -3
  50. package/src/eventDefinitions/ConditionalEventDefinition.js +2 -3
  51. package/src/eventDefinitions/ErrorEventDefinition.js +3 -4
  52. package/src/eventDefinitions/EscalationEventDefinition.js +1 -2
  53. package/src/eventDefinitions/LinkEventDefinition.js +1 -2
  54. package/src/eventDefinitions/MessageEventDefinition.js +2 -3
  55. package/src/eventDefinitions/SignalEventDefinition.js +2 -3
  56. package/src/eventDefinitions/TimerEventDefinition.js +2 -3
  57. package/src/events/BoundaryEvent.js +2 -3
  58. package/src/events/StartEvent.js +1 -2
  59. package/src/flows/Association.js +2 -2
  60. package/src/flows/SequenceFlow.js +3 -3
  61. package/src/gateways/EventBasedGateway.js +5 -3
  62. package/src/io/EnvironmentDataObject.js +1 -1
  63. package/src/io/EnvironmentDataStore.js +1 -1
  64. package/src/io/EnvironmentDataStoreReference.js +1 -1
  65. package/src/io/InputOutputSpecification.js +2 -2
  66. package/src/io/Properties.js +14 -14
  67. package/src/process/Process.js +5 -7
  68. package/src/process/ProcessExecution.js +13 -13
  69. package/src/tasks/ServiceTask.js +1 -1
package/dist/Api.js CHANGED
@@ -72,7 +72,7 @@ Api.prototype.resolveExpression = function resolveExpression(expression) {
72
72
  }, this.owner);
73
73
  };
74
74
  Api.prototype.sendApiMessage = function sendApiMessage(action, content, options) {
75
- const correlationId = options && options.correlationId || (0, _shared.getUniqueId)(`${this.id || this.messagePrefix}_signal`);
75
+ const correlationId = options?.correlationId || (0, _shared.getUniqueId)(`${this.id || this.messagePrefix}_signal`);
76
76
  let key = `${this.messagePrefix}.${action}`;
77
77
  if (this.executionId) key += `.${this.executionId}`;
78
78
  this.broker.publish('api', key, this.createMessage(content), {
package/dist/Context.js CHANGED
@@ -165,10 +165,8 @@ ContextInstance.prototype.getDataStoreById = function getDataStoreById(reference
165
165
  return dataStore;
166
166
  };
167
167
  ContextInstance.prototype.getStartActivities = function getStartActivities(filterOptions, scopeId) {
168
- const {
169
- referenceId,
170
- referenceType = 'unknown'
171
- } = filterOptions || {};
168
+ const referenceId = filterOptions?.referenceId;
169
+ const referenceType = filterOptions?.referenceType || 'unknown';
172
170
  const result = [];
173
171
  for (const activity of this.getActivities()) {
174
172
  if (!activity.isStart) continue;
@@ -64,7 +64,7 @@ Environment.prototype.recover = function recover(state) {
64
64
  if (state.output) Object.assign(this.output, state.output);
65
65
  return this;
66
66
  };
67
- Environment.prototype.clone = function clone(overrideOptions = {}) {
67
+ Environment.prototype.clone = function clone(overrideOptions) {
68
68
  const services = this[kServices];
69
69
  const newOptions = {
70
70
  settings: {
@@ -82,7 +82,7 @@ Environment.prototype.clone = function clone(overrideOptions = {}) {
82
82
  ...overrideOptions,
83
83
  services
84
84
  };
85
- if (overrideOptions.services) newOptions.services = {
85
+ if (overrideOptions?.services) newOptions.services = {
86
86
  ...services,
87
87
  ...overrideOptions.services
88
88
  };
@@ -111,7 +111,7 @@ Environment.prototype.registerScript = function registerScript(...args) {
111
111
  Environment.prototype.getServiceByName = function getServiceByName(serviceName) {
112
112
  return this[kServices][serviceName];
113
113
  };
114
- Environment.prototype.resolveExpression = function resolveExpression(expression, message = {}, expressionFnContext) {
114
+ Environment.prototype.resolveExpression = function resolveExpression(expression, message, expressionFnContext) {
115
115
  const from = {
116
116
  environment: this,
117
117
  ...message
@@ -111,7 +111,7 @@ EventBroker.prototype.on = function on(eventName, callback, eventOptions = {
111
111
  callback(owner.getApi(message));
112
112
  }
113
113
  };
114
- EventBroker.prototype.once = function once(eventName, callback, eventOptions = {}) {
114
+ EventBroker.prototype.once = function once(eventName, callback, eventOptions) {
115
115
  return this.on(eventName, callback, {
116
116
  ...eventOptions,
117
117
  once: true
@@ -66,7 +66,7 @@ Formatter.prototype._onMessage = function onMessage(routingKey, message) {
66
66
  default:
67
67
  {
68
68
  message.ack();
69
- const endRoutingKey = message.content && message.content.endRoutingKey;
69
+ const endRoutingKey = message.content?.endRoutingKey;
70
70
  if (endRoutingKey) {
71
71
  this._decorate(message.content);
72
72
  pending.push(message);
@@ -102,7 +102,7 @@ Formatter.prototype._complete = function complete(message, isError) {
102
102
  if (executeMessage) executeMessage.ack();
103
103
  this.broker.cancel(message.fields.consumerTag);
104
104
  if (isError) {
105
- const error = message.content && message.content.error || new Error('formatting failed');
105
+ const error = message.content?.error || new Error('formatting failed');
106
106
  const errMessage = error.message || 'formatting failed';
107
107
  this._debug(`formatting of ${formatKey} failed with ${message.fields.routingKey}: ${errMessage}`);
108
108
  return callback(new _Errors.ActivityError(errMessage, (0, _messageHelper.cloneMessage)(runMessage), error));
package/dist/Tracker.js CHANGED
@@ -37,6 +37,7 @@ ActivityTracker.prototype.track = function track(routingKey, message) {
37
37
  break;
38
38
  case 'activity.execution.outbound.take':
39
39
  case 'activity.detach':
40
+ case 'activity.call':
40
41
  case 'activity.wait':
41
42
  {
42
43
  if (content.isMultiInstance) this._waiting(content.parent.executionId);else this._waiting(executionId);
@@ -11,6 +11,7 @@ var _EventBroker = require("../EventBroker.js");
11
11
  var _MessageFormatter = require("../MessageFormatter.js");
12
12
  var _messageHelper = require("../messageHelper.js");
13
13
  var _Errors = require("../error/Errors.js");
14
+ var _outboundEvaluator = require("./outbound-evaluator.js");
14
15
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
15
16
  const kActivityDef = Symbol.for('activityDefinition');
16
17
  const kConsuming = Symbol.for('consuming');
@@ -84,15 +85,20 @@ function Activity(Behaviour, activityDef, context) {
84
85
  inboundTriggers = inboundSequenceFlows.slice();
85
86
  }
86
87
  const outboundSequenceFlows = context.getOutboundSequenceFlows(id);
88
+ const isParallelJoin = activityDef.isParallelGateway && inboundSequenceFlows.length > 1;
87
89
  const flows = this[kFlows] = {
88
90
  inboundSequenceFlows,
89
91
  inboundAssociations,
90
- inboundJoinFlows: new Set(),
91
92
  inboundTriggers,
92
93
  outboundSequenceFlows,
93
- outboundEvaluator: new OutboundEvaluator(this, outboundSequenceFlows)
94
+ outboundEvaluator: new _outboundEvaluator.OutboundEvaluator(this, outboundSequenceFlows),
95
+ ...(isParallelJoin && {
96
+ inboundJoinFlows: new Set(),
97
+ inboundSourceIds: new Set(inboundSequenceFlows.map(({
98
+ sourceId
99
+ }) => sourceId))
100
+ })
94
101
  };
95
- const isParallelJoin = activityDef.isParallelGateway && flows.inboundSequenceFlows.length > 1;
96
102
  this[kFlags] = {
97
103
  isEnd: flows.outboundSequenceFlows.length === 0,
98
104
  isStart: flows.inboundSequenceFlows.length === 0 && !attachedTo && !behaviour.triggeredByEvent && !isForCompensation,
@@ -103,7 +109,7 @@ function Activity(Behaviour, activityDef, context) {
103
109
  isTransaction: activityDef.isTransaction,
104
110
  isParallelJoin,
105
111
  isThrowing: activityDef.isThrowing,
106
- lane: activityDef.lane && activityDef.lane.id
112
+ lane: activityDef.lane?.id
107
113
  };
108
114
  this[kExec] = new Map();
109
115
  this[kMessageHandlers] = {
@@ -112,7 +118,7 @@ function Activity(Behaviour, activityDef, context) {
112
118
  onApiMessage: this._onApiMessage.bind(this),
113
119
  onExecutionMessage: this._onExecutionMessage.bind(this)
114
120
  };
115
- this[kEventDefinitions] = eventDefinitions && eventDefinitions.map((ed, idx) => new ed.Behaviour(this, ed, context, idx));
121
+ this[kEventDefinitions] = eventDefinitions?.map((ed, idx) => new ed.Behaviour(this, ed, context, idx));
116
122
  this[kExtensions] = context.loadExtensions(this);
117
123
  this[kConsuming] = false;
118
124
  this[kConsumingRunQ] = undefined;
@@ -143,7 +149,7 @@ Object.defineProperties(Activity.prototype, {
143
149
  bpmnIo: {
144
150
  get() {
145
151
  const extensions = this[kExtensions];
146
- return extensions && extensions.extensions.find(e => e.type === 'bpmnio');
152
+ return extensions?.extensions.find(e => e.type === 'bpmnio');
147
153
  }
148
154
  },
149
155
  formatter: {
@@ -508,21 +514,21 @@ Activity.prototype._onJoinInbound = function onJoinInbound(routingKey, message)
508
514
  } = message;
509
515
  const {
510
516
  inboundJoinFlows,
511
- inboundTriggers
517
+ inboundSourceIds
512
518
  } = this[kFlows];
513
519
  let alreadyTouched = false;
514
520
  const touched = new Set();
515
521
  let taken;
516
522
  for (const msg of inboundJoinFlows) {
517
- const flowId = msg.content.id;
518
- touched.add(flowId);
519
- if (flowId === content.id) {
523
+ const sourceId = msg.content.sourceId;
524
+ touched.add(sourceId);
525
+ if (sourceId === content.sourceId) {
520
526
  alreadyTouched = true;
521
527
  }
522
528
  }
523
529
  inboundJoinFlows.add(message);
524
530
  if (alreadyTouched) return;
525
- const remaining = inboundTriggers.length - touched.size - 1;
531
+ const remaining = inboundSourceIds.size - touched.size - 1;
526
532
  if (remaining) {
527
533
  return this.logger.debug(`<${this.id}> inbound ${message.content.action} from <${message.content.id}>, ${remaining} remaining`);
528
534
  }
@@ -660,7 +666,7 @@ Activity.prototype._continueRunMessage = function continueRunMessage(routingKey,
660
666
  }
661
667
  case 'run.execute.passthrough':
662
668
  {
663
- const execution = this.execution;
669
+ const execution = this[kExec].get('execution');
664
670
  if (!isRedelivered && execution) {
665
671
  if (execution.completed) return message.ack();
666
672
  this[kExecuteMessage] = message;
@@ -671,14 +677,18 @@ Activity.prototype._continueRunMessage = function continueRunMessage(routingKey,
671
677
  {
672
678
  this.status = 'executing';
673
679
  this[kExecuteMessage] = message;
674
- const exec = this[kExec];
675
680
  if (isRedelivered && this.extensions) this.extensions.activate((0, _messageHelper.cloneMessage)(message));
676
- if (!exec.has('execution')) exec.set('execution', new _ActivityExecution.default(this, this.context));
681
+ const exec = this[kExec];
682
+ let execution = exec.get('execution');
683
+ if (!execution) {
684
+ execution = new _ActivityExecution.default(this, this.context);
685
+ exec.set('execution', execution);
686
+ }
677
687
  this.broker.getQueue('execution-q').assertConsumer(this[kMessageHandlers].onExecutionMessage, {
678
688
  exclusive: true,
679
689
  consumerTag: '_activity-execution'
680
690
  });
681
- return exec.get('execution').execute(message);
691
+ return execution.execute(message);
682
692
  }
683
693
  case 'run.end':
684
694
  {
@@ -846,16 +856,16 @@ Activity.prototype._doOutbound = function doOutbound(fromMessage, isDiscarded, c
846
856
  if (!outboundSequenceFlows.length) return callback(null, []);
847
857
  const fromContent = fromMessage.content;
848
858
  let discardSequence = fromContent.discardSequence;
849
- if (isDiscarded && !discardSequence && this[kFlags].attachedTo && fromContent.inbound && fromContent.inbound[0]) {
859
+ if (isDiscarded && !discardSequence && this[kFlags].attachedTo && fromContent.inbound?.[0]) {
850
860
  discardSequence = [fromContent.inbound[0].id];
851
861
  }
852
862
  let outboundFlows;
853
863
  if (isDiscarded) {
854
- outboundFlows = outboundSequenceFlows.map(flow => formatFlowAction(flow, {
864
+ outboundFlows = outboundSequenceFlows.map(flow => (0, _outboundEvaluator.formatFlowAction)(flow, {
855
865
  action: 'discard'
856
866
  }));
857
- } else if (fromContent.outbound && fromContent.outbound.length) {
858
- outboundFlows = outboundSequenceFlows.map(flow => formatFlowAction(flow, fromContent.outbound.filter(f => f.id === flow.id).pop()));
867
+ } else if (fromContent.outbound?.length) {
868
+ outboundFlows = outboundSequenceFlows.map(flow => (0, _outboundEvaluator.formatFlowAction)(flow, fromContent.outbound.filter(f => f.id === flow.id).pop()));
859
869
  }
860
870
  if (outboundFlows) {
861
871
  this._doRunOutbound(outboundFlows, fromContent, discardSequence);
@@ -868,25 +878,41 @@ Activity.prototype._doOutbound = function doOutbound(fromMessage, isDiscarded, c
868
878
  });
869
879
  };
870
880
  Activity.prototype._doRunOutbound = function doRunOutbound(outboundList, content, discardSequence) {
871
- for (const outboundFlow of outboundList) {
872
- const {
873
- id: flowId,
874
- action,
875
- result
876
- } = outboundFlow;
877
- this.broker.publish('run', 'run.outbound.' + action, (0, _messageHelper.cloneContent)(content, {
878
- flow: {
879
- ...(result && typeof result === 'object' && result),
880
- ...outboundFlow,
881
- sequenceId: (0, _shared.getUniqueId)(`${flowId}_${action}`),
882
- ...(discardSequence && {
883
- discardSequence: discardSequence.slice()
884
- })
881
+ if (outboundList.length === 1) {
882
+ this._publishRunOutbound(outboundList[0], content, discardSequence);
883
+ } else {
884
+ const targets = new Map();
885
+ for (const outboundFlow of outboundList) {
886
+ const prevTarget = targets.get(outboundFlow.targetId);
887
+ if (!prevTarget) {
888
+ targets.set(outboundFlow.targetId, outboundFlow);
889
+ } else if (outboundFlow.action === 'take' && outboundFlow.action !== prevTarget.action) {
890
+ targets.set(outboundFlow.targetId, outboundFlow);
885
891
  }
886
- }));
892
+ }
893
+ for (const outboundFlow of targets.values()) {
894
+ this._publishRunOutbound(outboundFlow, content, discardSequence);
895
+ }
887
896
  }
888
897
  return outboundList;
889
898
  };
899
+ Activity.prototype._publishRunOutbound = function publishRunOutbound(outboundFlow, content, discardSequence) {
900
+ const {
901
+ id: flowId,
902
+ action,
903
+ result
904
+ } = outboundFlow;
905
+ this.broker.publish('run', 'run.outbound.' + action, (0, _messageHelper.cloneContent)(content, {
906
+ flow: {
907
+ ...(result && typeof result === 'object' && result),
908
+ ...outboundFlow,
909
+ sequenceId: (0, _shared.getUniqueId)(`${flowId}_${action}`),
910
+ ...(discardSequence && {
911
+ discardSequence: discardSequence.slice()
912
+ })
913
+ }
914
+ }));
915
+ };
890
916
  Activity.prototype._onResumeMessage = function onResumeMessage(message) {
891
917
  message.ack();
892
918
  const stateMessage = this[kStateMessage];
@@ -906,7 +932,7 @@ Activity.prototype._onResumeMessage = function onResumeMessage(message) {
906
932
  this.logger.debug(`<${this.id}> resume from ${message.content.status}`);
907
933
  return this.broker.publish('run', fields.routingKey, (0, _messageHelper.cloneContent)(stateMessage.content), stateMessage.properties);
908
934
  };
909
- Activity.prototype._publishEvent = function publishEvent(state, content, properties = {}) {
935
+ Activity.prototype._publishEvent = function publishEvent(state, content, properties) {
910
936
  this.broker.publish('event', `activity.${state}`, (0, _messageHelper.cloneContent)(content, {
911
937
  state
912
938
  }), {
@@ -960,9 +986,11 @@ Activity.prototype._onApiMessage = function onApiMessage(routingKey, message) {
960
986
  }
961
987
  };
962
988
  Activity.prototype._createMessage = function createMessage(override) {
963
- const name = this.name,
964
- status = this.status,
965
- parent = this.parent;
989
+ const {
990
+ name,
991
+ status,
992
+ parent
993
+ } = this;
966
994
  const result = {
967
995
  ...override,
968
996
  id: this.id,
@@ -991,128 +1019,4 @@ Activity.prototype._deactivateRunConsumers = function _deactivateRunConsumers()
991
1019
  this._pauseRunQ();
992
1020
  broker.cancel('_activity-execution');
993
1021
  this[kConsuming] = false;
994
- };
995
- function OutboundEvaluator(activity, outboundFlows) {
996
- this.activity = activity;
997
- this.broker = activity.broker;
998
- const flows = this.outboundFlows = outboundFlows.slice();
999
- const defaultFlowIdx = flows.findIndex(({
1000
- isDefault
1001
- }) => isDefault);
1002
- if (defaultFlowIdx > -1) {
1003
- const [defaultFlow] = flows.splice(defaultFlowIdx, 1);
1004
- flows.push(defaultFlow);
1005
- }
1006
- this.defaultFlowIdx = outboundFlows.findIndex(({
1007
- isDefault
1008
- }) => isDefault);
1009
- this._onEvaluated = this.onEvaluated.bind(this);
1010
- this.evaluateArgs = {};
1011
- }
1012
- OutboundEvaluator.prototype.evaluate = function evaluate(fromMessage, discardRestAtTake, callback) {
1013
- const outboundFlows = this.outboundFlows;
1014
- const args = this.evaluateArgs = {
1015
- fromMessage,
1016
- evaluationId: fromMessage.content.executionId,
1017
- discardRestAtTake,
1018
- callback,
1019
- conditionMet: false,
1020
- result: {},
1021
- takenCount: 0
1022
- };
1023
- if (!outboundFlows.length) return this.completed();
1024
- const flows = args.flows = outboundFlows.slice();
1025
- this.broker.subscribeTmp('execution', 'evaluate.flow.#', this._onEvaluated, {
1026
- consumerTag: `_flow-evaluation-${args.evaluationId}`
1027
- });
1028
- return this.evaluateFlow(flows.shift());
1029
- };
1030
- OutboundEvaluator.prototype.onEvaluated = function onEvaluated(routingKey, message) {
1031
- const content = message.content;
1032
- const {
1033
- id: flowId,
1034
- action,
1035
- evaluationId
1036
- } = message.content;
1037
- const args = this.evaluateArgs;
1038
- if (action === 'take') {
1039
- args.takenCount++;
1040
- args.conditionMet = true;
1041
- }
1042
- args.result[flowId] = content;
1043
- if ('result' in content) {
1044
- this.activity.logger.debug(`<${evaluationId} (${this.activity.id})> flow <${flowId}> evaluated to: ${!!content.result}`);
1045
- }
1046
- let nextFlow = args.flows.shift();
1047
- if (!nextFlow) return this.completed();
1048
- if (args.discardRestAtTake && args.conditionMet) {
1049
- do {
1050
- args.result[nextFlow.id] = formatFlowAction(nextFlow, {
1051
- action: 'discard'
1052
- });
1053
- } while (nextFlow = args.flows.shift());
1054
- return this.completed();
1055
- }
1056
- if (args.conditionMet && nextFlow.isDefault) {
1057
- args.result[nextFlow.id] = formatFlowAction(nextFlow, {
1058
- action: 'discard'
1059
- });
1060
- return this.completed();
1061
- }
1062
- message.ack();
1063
- this.evaluateFlow(nextFlow);
1064
- };
1065
- OutboundEvaluator.prototype.evaluateFlow = function evaluateFlow(flow) {
1066
- const broker = this.broker;
1067
- const {
1068
- fromMessage,
1069
- evaluationId
1070
- } = this.evaluateArgs;
1071
- flow.evaluate((0, _messageHelper.cloneMessage)(fromMessage), (err, result) => {
1072
- if (err) return this.completed(err);
1073
- const action = result ? 'take' : 'discard';
1074
- return broker.publish('execution', 'evaluate.flow.' + action, formatFlowAction(flow, {
1075
- action,
1076
- result,
1077
- evaluationId
1078
- }), {
1079
- persistent: false
1080
- });
1081
- });
1082
- };
1083
- OutboundEvaluator.prototype.completed = function completed(err) {
1084
- const {
1085
- callback,
1086
- evaluationId,
1087
- fromMessage,
1088
- result,
1089
- takenCount
1090
- } = this.evaluateArgs;
1091
- this.broker.cancel(`_flow-evaluation-${evaluationId}`);
1092
- if (err) return callback(err);
1093
- if (!takenCount && this.outboundFlows.length) {
1094
- const nonTakenError = new _Errors.ActivityError(`<${this.activity.id}> no conditional flow taken`, fromMessage);
1095
- return callback(nonTakenError);
1096
- }
1097
- const message = fromMessage.content.message;
1098
- const evaluationResult = [];
1099
- for (const flow of Object.values(result)) {
1100
- evaluationResult.push({
1101
- ...flow,
1102
- ...(message !== undefined && {
1103
- message
1104
- })
1105
- });
1106
- }
1107
- return callback(null, evaluationResult);
1108
- };
1109
- function formatFlowAction(flow, options) {
1110
- return {
1111
- ...options,
1112
- id: flow.id,
1113
- action: options.action,
1114
- ...(flow.isDefault && {
1115
- isDefault: true
1116
- })
1117
- };
1118
- }
1022
+ };
@@ -35,7 +35,7 @@ Object.defineProperty(ActivityExecution.prototype, 'completed', {
35
35
  });
36
36
  ActivityExecution.prototype.execute = function execute(executeMessage) {
37
37
  if (!executeMessage) throw new Error('Execution requires message');
38
- const executionId = executeMessage.content && executeMessage.content.executionId;
38
+ const executionId = executeMessage.content?.executionId;
39
39
  if (!executionId) throw new Error('Execution requires execution id');
40
40
  this.executionId = executionId;
41
41
  const initMessage = this[kExecuteMessage] = (0, _messageHelper.cloneMessage)(executeMessage, {
@@ -338,7 +338,7 @@ ActivityExecution.prototype._onParentApiMessage = function onParentApiMessage(ro
338
338
  }
339
339
  };
340
340
  ActivityExecution.prototype._onStop = function onStop(message) {
341
- const stoppedId = message && message.content && message.content.executionId;
341
+ const stoppedId = message?.content?.executionId;
342
342
  const running = this.getPostponed();
343
343
  for (const api of running) {
344
344
  if (stoppedId !== api.content.executionId) {
@@ -0,0 +1,131 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.OutboundEvaluator = OutboundEvaluator;
7
+ exports.formatFlowAction = formatFlowAction;
8
+ var _Errors = require("../error/Errors.js");
9
+ var _messageHelper = require("../messageHelper.js");
10
+ function OutboundEvaluator(activity, outboundFlows) {
11
+ this.activity = activity;
12
+ this.broker = activity.broker;
13
+ const flows = this.outboundFlows = outboundFlows.slice();
14
+ const defaultFlowIdx = flows.findIndex(({
15
+ isDefault
16
+ }) => isDefault);
17
+ if (defaultFlowIdx > -1) {
18
+ const [defaultFlow] = flows.splice(defaultFlowIdx, 1);
19
+ flows.push(defaultFlow);
20
+ }
21
+ this._onEvaluated = this.onEvaluated.bind(this);
22
+ this.evaluateArgs = {};
23
+ }
24
+ OutboundEvaluator.prototype.evaluate = function evaluate(fromMessage, discardRestAtTake, callback) {
25
+ const outboundFlows = this.outboundFlows;
26
+ const args = this.evaluateArgs = {
27
+ fromMessage,
28
+ evaluationId: fromMessage.content.executionId,
29
+ discardRestAtTake,
30
+ callback,
31
+ conditionMet: false,
32
+ result: {},
33
+ takenCount: 0
34
+ };
35
+ if (!outboundFlows.length) return this.completed();
36
+ const flows = args.flows = outboundFlows.slice();
37
+ this.broker.subscribeTmp('execution', 'evaluate.flow.#', this._onEvaluated, {
38
+ consumerTag: `_flow-evaluation-${args.evaluationId}`
39
+ });
40
+ return this.evaluateFlow(flows.shift());
41
+ };
42
+ OutboundEvaluator.prototype.onEvaluated = function onEvaluated(routingKey, message) {
43
+ const content = message.content;
44
+ const {
45
+ id: flowId,
46
+ action,
47
+ evaluationId
48
+ } = message.content;
49
+ const args = this.evaluateArgs;
50
+ if (action === 'take') {
51
+ args.takenCount++;
52
+ args.conditionMet = true;
53
+ }
54
+ args.result[flowId] = content;
55
+ if ('result' in content) {
56
+ this.activity.logger.debug(`<${evaluationId} (${this.activity.id})> flow <${flowId}> evaluated to: ${!!content.result}`);
57
+ }
58
+ let nextFlow = args.flows.shift();
59
+ if (!nextFlow) return this.completed();
60
+ if (args.discardRestAtTake && args.conditionMet) {
61
+ do {
62
+ args.result[nextFlow.id] = formatFlowAction(nextFlow, {
63
+ action: 'discard'
64
+ });
65
+ } while (nextFlow = args.flows.shift());
66
+ return this.completed();
67
+ }
68
+ if (args.conditionMet && nextFlow.isDefault) {
69
+ args.result[nextFlow.id] = formatFlowAction(nextFlow, {
70
+ action: 'discard'
71
+ });
72
+ return this.completed();
73
+ }
74
+ message.ack();
75
+ this.evaluateFlow(nextFlow);
76
+ };
77
+ OutboundEvaluator.prototype.evaluateFlow = function evaluateFlow(flow) {
78
+ const broker = this.broker;
79
+ const {
80
+ fromMessage,
81
+ evaluationId
82
+ } = this.evaluateArgs;
83
+ flow.evaluate((0, _messageHelper.cloneMessage)(fromMessage), (err, result) => {
84
+ if (err) return this.completed(err);
85
+ const action = result ? 'take' : 'discard';
86
+ return broker.publish('execution', 'evaluate.flow.' + action, formatFlowAction(flow, {
87
+ action,
88
+ result,
89
+ evaluationId
90
+ }), {
91
+ persistent: false
92
+ });
93
+ });
94
+ };
95
+ OutboundEvaluator.prototype.completed = function completed(err) {
96
+ const {
97
+ callback,
98
+ evaluationId,
99
+ fromMessage,
100
+ result,
101
+ takenCount
102
+ } = this.evaluateArgs;
103
+ this.broker.cancel(`_flow-evaluation-${evaluationId}`);
104
+ if (err) return callback(err);
105
+ if (!takenCount && this.outboundFlows.length) {
106
+ const nonTakenError = new _Errors.ActivityError(`<${this.activity.id}> no conditional flow taken`, fromMessage);
107
+ return callback(nonTakenError);
108
+ }
109
+ const message = fromMessage.content.message;
110
+ const evaluationResult = [];
111
+ for (const flow of Object.values(result)) {
112
+ evaluationResult.push({
113
+ ...flow,
114
+ ...(message !== undefined && {
115
+ message
116
+ })
117
+ });
118
+ }
119
+ return callback(null, evaluationResult);
120
+ };
121
+ function formatFlowAction(flow, options) {
122
+ return {
123
+ ...options,
124
+ id: flow.id,
125
+ action: options.action,
126
+ targetId: flow.targetId,
127
+ ...(flow.isDefault && {
128
+ isDefault: true
129
+ })
130
+ };
131
+ }
@@ -106,7 +106,7 @@ Object.defineProperties(Definition.prototype, {
106
106
  activityStatus: {
107
107
  get() {
108
108
  const execution = this[kExec].get('execution');
109
- return execution && execution.activityStatus || 'idle';
109
+ return execution?.activityStatus || 'idle';
110
110
  }
111
111
  }
112
112
  });
@@ -159,7 +159,7 @@ Definition.prototype.getState = function getState() {
159
159
  stopped: this.stopped,
160
160
  counters: this.counters,
161
161
  environment: this.environment.getState(),
162
- execution: this.execution && this.execution.getState(),
162
+ execution: this.execution?.getState(),
163
163
  broker: this.broker.getState(true)
164
164
  });
165
165
  };
@@ -272,8 +272,8 @@ Definition.prototype.sendMessage = function sendMessage(message) {
272
272
  message
273
273
  };
274
274
  let messageType = 'message';
275
- const reference = message && message.id && this.getElementById(message.id);
276
- if (reference && reference.resolve) {
275
+ const reference = message?.id && this.getElementById(message.id);
276
+ if (reference?.resolve) {
277
277
  const resolvedReference = reference.resolve(this._createMessage({
278
278
  message
279
279
  }));
@@ -390,7 +390,7 @@ DefinitionExecution.prototype._onProcessMessage = function onProcessMessage(rout
390
390
  break;
391
391
  case 'process.discarded':
392
392
  {
393
- if (inbound && inbound.length) {
393
+ if (inbound?.length) {
394
394
  const calledFrom = inbound[0];
395
395
  this._getProcessApi({
396
396
  content: calledFrom
@@ -402,7 +402,7 @@ DefinitionExecution.prototype._onProcessMessage = function onProcessMessage(rout
402
402
  }
403
403
  case 'process.end':
404
404
  {
405
- if (inbound && inbound.length) {
405
+ if (inbound?.length) {
406
406
  const calledFrom = inbound[0];
407
407
  this._getProcessApi({
408
408
  content: calledFrom
@@ -419,7 +419,7 @@ DefinitionExecution.prototype._onProcessMessage = function onProcessMessage(rout
419
419
  }
420
420
  case 'process.error':
421
421
  {
422
- if (inbound && inbound.length) {
422
+ if (inbound?.length) {
423
423
  const calledFrom = inbound[0];
424
424
  this._getProcessApi({
425
425
  content: calledFrom
@@ -467,7 +467,7 @@ DefinitionExecution.prototype._onProcessCompleted = function onProcessCompleted(
467
467
  } = message.content;
468
468
  message.ack();
469
469
  this._debug(`left <${executionId} (${id})> (${type}), pending runs ${this.postponedCount}`);
470
- if (inbound && inbound.length) {
470
+ if (inbound?.length) {
471
471
  const bp = this._removeProcessByExecutionId(executionId);
472
472
  this._deactivateProcess(bp);
473
473
  }
@@ -627,7 +627,7 @@ DefinitionExecution.prototype._onDelegateMessage = function onDelegateMessage(ro
627
627
  const messageType = executeMessage.properties.type;
628
628
  const delegateMessage = executeMessage.content.message;
629
629
  const reference = this.context.getActivityById(delegateMessage.id);
630
- const message = reference && reference.resolve(executeMessage);
630
+ const message = reference?.resolve(executeMessage);
631
631
  this._debug(`<${reference ? `${messageType} ${delegateMessage.id}>` : `anonymous ${messageType}`} event received from <${content.parent.id}.${content.id}>. Delegating.`);
632
632
  this.getApi().sendApiMessage(messageType, {
633
633
  source: {
@@ -674,7 +674,7 @@ DefinitionExecution.prototype._complete = function complete(completionType, cont
674
674
  ...options
675
675
  });
676
676
  };
677
- DefinitionExecution.prototype._createMessage = function createMessage(content = {}) {
677
+ DefinitionExecution.prototype._createMessage = function createMessage(content) {
678
678
  return {
679
679
  id: this.id,
680
680
  type: this.type,