bpmn-elements 6.0.1 → 7.0.0

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 +322 -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 +169 -175
  6. package/dist/src/Environment.js +90 -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 +1106 -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 +722 -409
  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 +433 -359
  49. package/dist/src/process/ProcessExecution.js +744 -645
  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 +195 -175
  60. package/dist/src/tasks/Task.js +17 -19
  61. package/index.js +2 -0
  62. package/package.json +13 -13
  63. package/src/Api.js +65 -59
  64. package/src/Context.js +138 -141
  65. package/src/Environment.js +88 -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 +915 -775
  70. package/src/activity/ActivityExecution.js +293 -247
  71. package/src/activity/Dummy.js +2 -2
  72. package/src/definition/Definition.js +436 -401
  73. package/src/definition/DefinitionExecution.js +603 -343
  74. package/src/error/Errors.js +11 -6
  75. package/src/eventDefinitions/CancelEventDefinition.js +164 -121
  76. package/src/eventDefinitions/CompensateEventDefinition.js +158 -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 +16 -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 +374 -333
  106. package/src/process/ProcessExecution.js +606 -554
  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
@@ -2,389 +2,430 @@ import ProcessExecution from './ProcessExecution';
2
2
  import {getUniqueId} from '../shared';
3
3
  import {ProcessApi} from '../Api';
4
4
  import {ProcessBroker} from '../EventBroker';
5
- import {cloneMessage, cloneContent} from '../messageHelper';
5
+ import {cloneMessage, cloneContent, cloneParent} from '../messageHelper';
6
6
  import {makeErrorFromMessage} from '../error/Errors';
7
7
 
8
+ const consumingSymbol = Symbol.for('consuming');
9
+ const countersSymbol = Symbol.for('counters');
10
+ const execSymbol = Symbol.for('exec');
11
+ const executeMessageSymbol = Symbol.for('executeMessage');
12
+ const extensionsSymbol = Symbol.for('extensions');
13
+ const messageHandlersSymbol = Symbol.for('messageHandlers');
14
+ const stateMessageSymbol = Symbol.for('stateMessage');
15
+ const statusSymbol = Symbol.for('status');
16
+ const stoppedSymbol = Symbol.for('stopped');
17
+
8
18
  export default Process;
9
19
 
