bpmn-elements 6.0.1 → 8.0.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 (116) hide show
  1. package/CHANGELOG.md +341 -0
  2. package/README.md +3 -0
  3. package/dist/index.js +52 -44
  4. package/dist/src/Api.js +77 -76
  5. package/dist/src/Context.js +176 -172
  6. package/dist/src/Environment.js +110 -102
  7. package/dist/src/EventBroker.js +89 -88
  8. package/dist/src/ExtensionsMapper.js +2 -2
  9. package/dist/src/MessageFormatter.js +164 -95
  10. package/dist/src/Scripts.js +6 -2
  11. package/dist/src/activity/Activity.js +1105 -916
  12. package/dist/src/activity/ActivityExecution.js +342 -297
  13. package/dist/src/activity/Dummy.js +3 -3
  14. package/dist/src/definition/Definition.js +498 -444
  15. package/dist/src/definition/DefinitionExecution.js +710 -408
  16. package/dist/src/error/Errors.js +17 -7
  17. package/dist/src/eventDefinitions/CancelEventDefinition.js +190 -150
  18. package/dist/src/eventDefinitions/CompensateEventDefinition.js +194 -161
  19. package/dist/src/eventDefinitions/ConditionalEventDefinition.js +197 -135
  20. package/dist/src/eventDefinitions/ErrorEventDefinition.js +207 -165
  21. package/dist/src/eventDefinitions/EscalationEventDefinition.js +175 -141
  22. package/dist/src/eventDefinitions/EventDefinitionExecution.js +157 -129
  23. package/dist/src/eventDefinitions/LinkEventDefinition.js +174 -149
  24. package/dist/src/eventDefinitions/MessageEventDefinition.js +213 -176
  25. package/dist/src/eventDefinitions/SignalEventDefinition.js +203 -161
  26. package/dist/src/eventDefinitions/TerminateEventDefinition.js +21 -23
  27. package/dist/src/eventDefinitions/TimerEventDefinition.js +243 -228
  28. package/dist/src/events/BoundaryEvent.js +180 -144
  29. package/dist/src/events/EndEvent.js +18 -23
  30. package/dist/src/events/IntermediateCatchEvent.js +44 -58
  31. package/dist/src/events/IntermediateThrowEvent.js +18 -23
  32. package/dist/src/events/StartEvent.js +109 -94
  33. package/dist/src/flows/Association.js +94 -100
  34. package/dist/src/flows/MessageFlow.js +86 -103
  35. package/dist/src/flows/SequenceFlow.js +173 -182
  36. package/dist/src/gateways/EventBasedGateway.js +88 -84
  37. package/dist/src/gateways/ExclusiveGateway.js +13 -16
  38. package/dist/src/gateways/InclusiveGateway.js +11 -14
  39. package/dist/src/gateways/ParallelGateway.js +11 -14
  40. package/dist/src/getPropertyValue.js +34 -34
  41. package/dist/src/io/BpmnIO.js +17 -14
  42. package/dist/src/io/EnvironmentDataObject.js +33 -29
  43. package/dist/src/io/EnvironmentDataStore.js +33 -29
  44. package/dist/src/io/EnvironmentDataStoreReference.js +35 -31
  45. package/dist/src/io/InputOutputSpecification.js +177 -168
  46. package/dist/src/io/Properties.js +117 -124
  47. package/dist/src/messageHelper.js +1 -1
  48. package/dist/src/process/Process.js +439 -362
  49. package/dist/src/process/ProcessExecution.js +748 -646
  50. package/dist/src/shared.js +2 -2
  51. package/dist/src/tasks/CallActivity.js +160 -0
  52. package/dist/src/tasks/LoopCharacteristics.js +309 -330
  53. package/dist/src/tasks/ReceiveTask.js +233 -182
  54. package/dist/src/tasks/ScriptTask.js +35 -41
  55. package/dist/src/tasks/ServiceImplementation.js +13 -20
  56. package/dist/src/tasks/ServiceTask.js +82 -75
  57. package/dist/src/tasks/SignalTask.js +97 -93
  58. package/dist/src/tasks/StandardLoopCharacteristics.js +1 -1
  59. package/dist/src/tasks/SubProcess.js +193 -175
  60. package/dist/src/tasks/Task.js +17 -19
  61. package/index.js +2 -0
  62. package/package.json +16 -16
  63. package/src/Api.js +65 -59
  64. package/src/Context.js +145 -140
  65. package/src/Environment.js +116 -100
  66. package/src/EventBroker.js +67 -68
  67. package/src/ExtensionsMapper.js +2 -2
  68. package/src/MessageFormatter.js +132 -74
  69. package/src/activity/Activity.js +914 -776
  70. package/src/activity/ActivityExecution.js +293 -247
  71. package/src/activity/Dummy.js +2 -2
  72. package/src/definition/Definition.js +437 -401
  73. package/src/definition/DefinitionExecution.js +598 -340
  74. package/src/error/Errors.js +11 -6
  75. package/src/eventDefinitions/CancelEventDefinition.js +164 -121
  76. package/src/eventDefinitions/CompensateEventDefinition.js +159 -124
  77. package/src/eventDefinitions/ConditionalEventDefinition.js +147 -104
  78. package/src/eventDefinitions/ErrorEventDefinition.js +190 -131
  79. package/src/eventDefinitions/EscalationEventDefinition.js +139 -101
  80. package/src/eventDefinitions/EventDefinitionExecution.js +127 -95
  81. package/src/eventDefinitions/LinkEventDefinition.js +160 -129
  82. package/src/eventDefinitions/MessageEventDefinition.js +178 -121
  83. package/src/eventDefinitions/SignalEventDefinition.js +162 -106
  84. package/src/eventDefinitions/TerminateEventDefinition.js +19 -19
  85. package/src/eventDefinitions/TimerEventDefinition.js +202 -167
  86. package/src/events/BoundaryEvent.js +156 -115
  87. package/src/events/EndEvent.js +15 -18
  88. package/src/events/IntermediateCatchEvent.js +40 -44
  89. package/src/events/IntermediateThrowEvent.js +15 -18
  90. package/src/events/StartEvent.js +84 -50
  91. package/src/flows/Association.js +98 -112
  92. package/src/flows/MessageFlow.js +81 -97
  93. package/src/flows/SequenceFlow.js +146 -160
  94. package/src/gateways/EventBasedGateway.js +75 -68
  95. package/src/gateways/ExclusiveGateway.js +8 -13
  96. package/src/gateways/InclusiveGateway.js +8 -13
  97. package/src/gateways/ParallelGateway.js +8 -13
  98. package/src/getPropertyValue.js +34 -33
  99. package/src/io/BpmnIO.js +20 -15
  100. package/src/io/EnvironmentDataObject.js +29 -18
  101. package/src/io/EnvironmentDataStore.js +29 -18
  102. package/src/io/EnvironmentDataStoreReference.js +31 -20
  103. package/src/io/InputOutputSpecification.js +154 -157
  104. package/src/io/Properties.js +95 -97
  105. package/src/process/Process.js +378 -333
  106. package/src/process/ProcessExecution.js +603 -553
  107. package/src/tasks/CallActivity.js +130 -0
  108. package/src/tasks/LoopCharacteristics.js +290 -289
  109. package/src/tasks/ReceiveTask.js +174 -107
  110. package/src/tasks/ScriptTask.js +27 -30
  111. package/src/tasks/ServiceImplementation.js +13 -18
  112. package/src/tasks/ServiceTask.js +67 -60
  113. package/src/tasks/SignalTask.js +77 -52
  114. package/src/tasks/StandardLoopCharacteristics.js +1 -1
  115. package/src/tasks/SubProcess.js +184 -157
  116. package/src/tasks/Task.js +15 -19