10
20
  export function Process(processDef, context) {
11
21
  const {id, type = 'process', name, parent, behaviour = {}} = processDef;
12
- const environment = context.environment;
13
- const {isExecutable} = behaviour;
14
-
15
- const logger = environment.Logger(type.toLowerCase());
22
+ this.id = id;
23
+ this.type = type;
24
+ this.name = name;
25
+ this.parent = parent ? cloneParent(parent) : {};
26
+ this.behaviour = behaviour;
16
27
 
17
- let execution, initExecutionId, executionId, status, stopped, postponedMessage, stateMessage, consumingRunQ;
28
+ const {isExecutable} = behaviour;
29
+ this.isExecutable = isExecutable;
18
30
 
19
- let counters = {
31
+ const environment = this.environment = context.environment;
32
+ this.context = context;
33
+ this[countersSymbol] = {
20
34
  completed: 0,
21
35
  discarded: 0,
22
- terminated: 0,
23
36
  };
24
-
25
- const processApi = {
26
- id,
27
- type,
28
- name,
29
- isExecutable,
30
- behaviour,
31
- get counters() {
32
- return {...counters};
33
- },
34
- get executionId() {
35
- return executionId;
36
- },
37
- get status() {
38
- return status;
39
- },
40
- get stopped() {
41
- return stopped;
42
- },
43
- get execution() {
44
- return execution;
45
- },
46
- get isRunning() {
47
- if (!consumingRunQ) return false;
48
- return !!status;
49
- },
50
- context,
51
- environment,
52
- parent: {...parent},
53
- logger,
54
- getApi,
55
- getActivities,
56
- getActivityById,
57
- getSequenceFlows,
58
- getPostponed,
59
- getStartActivities,
60
- getState,
61
- init,
62
- recover,
63
- resume,
64
- run,
65
- sendMessage,
66
- shake,
67
- signal,
68
- cancelActivity,
69
- stop,
37
+ this[stoppedSymbol] = false;
38
+ this[execSymbol] = {};
39
+
40
+ const {broker, on, once, waitFor} = ProcessBroker(this);
41
+ this.broker = broker;
42
+ this.on = on;
43
+ this.once = once;
44
+ this.waitFor = waitFor;
45
+
46
+ this[messageHandlersSymbol] = {
47
+ onApiMessage: this._onApiMessage.bind(this),
48
+ onRunMessage: this._onRunMessage.bind(this),
49
+ onExecutionMessage: this._onExecutionMessage.bind(this),
70
50
  };
71
51
 
72
- const {broker, on, once, waitFor} = ProcessBroker(processApi);
73
-
74
- processApi.on = on;
75
- processApi.once = once;
76
- processApi.waitFor = waitFor;
77
-
78
- const runQ = broker.getQueue('run-q');
79
- const executionQ = broker.getQueue('execution-q');
80
-
81
- Object.defineProperty(processApi, 'broker', {
82
- enumerable: true,
83
- get: () => broker,
84
- });
85
-
86
- const extensions = context.loadExtensions(processApi);
87
- Object.defineProperty(processApi, 'extensions', {
88
- enumerable: true,
89
- get: () => extensions,
90
- });
52
+ this.logger = environment.Logger(type.toLowerCase());
91
53
 
92
- return processApi;
54
+ this[extensionsSymbol] = context.loadExtensions(this);
55
+ }
93
56
 
94
- function init() {
95
- initExecutionId = getUniqueId(id);
96
- logger.debug(`<${id}> initialized with executionId <${initExecutionId}>`);
97
- publishEvent('init', createMessage({executionId: initExecutionId}));
57
+ const proto = Process.prototype;
58
+
59
+ Object.defineProperty(proto, 'counters', {
60
+ enumerable: true,
61
+ get() {
62
+ return {...this[countersSymbol]};
63
+ },
64
+ });
65
+
66
+ Object.defineProperty(proto, 'extensions', {
67
+ enumerable: true,
68
+ get() {
69
+ return this[extensionsSymbol];
70
+ },
71
+ });
72
+
73
+ Object.defineProperty(proto, 'stopped', {
74
+ enumerable: true,
75
+ get() {
76
+ return this[stoppedSymbol];
77
+ },
78
+ });
79
+
80
+ Object.defineProperty(proto, 'isRunning', {
81
+ enumerable: true,
82
+ get() {
83
+ if (!this[consumingSymbol]) return false;
84
+ return !!this.status;
85
+ },
86
+ });
87
+
88
+ Object.defineProperty(proto, 'executionId', {
89
+ enumerable: true,
90
+ get() {
91
+ const {executionId, initExecutionId} = this[execSymbol];
92
+ return executionId || initExecutionId;
93
+ },
94
+ });
95
+
96
+ Object.defineProperty(proto, 'execution', {
97
+ enumerable: true,
98
+ get() {
99
+ return this[execSymbol].execution;
100
+ },
101
+ });
102
+
103
+ Object.defineProperty(proto, 'status', {
104
+ enumerable: true,
105
+ get() {
106
+ return this[statusSymbol];
107
+ },
108
+ });
109
+
110
+ proto.init = function init(useAsExecutionId) {
111
+ const exec = this[execSymbol];
112
+ const initExecutionId = exec.initExecutionId = useAsExecutionId || getUniqueId(this.id);
113
+ this._debug(`initialized with executionId <${initExecutionId}>`);
114
+ this._publishEvent('init', this._createMessage({executionId: initExecutionId}));
115
+ };
116
+
117
+ proto.run = function run(runContent) {
118
+ if (this.isRunning) throw new Error(`process <${this.id}> is already running`);
119
+
120
+ const exec = this[execSymbol];
121
+ const executionId = exec.executionId = exec.initExecutionId || getUniqueId(this.id);
122
+ exec.initExecutionId = undefined;
123
+
124
+ const content = this._createMessage({...runContent, executionId});
125
+
126
+ const broker = this.broker;
127
+ broker.publish('run', 'run.enter', content);
128
+ broker.publish('run', 'run.start', cloneContent(content));
129
+ broker.publish('run', 'run.execute', cloneContent(content));
130
+
131
+ this._activateRunConsumers();
132
+ };
133
+
134
+ proto.resume = function resume() {
135
+ if (this.isRunning) throw new Error(`cannot resume running process <${this.id}>`);
136
+ if (!this.status) return this;
137
+
138
+ this[stoppedSymbol] = false;
139
+
140
+ const content = this._createMessage();
141
+ this.broker.publish('run', 'run.resume', content, {persistent: false});
142
+ this._activateRunConsumers();
143
+ return this;
144
+ };
145
+
146
+ proto.recover = function recover(state) {
147
+ if (this.isRunning) throw new Error(`cannot recover running process <${this.id}>`);
148
+ if (!state) return this;
149
+
150
+ this[stoppedSymbol] = !!state.stopped;
151
+ this[statusSymbol] = state.status;
152
+ const exec = this[execSymbol];
153
+ exec.executionId = state.executionId;
154
+ this[countersSymbol] = {...this[countersSymbol], ...state.counters};
155
+ this.environment.recover(state.environment);
156
+
157
+ if (state.execution) {
158
+ exec.execution = new ProcessExecution(this, this.context).recover(state.execution);
98
159
  }
99
160
 
100
- function run(runContent) {
101
- if (processApi.isRunning) throw new Error(`process <${id}> is already running`);
102
-
103
- executionId = initExecutionId || getUniqueId(id);
104
- initExecutionId = undefined;
105
-
106
- const content = createMessage({...runContent, executionId});
107
-
108
- broker.publish('run', 'run.enter', content);
109
- broker.publish('run', 'run.start', cloneContent(content));
110
- broker.publish('run', 'run.execute', cloneContent(content));
111
-
112
- activateRunConsumers();
161
+ this.broker.recover(state.broker);
162
+
163
+ return this;
164
+ };
165
+
166
+ proto.shake = function shake(startId) {
167
+ if (this.isRunning) return this.execution.shake(startId);
168
+ return new ProcessExecution(this, this.context).shake(startId);
169
+ };
170
+
171
+ proto.stop = function stop() {
172
+ if (!this.isRunning) return;
173
+ this.getApi().stop();
174
+ };
175
+
176
+ proto.getApi = function getApi(message) {
177
+ const execution = this.execution;
178
+ if (execution) return execution.getApi(message);
179
+ return ProcessApi(this.broker, message || this[stateMessageSymbol]);
180
+ };
181
+
182
+ proto.signal = function signal(message) {
183
+ return this.getApi().signal(message, {delegate: true});
184
+ };
185
+
186
+ proto.getState = function getState() {
187
+ return this._createMessage({
188
+ environment: this.environment.getState(),
189
+ status: this.status,
190
+ stopped: this.stopped,
191
+ counters: this.counters,
192
+ broker: this.broker.getState(true),
193
+ execution: this.execution && this.execution.getState(),
194
+ output: {...this.environment.output},
195
+ });
196
+ };
197
+
198
+ proto.cancelActivity = function cancelActivity(message) {
199
+ return this.getApi().cancel(message, {delegate: true});
200
+ };
201
+
202
+ proto._activateRunConsumers = function activateRunConsumers() {
203
+ this[consumingSymbol] = true;
204
+ const broker = this.broker;
205
+ const {onApiMessage, onRunMessage} = this[messageHandlersSymbol];
206
+ broker.subscribeTmp('api', `process.*.${this.executionId}`, onApiMessage, {noAck: true, consumerTag: '_process-api', priority: 100});
207
+ broker.getQueue('run-q').assertConsumer(onRunMessage, {exclusive: true, consumerTag: '_process-run'});
208
+ };
209
+
210
+ proto._deactivateRunConsumers = function deactivateRunConsumers() {
211
+ const broker = this.broker;
212
+ broker.cancel('_process-api');
213
+ broker.cancel('_process-run');
214
+ broker.cancel('_process-execution');
215
+ this[consumingSymbol] = false;
216
+ };
217
+
218
+ proto._onRunMessage = function onRunMessage(routingKey, message) {
219
+ const {content, fields} = message;
220
+
221
+ if (routingKey === 'run.resume') {
222
+ return this._onResumeMessage(message);
113
223
  }
114
224
 
115
- function resume() {
116
- if (processApi.isRunning) throw new Error(`cannot resume running process <${id}>`);
117
- if (!status) return processApi;
118
-
119
- stopped = false;
225
+ const exec = this[execSymbol];
226
+ this[stateMessageSymbol] = message;
120
227
 
121
- const content = createMessage({executionId});
122
- broker.publish('run', 'run.resume', content, {persistent: false});
123
- activateRunConsumers();
124
- return processApi;
125
- }
228
+ switch (routingKey) {
229
+ case 'run.enter': {
230
+ this._debug('enter');
126
231
 
127
- function recover(state) {
128
- if (processApi.isRunning) throw new Error(`cannot recover running process <${id}>`);
129
- if (!state) return processApi;
232
+ this[statusSymbol] = 'entered';
233
+ if (fields.redelivered) break;
130
234
 
131
- stopped = state.stopped;
132
- status = state.status;
133
- executionId = state.executionId;
134
- counters = {...counters, ...state.counters};
235
+ exec.execution = undefined;
236
+ this._publishEvent('enter', content);
135
237
 
136
- if (state.execution) {
137
- execution = ProcessExecution(processApi, context).recover(state.execution);
238
+ break;
138
239
  }
139
-
140
- broker.recover(state.broker);
141
-
142
- return processApi;
143
- }
144
-
145
- function shake(startId) {
146
- if (processApi.isRunning) return execution.shake(startId);
147
- return ProcessExecution(processApi, context).shake(startId);
148
- }
149
-
150
- function activateRunConsumers() {
151
- consumingRunQ = true;
152
- broker.subscribeTmp('api', `process.*.${executionId}`, onApiMessage, {noAck: true, consumerTag: '_process-api', priority: 100});
153
- runQ.assertConsumer(onRunMessage, {exclusive: true, consumerTag: '_process-run'});
154
- }
155
-
156
- function deactivateRunConsumers() {
157
- broker.cancel('_process-api');
158
- broker.cancel('_process-run');
159
- broker.cancel('_process-execution');
160
- consumingRunQ = false;
161
- }
162
-
163
- function stop() {
164
- if (!processApi.isRunning) return;
165
- getApi().stop();
166
- }
167
-
168
- function getApi(message) {
169
- if (execution) return execution.getApi(message);
170
- return ProcessApi(broker, message || stateMessage);
171
- }
172
-
173
- function signal(message) {
174
- return getApi().signal(message, {delegate: true});
175
- }
176
-
177
- function cancelActivity(message) {
178
- return getApi().cancel(message, {delegate: true});
179
- }
180
-
181
- function onRunMessage(routingKey, message) {
182
- const {content, ack, fields} = message;
183
-
184
- if (routingKey === 'run.resume') {
185
- return onResumeMessage();
240
+ case 'run.start': {
241
+ this._debug('start');
242
+ this[statusSymbol] = 'start';
243
+ this._publishEvent('start', content);
244
+ break;
186
245
  }
187
-
188
- stateMessage = message;
189
-
190
- switch (routingKey) {
191
- case 'run.enter': {
192
- logger.debug(`<${id}> enter`);
193
-
194
- status = 'entered';
195
- if (fields.redelivered) break;
196
-
197
- execution = undefined;
198
- publishEvent('enter', content);
199
-
200
- break;
201
- }
202
- case 'run.start': {
203
- logger.debug(`<${id}> start`);
204
- status = 'start';
205
- publishEvent('start', content);
206
- break;
207
- }
208
- case 'run.execute': {
209
- status = 'executing';
210
- const executeMessage = cloneMessage(message);
211
- if (fields.redelivered && !execution) {
212
- executeMessage.fields.redelivered = undefined;
213
- }
214
- postponedMessage = message;
215
- executionQ.assertConsumer(onExecutionMessage, {exclusive: true, consumerTag: '_process-execution'});
216
- execution = execution || ProcessExecution(processApi, context);
217
- return execution.execute(executeMessage);
218
- }
219
- case 'run.error': {
220
- publishEvent('error', cloneContent(content, {
221
- error: fields.redelivered ? makeErrorFromMessage(message) : content.error,
222
- }));
223
- break;
246
+ case 'run.execute': {
247
+ this[statusSymbol] = 'executing';
248
+ const executeMessage = cloneMessage(message);
249
+ if (fields.redelivered && !exec.execution) {
250
+ executeMessage.fields.redelivered = undefined;
224
251
  }
225
- case 'run.end': {
226
- status = 'end';
252
+ this[executeMessageSymbol] = message;
227
253
 
228
- if (fields.redelivered) break;
229
- logger.debug(`<${id}> completed`);
254
+ this.broker.getQueue('execution-q').assertConsumer(this[messageHandlersSymbol].onExecutionMessage, {
255
+ exclusive: true,
256
+ consumerTag: '_process-execution',
257
+ });
230
258
 
231
- counters.completed++;
232
-
233
- broker.publish('run', 'run.leave', content);
234
- publishEvent('end', content);
235
- break;
236
- }
237
- case 'run.discarded': {
238
- status = 'discarded';
239
- if (fields.redelivered) break;
240
-
241
- counters.discarded++;
242
-
243
- broker.publish('run', 'run.leave', content);
244
- break;
245
- }
246
- case 'run.leave': {
247
- status = undefined;
248
- broker.cancel('_process-api');
249
- publishEvent('leave');
250
- break;
251
- }
259
+ const execution = exec.execution = exec.execution || new ProcessExecution(this, this.context);
260
+ return execution.execute(executeMessage);
252
261
  }
262
+ case 'run.error': {
263
+ this._publishEvent('error', cloneContent(content, {
264
+ error: fields.redelivered ? makeErrorFromMessage(message) : content.error,
265
+ }));
266
+ break;
267
+ }
268
+ case 'run.end': {
269
+ this[statusSymbol] = 'end';
253
270
 
254
- ack();
255
-
256
- function onResumeMessage() {
257
- message.ack();
258
-
259
- switch (stateMessage.fields.routingKey) {
260
- case 'run.enter':
261
- case 'run.start':
262
- case 'run.discarded':
263
- case 'run.end':
264
- case 'run.leave':
265
- break;
266
- default:
267
- return;
268
- }
271
+ if (fields.redelivered) break;
272
+ this._debug('completed');
269
273
 
270
- if (!stateMessage.fields.redelivered) return;
274
+ this[countersSymbol].completed++;
271
275
 
272
- logger.debug(`<${id}> resume from ${status}`);
276
+ this.broker.publish('run', 'run.leave', content);
273
277
 
274
- return broker.publish('run', stateMessage.fields.routingKey, cloneContent(stateMessage.content), stateMessage.properties);
278
+ this._publishEvent('end', content);
279
+ break;
275
280
  }
276
- }
281
+ case 'run.discarded': {
282
+ this[statusSymbol] = 'discarded';
283
+ if (fields.redelivered) break;
277
284
 
278
- function onExecutionMessage(routingKey, message) {
279
- const content = message.content;
280
- const messageType = message.properties.type;
281
- message.ack();
285
+ this[countersSymbol].discarded++;
282
286
 
283
- switch (messageType) {
284
- case 'stopped': {
285
- return onStop();
286
- }
287
- case 'error': {
288
- broker.publish('run', 'run.error', content);
289
- broker.publish('run', 'run.discarded', content);
290
- break;
291
- }
292
- case 'discard':
293
- broker.publish('run', 'run.discarded', content);
294
- break;
295
- default: {
296
- broker.publish('run', 'run.end', content);
297
- }
298
- }
287
+ this.broker.publish('run', 'run.leave', content);
299
288
 
300
- if (postponedMessage) {
301
- const ackMessage = postponedMessage;
302
- postponedMessage = null;
303
- ackMessage.ack();
289
+ this._publishEvent('discarded', content);
290
+ break;
304
291
  }
305
- }
306
-
307
- function publishEvent(state, content) {
308
- if (!content) content = createMessage();
309
- broker.publish('event', `process.${state}`, {...content, state}, {type: state, mandatory: state === 'error'});
310
- }
311
-
312
- function sendMessage(message) {
313
- const messageContent = message.content;
314
- if (!messageContent) return;
315
-
316
- let targetsFound = false;
317
- if (messageContent.target && messageContent.target.id && getActivityById(messageContent.target.id)) {
318
- targetsFound = true;
319
- } else if (messageContent.message && getStartActivities({referenceId: messageContent.message.id, referenceType: messageContent.message.messageType}).length) {
320
- targetsFound = true;
292
+ case 'run.leave': {
293
+ this[statusSymbol] = undefined;
294
+ this.broker.cancel('_process-api');
295
+ const {output, ...rest} = content; // eslint-disable-line no-unused-vars
296
+ this._publishEvent('leave', rest);
297
+ break;
321
298
  }
322
- if (!targetsFound) return;
323
-
324
- if (!status) run();
325
- getApi().sendApiMessage(message.properties.type || 'message', cloneContent(messageContent), {delegate: true});
326
299
  }
327
300
 
328
- function getActivityById(childId) {
329
- if (execution) return execution.getActivityById(childId);
330
- return context.getActivityById(childId);
301
+ message.ack();
302
+ };
303
+
304
+ proto._onResumeMessage = function onResumeMessage(message) {
305
+ message.ack();
306
+
307
+ const stateMessage = this[stateMessageSymbol];
308
+ switch (stateMessage.fields.routingKey) {
309
+ case 'run.enter':
310
+ case 'run.start':
311
+ case 'run.discarded':
312
+ case 'run.end':
313
+ case 'run.leave':
314
+ break;
315
+ default:
316
+ return;
331
317
  }
332
318
 
333
- function getActivities() {
334
- if (execution) return execution.getActivities();
335
- return context.getActivities(id);
336
- }
319
+ if (!stateMessage.fields.redelivered) return;
337
320
 
338
- function getStartActivities(filterOptions) {
339
- return context.getStartActivities(filterOptions, id);
340
- }
341
-
342
- function getSequenceFlows() {
343
- if (execution) return execution.getSequenceFlows();
344
- return context.getSequenceFlows();
345
- }
321
+ this._debug(`resume from ${this.status}`);
346
322
 
347
- function getPostponed(...args) {
348
- if (execution) return execution.getPostponed(...args);
349
- return [];
350
- }
323
+ return this.broker.publish('run', stateMessage.fields.routingKey, cloneContent(stateMessage.content), stateMessage.properties);
324
+ };
351
325
 
352
- function onApiMessage(routingKey, message) {
353
- const messageType = message.properties.type;
326
+ proto._onExecutionMessage = function onExecutionMessage(routingKey, message) {
327
+ const content = message.content;
328
+ const messageType = message.properties.type;
329
+ message.ack();
354
330
 
355
- switch (messageType) {
356
- case 'stop': {
357
- if (execution && !execution.completed) return;
358
- onStop();
359
- break;
360
- }
331
+ switch (messageType) {
332
+ case 'stopped': {
333
+ return this._onStop();
334
+ }
335
+ case 'error': {
336
+ this.broker.publish('run', 'run.error', content);
337
+ this.broker.publish('run', 'run.discarded', content);
338
+ break;
339
+ }
340
+ case 'discard':
341
+ this.broker.publish('run', 'run.discarded', content);
342
+ break;
343
+ default: {
344
+ this.broker.publish('run', 'run.end', content);
361
345
  }
362
346
  }
363
347
 
364
- function onStop() {
365
- stopped = true;
366
- deactivateRunConsumers();
367
- return publishEvent('stop');
348
+ const executeMessage = this[executeMessageSymbol];
349
+ if (executeMessage) {
350
+ this[executeMessageSymbol] = null;
351
+ executeMessage.ack();
368
352
  }
369
-
370
- function createMessage(override = {}) {
371
- return {
372
- id,
373
- type,
374
- name,
375
- parent: {...parent},
376
- ...override,
377
- };
353
+ };
354
+
355
+ proto._publishEvent = function publishEvent(state, content) {
356
+ const eventContent = this._createMessage({...content, state});
357
+ this.broker.publish('event', `process.${state}`, eventContent, {type: state, mandatory: state === 'error'});
358
+ };
359
+
360
+ proto.sendMessage = function sendMessage(message) {
361
+ const messageContent = message.content;
362
+ if (!messageContent) return;
363
+
364
+ let targetsFound = false;
365
+ if (messageContent.target && messageContent.target.id && this.getActivityById(messageContent.target.id)) {
366
+ targetsFound = true;
367
+ } else if (messageContent.message && this.getStartActivities({referenceId: messageContent.message.id, referenceType: messageContent.message.messageType}).length) {
368
+ targetsFound = true;
378
369
  }
379
-
380
- function getState() {
381
- return createMessage({
382
- executionId,
383
- status,
384
- stopped,
385
- counters: {...counters},
386
- broker: broker.getState(true),
387
- execution: execution && execution.getState(),
388
- });
370
+ if (!targetsFound) return;
371
+
372
+ if (!this.status) this.run();
373
+ this.getApi().sendApiMessage(message.properties.type || 'message', cloneContent(messageContent), {delegate: true});
374
+ };
375
+
376
+ proto.getActivityById = function getActivityById(childId) {
377
+ if (this.execution) return this.execution.getActivityById(childId);
378
+ return this.context.getActivityById(childId);
379
+ };
380
+
381
+ proto.getActivities = function getActivities() {
382
+ if (this.execution) return this.execution.getActivities();
383
+ return this.context.getActivities(this.id);
384
+ };
385
+
386
+ proto.getStartActivities = function getStartActivities(filterOptions) {
387
+ return this.context.getStartActivities(filterOptions, this.id);
388
+ };
389
+
390
+ proto.getSequenceFlows = function getSequenceFlows() {
391
+ if (this.execution) return this.execution.getSequenceFlows();
392
+ return this.context.getSequenceFlows();
393
+ };
394
+
395
+ proto.getPostponed = function getPostponed(...args) {
396
+ if (!this.execution) return [];
397
+ return this.execution.getPostponed(...args);
398
+ };
399
+
400
+ proto._onApiMessage = function onApiMessage(routingKey, message) {
401
+ const messageType = message.properties.type;
402
+
403
+ switch (messageType) {
404
+ case 'stop': {
405
+ if (this.execution && !this.execution.completed) return;
406
+ this._onStop();
407
+ break;
408
+ }
389
409
  }
390
- }
410
+ };
411
+
412
+ proto._onStop = function onStop() {
413
+ this[stoppedSymbol] = true;
414
+ this._deactivateRunConsumers();
415
+ return this._publishEvent('stop');
416
+ };
417
+
418
+ proto._createMessage = function createMessage(override) {
419
+ return {
420
+ id: this.id,
421
+ type: this.type,
422
+ name: this.name,
423
+ executionId: this.executionId,
424
+ parent: {...this.parent},
425
+ ...override,
426
+ };
427
+ };
428
+
429
+ proto._debug = function debug(msg) {
430
+ this.logger.debug(`<${this.id}> ${msg}`);
431
+ };