@@ -1,71 +1,96 @@
1
1
  import Activity from '../activity/Activity';
2
- import { ActivityError } from '../error/Errors';
2
+ import {ActivityError} from '../error/Errors';
3
3
  import {cloneContent} from '../messageHelper';
4
4
 
5
5
  export default function SignalTask(activityDef, context) {
6
- return Activity(SignalTaskBehaviour, activityDef, context);
6
+ return new Activity(SignalTaskBehaviour, activityDef, context);
7
7
  }
8
8
 
9
9
  export function SignalTaskBehaviour(activity) {
10
- const {id, type, behaviour, broker} = activity;
11
- const loopCharacteristics = behaviour.loopCharacteristics && behaviour.loopCharacteristics.Behaviour(activity, behaviour.loopCharacteristics);
10
+ const {id, type, behaviour} = activity;
12
11
 
13
- const source = {
14
- id,
15
- type,
16
- loopCharacteristics,
17
- execute,
18
- };
12
+ this.id = id;
13
+ this.type = type;
14
+ this.loopCharacteristics = behaviour.loopCharacteristics && new behaviour.loopCharacteristics.Behaviour(activity, behaviour.loopCharacteristics);
15
+ this.activity = activity;
16
+ this.broker = activity.broker;
17
+ }
19
18
 
20
- return source;
19
+ const proto = SignalTaskBehaviour.prototype;
21
20
 
22
- function execute(executeMessage) {
23
- const content = executeMessage.content;
24
- if (loopCharacteristics && content.isRootScope) {
25
- return loopCharacteristics.execute(executeMessage);
26
- }
21
+ proto.execute = function execute(executeMessage) {
22
+ const executeContent = executeMessage.content;
23
+ const loopCharacteristics = this.loopCharacteristics;
24
+ if (loopCharacteristics && executeContent.isRootScope) {
25
+ return loopCharacteristics.execute(executeMessage);
26
+ }
27
27
 
28
- const {executionId} = content;
28
+ const executionId = executeContent.executionId;
29
29
 
30
- broker.subscribeTmp('api', `activity.#.${executionId}`, onApiMessage, {noAck: true, consumerTag: `_api-${executionId}`});
31
- broker.subscribeTmp('api', '#.signal.*', onDelegatedApiMessage, {noAck: true, consumerTag: `_api-delegated-${executionId}`});
32
- broker.publish('event', 'activity.wait', cloneContent(content, {state: 'wait'}));
30
+ const broker = this.broker;
31
+ broker.subscribeTmp('api', `activity.#.${executionId}`, (...args) => this._onApiMessage(executeMessage, ...args), {
32
+ noAck: true,
33
+ consumerTag: `_api-${executionId}`,
34
+ });
35
+ broker.subscribeTmp('api', '#.signal.*', (...args) => this._onDelegatedApiMessage(executeMessage, ...args), {
36
+ noAck: true,
37
+ consumerTag: `_api-delegated-${executionId}`,
38
+ });
39
+ broker.publish('event', 'activity.wait', cloneContent(executeContent, {state: 'wait'}));
40
+ };
33
41
 
34
- function onDelegatedApiMessage(routingKey, message) {
35
- if (!message.properties.delegate) return;
36
- const {content: delegateContent} = message;
37
- if (!delegateContent || !delegateContent.message) return;
42
+ proto._onDelegatedApiMessage = function onDelegatedApiMessage(executeMessage, routingKey, message) {
43
+ if (!message.properties.delegate) return;
38
44
 
39
- const {id: signalId, executionId: signalExecutionId} = delegateContent.message;
40
- if (loopCharacteristics && signalExecutionId !== executionId) return;
41
- if (signalId !== id && signalExecutionId !== executionId) return;
45
+ const {content: delegateContent} = message;
46
+ if (!delegateContent || !delegateContent.message) return;
42
47
 
43
- const {type: messageType, correlationId} = message.properties;
44
- broker.publish('event', 'activity.consumed', cloneContent(content, {message: {...delegateContent.message}}), {correlationId, type: messageType});
45
- return onApiMessage(routingKey, message);
46
- }
48
+ const executeContent = executeMessage.content;
49
+ const {id: signalId, executionId: signalExecutionId} = delegateContent.message;
50
+ if (this.loopCharacteristics && signalExecutionId !== executeContent.executionId) return;
51
+ if (signalId !== this.id && signalExecutionId !== executeContent.executionId) return;
47
52
 
48
- function onApiMessage(routingKey, message) {
49
- const {type: messageType, correlationId} = message.properties;
53
+ const {type: messageType, correlationId} = message.properties;
54
+ this.broker.publish('event', 'activity.consumed', cloneContent(executeContent, {
55
+ message: { ...delegateContent.message},
56
+ }), {
57
+ correlationId,
58
+ type: messageType,
59
+ });
50
60
 
51
- switch (messageType) {
52
- case 'stop':
53
- return stop();
54
- case 'signal':
55
- stop();
56
- return broker.publish('execution', 'execute.completed', cloneContent(content, {output: message.content.message, state: 'signal'}), {correlationId});
57
- case 'error':
58
- stop();
59
- return broker.publish('execution', 'execute.error', cloneContent(content, {error: new ActivityError(message.content.message, executeMessage, message.content)}, {mandatory: true, correlationId}));
60
- case 'discard':
61
- stop();
62
- return broker.publish('execution', 'execute.discard', cloneContent(content), {correlationId});
63
- }
64
- }
61
+ return this._onApiMessage(executeMessage, routingKey, message);
62
+ };
65
63
 
66
- function stop() {
67
- broker.cancel(`_api-${executionId}`);
68
- broker.cancel(`_api-delegated-${executionId}`);
69
- }
64
+ proto._onApiMessage = function onApiMessage(executeMessage, routingKey, message) {
65
+ const {type: messageType, correlationId} = message.properties;
66
+ const executeContent = executeMessage.content;
67
+ switch (messageType) {
68
+ case 'stop':
69
+ return this._stop(executeContent.executionId);
70
+ case 'signal':
71
+ this._stop(executeContent.executionId);
72
+ return this.broker.publish('execution', 'execute.completed', cloneContent(executeContent, {
73
+ output: message.content.message,
74
+ state: 'signal',
75
+ }), {
76
+ correlationId,
77
+ });
78
+ case 'error':
79
+ this._stop(executeContent.executionId);
80
+ return this.broker.publish('execution', 'execute.error', cloneContent(executeContent, {
81
+ error: new ActivityError(message.content.message, executeMessage, message.content),
82
+ }, {
83
+ mandatory: true,
84
+ correlationId,
85
+ }));
86
+ case 'discard':
87
+ this._stop(executeContent.executionId);
88
+ return this.broker.publish('execution', 'execute.discard', cloneContent(executeContent), {correlationId});
70
89
  }
71
- }
90
+ };
91
+
92
+ proto._stop = function stop(executionId) {
93
+ const broker = this.broker;
94
+ broker.cancel(`_api-${executionId}`);
95
+ broker.cancel(`_api-delegated-${executionId}`);
96
+ };
@@ -3,6 +3,6 @@ import LoopCharacteristics from './LoopCharacteristics';
3
3
  export default function StandardLoopCharacteristics(activity, loopCharacteristics) {
4
4
  let {behaviour} = loopCharacteristics;
5
5
  behaviour = {...behaviour, isSequential: true};
6
- return LoopCharacteristics(activity, {...loopCharacteristics, behaviour});
6
+ return new LoopCharacteristics(activity, {...loopCharacteristics, behaviour});
7
7
  }
8
8
 
@@ -2,9 +2,12 @@ import Activity from '../activity/Activity';
2
2
  import ProcessExecution from '../process/ProcessExecution';
3
3
  import {cloneContent} from '../messageHelper';
4
4
 
5
+ const kExecutions = Symbol.for('executions');
6
+ const kMessageHandlers = Symbol.for('messageHandlers');
7
+
5
8
  export default function SubProcess(activityDef, context) {
6
9
  const triggeredByEvent = activityDef.behaviour && activityDef.behaviour.triggeredByEvent;
7
- const subProcess = Activity(SubProcessBehaviour, {...activityDef, isSubProcess: true, triggeredByEvent}, context);
10
+ const subProcess = new Activity(SubProcessBehaviour, {...activityDef, isSubProcess: true, triggeredByEvent}, context);
8
11
 
9
12
  subProcess.getStartActivities = function getStartActivities(filterOptions) {
10
13
  return context.getStartActivities(filterOptions, activityDef.id);
@@ -18,205 +21,229 @@ export default function SubProcess(activityDef, context) {
18
21
  function onShake(_, message) {
19
22
  const {startId} = message.content;
20
23
  const last = message.content.sequence.pop();
21
- const sequence = ProcessExecution(subProcess, context).shake(startId);
24
+ const sequence = new ProcessExecution(subProcess, context).shake(startId);
22
25
  message.content.sequence.push({...last, isSubProcess: true, sequence});
23
26
  }
24
27
  }
25
28
 
26
29
  export function SubProcessBehaviour(activity, context) {
27
- const {id, type, broker, behaviour, environment, logger} = activity;
28
- const loopCharacteristics = behaviour.loopCharacteristics && behaviour.loopCharacteristics.Behaviour(activity, behaviour.loopCharacteristics);
29
-
30
- const processExecutions = [];
31
- let rootExecutionId;
32
-
33
- const source = {
34
- id,
35
- type,
36
- loopCharacteristics,
37
- get execution() {
38
- return processExecutions[0];
39
- },
40
- get executions() {
41
- return processExecutions;
42
- },
43
- execute,
44
- getApi,
45
- getState,
46
- getPostponed() {
47
- return this.executions.reduce((result, pe) => {
48
- result = result.concat(pe.getPostponed());
49
- return result;
50
- }, []);
51
- },
52
- recover,
30
+ const {id, type, behaviour} = activity;
31
+ this.id = id;
32
+ this.type = type;
33
+ this.loopCharacteristics = behaviour.loopCharacteristics && new behaviour.loopCharacteristics.Behaviour(activity, behaviour.loopCharacteristics);
34
+ this.activity = activity;
35
+ this.context = context;
36
+ this.environment = activity.environment;
37
+ this.broker = activity.broker;
38
+ this.executionId = undefined;
39
+
40
+ this[kExecutions] = [];
41
+ this[kMessageHandlers] = {
42
+ onApiRootMessage: this._onApiRootMessage.bind(this),
43
+ onExecutionCompleted: this._onExecutionCompleted.bind(this),
53
44
  };
45
+ }
54
46
 
55
- return source;
47
+ const proto = SubProcessBehaviour.prototype;
56
48
 
57
- function execute(executeMessage) {
58
- const content = executeMessage.content;
49
+ Object.defineProperty(proto, 'execution', {
50
+ get() {
51
+ return this[kExecutions][0];
52
+ },
53
+ });
59
54
 
60
- if (content.isRootScope) {
61
- rootExecutionId = content.executionId;
62
- }
55
+ Object.defineProperty(proto, 'executions', {
56
+ get() {
57
+ return this[kExecutions].slice();
58
+ },
59
+ });
63
60
 
64
- if (loopCharacteristics && content.isRootScope) {
65
- broker.subscribeTmp('api', `activity.#.${rootExecutionId}`, onApiRootMessage, {noAck: true, consumerTag: `_api-${rootExecutionId}`, priority: 200});
66
- return loopCharacteristics.execute(executeMessage);
67
- }
61
+ proto.execute = function execute(executeMessage) {
62
+ const content = executeMessage.content;
68
63
 
69
- const processExecution = upsertExecution(executeMessage);
70
- if (!processExecution) return;
64
+ let executionId = this.executionId;
65
+ if (content.isRootScope) {
66
+ executionId = this.executionId = content.executionId;
67
+ }
71
68
 
72
- return processExecution.execute(executeMessage);
69
+ const loopCharacteristics = this.loopCharacteristics;
70
+ if (loopCharacteristics && content.isRootScope) {
71
+ this.broker.subscribeTmp('api', `activity.#.${executionId}`, this[kMessageHandlers].onApiRootMessage, {
72
+ noAck: true,
73
+ consumerTag: `_api-${executionId}`,
74
+ priority: 200,
75
+ });
73
76
 
74
- function onApiRootMessage(routingKey, message) {
75
- const messageType = message.properties.type;
77
+ return loopCharacteristics.execute(executeMessage);
78
+ }
76
79
 
77
- switch (messageType) {
78
- case 'stop':
79
- broker.cancel(`_api-${rootExecutionId}`);
80
- stop();
81
- break;
82
- case 'discard':
83
- broker.cancel(`_api-${rootExecutionId}`);
84
- discard();
85
- break;
86
- }
87
- }
80
+ const processExecution = this._upsertExecution(executeMessage);
81
+ if (!processExecution) return;
82
+
83
+ return processExecution.execute(executeMessage);
84
+ };
85
+
86
+ proto.stop = function stop() {
87
+ for (const execution of this[kExecutions]) {
88
+ this.broker.cancel(`_sub-process-execution-${execution.executionId}`);
89
+ this.broker.cancel(`_sub-process-api-${execution.executionId}`);
90
+ execution.stop();
88
91
  }
92
+ };
89
93
 
90
- function stop() {
91
- return processExecutions.forEach((pe) => {
92
- broker.cancel(`_sub-process-execution-${pe.executionId}`);
93
- broker.cancel(`_sub-process-api-${pe.executionId}`);
94
- pe.stop();
95
- });
94
+ proto.discard = function discard() {
95
+ for (const execution of this[kExecutions]) {
96
+ this.broker.cancel(`_sub-process-execution-${execution.executionId}`);
97
+ this.broker.cancel(`_sub-process-api-${execution.executionId}`);
98
+ execution.discard();
99
+ }
100
+ };
101
+
102
+ proto.getState = function getState() {
103
+ if (this.loopCharacteristics) {
104
+ return {
105
+ executions: this[kExecutions].map((pe) => {
106
+ const state = pe.getState();
107
+ state.environment = pe.environment.getState();
108
+ return state;
109
+ }),
110
+ };
96
111
  }
97
112
 
98
- function discard() {
99
- return processExecutions.forEach((pe) => {
100
- broker.cancel(`_sub-process-execution-${pe.executionId}`);
101
- broker.cancel(`_sub-process-api-${pe.executionId}`);
102
- pe.discard();
103
- });
113
+ const execution = this.execution;
114
+ if (execution) {
115
+ const state = execution.getState();
116
+ state.environment = execution.environment.getState();
117
+ return state;
104
118
  }
119
+ };
105
120
 
106
- function getState() {
107
- if (loopCharacteristics) {
108
- return {
109
- executions: processExecutions.map(getExecutionState),
110
- };
111
- }
121
+ proto.recover = function recover(state) {
122
+ if (!state) return;
112
123
 
113
- if (processExecutions.length) {
114
- return getExecutionState(processExecutions[0]);
115
- }
124
+ const executions = this[kExecutions];
116
125
 
117
- function getExecutionState(pe) {
118
- const state = pe.getState();
119
- state.environment = pe.environment.getState();
120
- return state;
126
+ const loopCharacteristics = this.loopCharacteristics;
127
+ if (loopCharacteristics && state.executions) {
128
+ executions.splice(0);
129
+ for (const se of state.executions) {
130
+ this.recover(se);
121
131
  }
132
+ return;
122
133
  }
123
134
 
124
- function recover(state) {
125
- if (!state) return;
126
-
127
- if (loopCharacteristics && state.executions) {
128
- processExecutions.splice(0);
129
- return state.executions.forEach(recover);
130
- } else if (!loopCharacteristics) {
131
- processExecutions.splice(0);
132
- }
135
+ if (!loopCharacteristics) {
136
+ executions.splice(0);
137
+ }
133
138
 
134
- const subEnvironment = environment.clone().recover(state.environment);
135
- const subContext = context.clone(subEnvironment);
139
+ const subEnvironment = this.environment.clone().recover(state.environment);
140
+ const subContext = this.context.clone(subEnvironment);
141
+
142
+ const execution = new ProcessExecution(this.activity, subContext).recover(state);
143
+
144
+ executions.push(execution);
145
+ return execution;
146
+ };
147
+
148
+ proto.getPostponed = function getPostponed() {
149
+ return this[kExecutions].reduce((result, pe) => {
150
+ result = result.concat(pe.getPostponed());
151
+ return result;
152
+ }, []);
153
+ };
154
+
155
+ proto._onApiRootMessage = function onApiRootMessage(_, message) {
156
+ const messageType = message.properties.type;
157
+
158
+ switch (messageType) {
159
+ case 'stop':
160
+ this.broker.cancel(message.fields.consumerTag);
161
+ this.stop();
162
+ break;
163
+ case 'discard':
164
+ this.broker.cancel(message.fields.consumerTag);
165
+ this.discard();
166
+ break;
167
+ }
168
+ };
136
169
 
137
- const execution = ProcessExecution(activity, subContext).recover(state);
170
+ proto._upsertExecution = function upsertExecution(executeMessage) {
171
+ const content = executeMessage.content;
172
+ const executionId = content.executionId;
138
173
 
139
- processExecutions.push(execution);
174
+ let execution = this._getExecutionById(executionId);
175
+ if (execution) {
176
+ if (executeMessage.fields.redelivered) this._addListeners(execution, executionId);
140
177
  return execution;
141
178
  }
142
179
 
143
- function upsertExecution(executeMessage) {
144
- const content = executeMessage.content;
145
- const executionId = content.executionId;
180
+ const subEnvironment = this.environment.clone();
181
+ const subContext = this.context.clone(subEnvironment);
146
182
 
147
- let execution = getExecutionById(executionId);
148
- if (execution) {
149
- if (executeMessage.fields.redelivered) addListeners(execution, executionId);
150
- return execution;
151
- }
183
+ execution = new ProcessExecution(this.activity, subContext);
184
+ this[kExecutions].push(execution);
152
185
 
153
- const subEnvironment = environment.clone({ output: {} });
154
- const subContext = context.clone(subEnvironment);
186
+ this._addListeners(execution, executionId);
155
187
 
156
- execution = ProcessExecution(activity, subContext);
157
- processExecutions.push(execution);
188
+ return execution;
189
+ };
158
190
 
159
- addListeners(execution, executionId);
191
+ proto._addListeners = function addListeners(processExecution, executionId) {
192
+ this.broker.subscribeTmp('subprocess-execution', `execution.#.${executionId}`, this[kMessageHandlers].onExecutionCompleted, {
193
+ noAck: true,
194
+ consumerTag: `_sub-process-execution-${executionId}`,
195
+ });
196
+ };
160
197
 
161
- return execution;
162
- }
198
+ proto._onExecutionCompleted = function onExecutionCompleted(_, message) {
199
+ if (message.fields.redelivered && message.properties.persistent === false) return;
163
200
 
164
- function addListeners(processExecution, executionId) {
165
- const executionConsumerTag = `_sub-process-execution-${executionId}`;
166
-
167
- broker.subscribeTmp('subprocess-execution', `execution.#.${executionId}`, onExecutionCompleted, {noAck: true, consumerTag: executionConsumerTag});
168
-
169
- function onExecutionCompleted(_, message) {
170
- const content = message.content;
171
- const messageType = message.properties.type;
172
-
173
- if (message.fields.redelivered && message.properties.persistent === false) return;
174
-
175
- switch (messageType) {
176
- case 'stopped': {
177
- broker.cancel(executionConsumerTag);
178
- break;
179
- }
180
- case 'discard': {
181
- broker.cancel(executionConsumerTag);
182
- broker.publish('execution', 'execute.discard', cloneContent(content));
183
- break;
184
- }
185
- case 'completed': {
186
- broker.cancel(executionConsumerTag);
187
- broker.publish('execution', 'execute.completed', cloneContent(content));
188
- break;
189
- }
190
- case 'error': {
191
- broker.cancel(executionConsumerTag);
192
-
193
- const {error} = content;
194
- logger.error(`<${id}>`, error);
195
- broker.publish('execution', 'execute.error', cloneContent(content));
196
- break;
197
- }
198
- }
201
+ const content = message.content;
202
+ const messageType = message.properties.type;
203
+ const broker = this.broker;
204
+
205
+ switch (messageType) {
206
+ case 'stopped': {
207
+ broker.cancel(message.fields.consumerTag);
208
+ break;
199
209
  }
200
- }
210
+ case 'discard': {
211
+ broker.cancel(message.fields.consumerTag);
212
+ broker.publish('execution', 'execute.discard', cloneContent(content));
213
+ break;
214
+ }
215
+ case 'completed': {
216
+ broker.cancel(message.fields.consumerTag);
217
+ broker.publish('execution', 'execute.completed', cloneContent(content));
218
+ break;
219
+ }
220
+ case 'error': {
221
+ broker.cancel(message.fields.consumerTag);
201
222
 
202
- function getApi(apiMessage) {
203
- const content = apiMessage.content;
223
+ const {error} = content;
224
+ this.activity.logger.error(`<${this.id}>`, error);
225
+ broker.publish('execution', 'execute.error', cloneContent(content));
226
+ break;
227
+ }
228
+ }
229
+ };
204
230
 
205
- if (content.id === id) return;
231
+ proto.getApi = function getApi(apiMessage) {
232
+ const content = apiMessage.content;
206
233
 
207
- let execution;
208
- if ((execution = getExecutionById(content.parent.executionId))) {
209
- return execution.getApi(apiMessage);
210
- }
234
+ if (content.id === this.id) return;
211
235
 
212
- const parentPath = content.parent.path;
236
+ let execution;
213
237
 
214
- for (let i = 0; i < parentPath.length; i++) {
215
- if ((execution = getExecutionById(parentPath[i].executionId))) return execution.getApi(apiMessage);
216
- }
238
+ if ((execution = this._getExecutionById(content.parent.executionId))) {
239
+ return execution.getApi(apiMessage);
217
240
  }
218
241
 
219
- function getExecutionById(executionId) {
220
- return processExecutions.find((pe) => pe.executionId === executionId);
242
+ for (const pp of content.parent.path) {
243
+ if ((execution = this._getExecutionById(pp.executionId))) return execution.getApi(apiMessage);
221
244
  }
222
- }
245
+ };
246
+
247
+ proto._getExecutionById = function getExecutionById(executionId) {
248
+ return this[kExecutions].find((pe) => pe.executionId === executionId);
249
+ };
package/src/tasks/Task.js CHANGED
@@ -1,28 +1,24 @@
1
1
  import Activity from '../activity/Activity';
2
+ import {cloneContent} from '../messageHelper';
2
3
 
3
4
  export default function Task(activityDef, context) {
4
- return Activity(TaskBehaviour, activityDef, context);
5
+ return new Activity(TaskBehaviour, activityDef, context);
5
6
  }
6
7
 
7
8
  export function TaskBehaviour(activity) {
8
9
  const {id, type, behaviour, broker} = activity;
9
- const loopCharacteristics = behaviour.loopCharacteristics && behaviour.loopCharacteristics.Behaviour(activity, behaviour.loopCharacteristics);
10
-
11
- const source = {
12
- id,
13
- type,
14
- loopCharacteristics,
15
- execute,
16
- };
17
-
18
- return source;
19
-
20
- function execute(executeMessage) {
21
- const content = executeMessage.content;
22
- if (loopCharacteristics && content.isRootScope) {
23
- return loopCharacteristics.execute(executeMessage);
24
- }
10
+ this.id = id;
11
+ this.type = type;
12
+ this.loopCharacteristics = behaviour.loopCharacteristics && new behaviour.loopCharacteristics.Behaviour(activity, behaviour.loopCharacteristics);
13
+ this.broker = broker;
14
+ }
25
15
 
26
- return broker.publish('execution', 'execute.completed', {...content});
16
+ TaskBehaviour.prototype.execute = function execute(executeMessage) {
17
+ const executeContent = executeMessage.content;
18
+ const loopCharacteristics = this.loopCharacteristics;
19
+ if (loopCharacteristics && executeContent.isRootScope) {
20
+ return loopCharacteristics.execute(executeMessage);
27
21
  }
28
- }
22
+
23
+ return this.broker.publish('execution', 'execute.completed', cloneContent(executeContent));
24
+ };