bpmn-elements 6.0.0 → 8.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 +335 -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 +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 +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 +15 -15
  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 +915 -775
  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
@@ -20,6 +20,15 @@ var _Errors = require("../error/Errors");
20
20
 
21
21
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
22
22
 
23
+ const kConsuming = Symbol.for('consuming');
24
+ const kCounters = Symbol.for('counters');
25
+ const kExec = Symbol.for('execution');
26
+ const kExecuteMessage = Symbol.for('executeMessage');
27
+ const kExtensions = Symbol.for('extensions');
28
+ const kMessageHandlers = Symbol.for('messageHandlers');
29
+ const kStateMessage = Symbol.for('stateMessage');
30
+ const kStatus = Symbol.for('status');
31
+ const kStopped = Symbol.for('stopped');
23
32
  var _default = Process;
24
33
  exports.default = _default;
25
34
 
@@ -31,433 +40,501 @@ function Process(processDef, context) {
31
40
  parent,
32
41
  behaviour = {}
33
42
  } = processDef;
34
- const environment = context.environment;
43
+ this.id = id;
44
+ this.type = type;
45
+ this.name = name;
46
+ this.parent = parent ? (0, _messageHelper.cloneParent)(parent) : {};
47
+ this.behaviour = behaviour;
35
48
  const {
36
49
  isExecutable
37
50
  } = behaviour;
38
- const logger = environment.Logger(type.toLowerCase());
39
- let execution, initExecutionId, executionId, status, stopped, postponedMessage, stateMessage, consumingRunQ;
40
- let counters = {
51
+ this.isExecutable = isExecutable;
52
+ const environment = this.environment = context.environment;
53
+ this.context = context;
54
+ this[kCounters] = {
41
55
  completed: 0,
42
- discarded: 0,
43
- terminated: 0
44
- };
45
- const processApi = {
46
- id,
47
- type,
48
- name,
49
- isExecutable,
50
- behaviour,
51
-
52
- get counters() {
53
- return { ...counters
54
- };
55
- },
56
-
57
- get executionId() {
58
- return executionId;
59
- },
60
-
61
- get status() {
62
- return status;
63
- },
64
-
65
- get stopped() {
66
- return stopped;
67
- },
68
-
69
- get execution() {
70
- return execution;
71
- },
72
-
73
- get isRunning() {
74
- if (!consumingRunQ) return false;
75
- return !!status;
76
- },
77
-
78
- context,
79
- environment,
80
- parent: { ...parent
81
- },
82
- logger,
83
- getApi,
84
- getActivities,
85
- getActivityById,
86
- getSequenceFlows,
87
- getPostponed,
88
- getStartActivities,
89
- getState,
90
- init,
91
- recover,
92
- resume,
93
- run,
94
- sendMessage,
95
- shake,
96
- signal,
97
- cancelActivity,
98
- stop
56
+ discarded: 0
99
57
  };
58
+ this[kConsuming] = false;
59
+ this[kExec] = {};
60
+ this[kStatus] = undefined;
61
+ this[kStopped] = false;
100
62
  const {
101
63
  broker,
102
64
  on,
103
65
  once,
104
66
  waitFor
105
- } = (0, _EventBroker.ProcessBroker)(processApi);
106
- processApi.on = on;
107
- processApi.once = once;
108
- processApi.waitFor = waitFor;
109
- const runQ = broker.getQueue('run-q');
110
- const executionQ = broker.getQueue('execution-q');
111
- Object.defineProperty(processApi, 'broker', {
112
- enumerable: true,
113
- get: () => broker
114
- });
115
- const extensions = context.loadExtensions(processApi);
116
- Object.defineProperty(processApi, 'extensions', {
117
- enumerable: true,
118
- get: () => extensions
119
- });
120
- return processApi;
121
-
122
- function init() {
123
- initExecutionId = (0, _shared.getUniqueId)(id);
124
- logger.debug(`<${id}> initialized with executionId <${initExecutionId}>`);
125
- publishEvent('init', createMessage({
126
- executionId: initExecutionId
127
- }));
128
- }
129
-
130
- function run(runContent) {
131
- if (processApi.isRunning) throw new Error(`process <${id}> is already running`);
132
- executionId = initExecutionId || (0, _shared.getUniqueId)(id);
133
- initExecutionId = undefined;
134
- const content = createMessage({ ...runContent,
135
- executionId
136
- });
137
- broker.publish('run', 'run.enter', content);
138
- broker.publish('run', 'run.start', (0, _messageHelper.cloneContent)(content));
139
- broker.publish('run', 'run.execute', (0, _messageHelper.cloneContent)(content));
140
- activateRunConsumers();
141
- }
67
+ } = (0, _EventBroker.ProcessBroker)(this);
68
+ this.broker = broker;
69
+ this.on = on;
70
+ this.once = once;
71
+ this.waitFor = waitFor;
72
+ this[kMessageHandlers] = {
73
+ onApiMessage: this._onApiMessage.bind(this),
74
+ onRunMessage: this._onRunMessage.bind(this),
75
+ onExecutionMessage: this._onExecutionMessage.bind(this)
76
+ };
77
+ this.logger = environment.Logger(type.toLowerCase());
78
+ this[kExtensions] = context.loadExtensions(this);
79
+ }
142
80
 
143
- function resume() {
144
- if (processApi.isRunning) throw new Error(`cannot resume running process <${id}>`);
145
- if (!status) return processApi;
146
- stopped = false;
147
- const content = createMessage({
148
- executionId
149
- });
150
- broker.publish('run', 'run.resume', content, {
151
- persistent: false
152
- });
153
- activateRunConsumers();
154
- return processApi;
155
- }
81
+ const proto = Process.prototype;
82
+ Object.defineProperty(proto, 'counters', {
83
+ enumerable: true,
156
84
 
157
- function recover(state) {
158
- if (processApi.isRunning) throw new Error(`cannot recover running process <${id}>`);
159
- if (!state) return processApi;
160
- stopped = state.stopped;
161
- status = state.status;
162
- executionId = state.executionId;
163
- counters = { ...counters,
164
- ...state.counters
85
+ get() {
86
+ return { ...this[kCounters]
165
87
  };
88
+ }
166
89
 
167
- if (state.execution) {
168
- execution = (0, _ProcessExecution.default)(processApi, context).recover(state.execution);
169
- }
90
+ });
91
+ Object.defineProperty(proto, 'extensions', {
92
+ enumerable: true,
170
93
 
171
- broker.recover(state.broker);
172
- return processApi;
94
+ get() {
95
+ return this[kExtensions];
173
96
  }
174
97
 
175
- function shake(startId) {
176
- if (processApi.isRunning) return execution.shake(startId);
177
- return (0, _ProcessExecution.default)(processApi, context).shake(startId);
98
+ });
99
+ Object.defineProperty(proto, 'stopped', {
100
+ enumerable: true,
101
+
102
+ get() {
103
+ return this[kStopped];
178
104
  }
179
105
 
180
- function activateRunConsumers() {
181
- consumingRunQ = true;
182
- broker.subscribeTmp('api', `process.*.${executionId}`, onApiMessage, {
183
- noAck: true,
184
- consumerTag: '_process-api',
185
- priority: 100
186
- });
187
- runQ.assertConsumer(onRunMessage, {
188
- exclusive: true,
189
- consumerTag: '_process-run'
190
- });
106
+ });
107
+ Object.defineProperty(proto, 'isRunning', {
108
+ enumerable: true,
109
+
110
+ get() {
111
+ if (!this[kConsuming]) return false;
112
+ return !!this.status;
191
113
  }
192
114
 
193
- function deactivateRunConsumers() {
194
- broker.cancel('_process-api');
195
- broker.cancel('_process-run');
196
- broker.cancel('_process-execution');
197
- consumingRunQ = false;
115
+ });
116
+ Object.defineProperty(proto, 'executionId', {
117
+ enumerable: true,
118
+
119
+ get() {
120
+ const {
121
+ executionId,
122
+ initExecutionId
123
+ } = this[kExec];
124
+ return executionId || initExecutionId;
198
125
  }
199
126
 
200
- function stop() {
201
- if (!processApi.isRunning) return;
202
- getApi().stop();
127
+ });
128
+ Object.defineProperty(proto, 'execution', {
129
+ enumerable: true,
130
+
131
+ get() {
132
+ return this[kExec].execution;
203
133
  }
204
134
 
205
- function getApi(message) {
206
- if (execution) return execution.getApi(message);
207
- return (0, _Api.ProcessApi)(broker, message || stateMessage);
135
+ });
136
+ Object.defineProperty(proto, 'status', {
137
+ enumerable: true,
138
+
139
+ get() {
140
+ return this[kStatus];
208
141
  }
209
142
 
210
- function signal(message) {
211
- return getApi().signal(message, {
212
- delegate: true
213
- });
143
+ });
144
+
145
+ proto.init = function init(useAsExecutionId) {
146
+ const exec = this[kExec];
147
+ const initExecutionId = exec.initExecutionId = useAsExecutionId || (0, _shared.getUniqueId)(this.id);
148
+
149
+ this._debug(`initialized with executionId <${initExecutionId}>`);
150
+
151
+ this._publishEvent('init', this._createMessage({
152
+ executionId: initExecutionId
153
+ }));
154
+ };
155
+
156
+ proto.run = function run(runContent) {
157
+ if (this.isRunning) throw new Error(`process <${this.id}> is already running`);
158
+ const exec = this[kExec];
159
+ const executionId = exec.executionId = exec.initExecutionId || (0, _shared.getUniqueId)(this.id);
160
+ exec.initExecutionId = undefined;
161
+
162
+ const content = this._createMessage({ ...runContent,
163
+ executionId
164
+ });
165
+
166
+ const broker = this.broker;
167
+ broker.publish('run', 'run.enter', content);
168
+ broker.publish('run', 'run.start', (0, _messageHelper.cloneContent)(content));
169
+ broker.publish('run', 'run.execute', (0, _messageHelper.cloneContent)(content));
170
+
171
+ this._activateRunConsumers();
172
+ };
173
+
174
+ proto.resume = function resume() {
175
+ if (this.isRunning) throw new Error(`cannot resume running process <${this.id}>`);
176
+ if (!this.status) return this;
177
+ this[kStopped] = false;
178
+
179
+ const content = this._createMessage();
180
+
181
+ this.broker.publish('run', 'run.resume', content, {
182
+ persistent: false
183
+ });
184
+
185
+ this._activateRunConsumers();
186
+
187
+ return this;
188
+ };
189
+
190
+ proto.recover = function recover(state) {
191
+ if (this.isRunning) throw new Error(`cannot recover running process <${this.id}>`);
192
+ if (!state) return this;
193
+ this[kStopped] = !!state.stopped;
194
+ this[kStatus] = state.status;
195
+ const exec = this[kExec];
196
+ exec.executionId = state.executionId;
197
+ this[kCounters] = { ...this[kCounters],
198
+ ...state.counters
199
+ };
200
+ this.environment.recover(state.environment);
201
+
202
+ if (state.execution) {
203
+ exec.execution = new _ProcessExecution.default(this, this.context).recover(state.execution);
214
204
  }
215
205
 
216
- function cancelActivity(message) {
217
- return getApi().cancel(message, {
218
- delegate: true
219
- });
206
+ this.broker.recover(state.broker);
207
+ return this;
208
+ };
209
+
210
+ proto.shake = function shake(startId) {
211
+ if (this.isRunning) return this.execution.shake(startId);
212
+ return new _ProcessExecution.default(this, this.context).shake(startId);
213
+ };
214
+
215
+ proto.stop = function stop() {
216
+ if (!this.isRunning) return;
217
+ this.getApi().stop();
218
+ };
219
+
220
+ proto.getApi = function getApi(message) {
221
+ const execution = this.execution;
222
+ if (execution) return execution.getApi(message);
223
+ return (0, _Api.ProcessApi)(this.broker, message || this[kStateMessage]);
224
+ };
225
+
226
+ proto.signal = function signal(message) {
227
+ return this.getApi().signal(message, {
228
+ delegate: true
229
+ });
230
+ };
231
+
232
+ proto.getState = function getState() {
233
+ return this._createMessage({
234
+ environment: this.environment.getState(),
235
+ status: this.status,
236
+ stopped: this.stopped,
237
+ counters: this.counters,
238
+ broker: this.broker.getState(true),
239
+ execution: this.execution && this.execution.getState()
240
+ });
241
+ };
242
+
243
+ proto.cancelActivity = function cancelActivity(message) {
244
+ return this.getApi().cancel(message, {
245
+ delegate: true
246
+ });
247
+ };
248
+
249
+ proto._activateRunConsumers = function activateRunConsumers() {
250
+ this[kConsuming] = true;
251
+ const broker = this.broker;
252
+ const {
253
+ onApiMessage,
254
+ onRunMessage
255
+ } = this[kMessageHandlers];
256
+ broker.subscribeTmp('api', `process.*.${this.executionId}`, onApiMessage, {
257
+ noAck: true,
258
+ consumerTag: '_process-api',
259
+ priority: 100
260
+ });
261
+ broker.getQueue('run-q').assertConsumer(onRunMessage, {
262
+ exclusive: true,
263
+ consumerTag: '_process-run'
264
+ });
265
+ };
266
+
267
+ proto._deactivateRunConsumers = function deactivateRunConsumers() {
268
+ const broker = this.broker;
269
+ broker.cancel('_process-api');
270
+ broker.cancel('_process-run');
271
+ broker.cancel('_process-execution');
272
+ this[kConsuming] = false;
273
+ };
274
+
275
+ proto._onRunMessage = function onRunMessage(routingKey, message) {
276
+ const {
277
+ content,
278
+ fields
279
+ } = message;
280
+
281
+ if (routingKey === 'run.resume') {
282
+ return this._onResumeMessage(message);
220
283
  }
221
284
 
222
- function onRunMessage(routingKey, message) {
223
- const {
224
- content,
225
- ack,
226
- fields
227
- } = message;
228
-
229
- if (routingKey === 'run.resume') {
230
- return onResumeMessage();
231
- }
232
-
233
- stateMessage = message;
234
-
235
- switch (routingKey) {
236
- case 'run.enter':
237
- {
238
- logger.debug(`<${id}> enter`);
239
- status = 'entered';
240
- if (fields.redelivered) break;
241
- execution = undefined;
242
- publishEvent('enter', content);
243
- break;
244
- }
285
+ const exec = this[kExec];
286
+ this[kStateMessage] = message;
245
287
 
246
- case 'run.start':
247
- {
248
- logger.debug(`<${id}> start`);
249
- status = 'start';
250
- publishEvent('start', content);
251
- break;
252
- }
288
+ switch (routingKey) {
289
+ case 'run.enter':
290
+ {
291
+ this._debug('enter');
253
292
 
254
- case 'run.execute':
255
- {
256
- status = 'executing';
257
- const executeMessage = (0, _messageHelper.cloneMessage)(message);
258
-
259
- if (fields.redelivered && !execution) {
260
- executeMessage.fields.redelivered = undefined;
261
- }
262
-
263
- postponedMessage = message;
264
- executionQ.assertConsumer(onExecutionMessage, {
265
- exclusive: true,
266
- consumerTag: '_process-execution'
267
- });
268
- execution = execution || (0, _ProcessExecution.default)(processApi, context);
269
- return execution.execute(executeMessage);
270
- }
293
+ this[kStatus] = 'entered';
294
+ if (fields.redelivered) break;
295
+ exec.execution = undefined;
271
296
 
272
- case 'run.error':
273
- {
274
- publishEvent('error', (0, _messageHelper.cloneContent)(content, {
275
- error: fields.redelivered ? (0, _Errors.makeErrorFromMessage)(message) : content.error
276
- }));
277
- break;
278
- }
297
+ this._publishEvent('enter', content);
279
298
 
280
- case 'run.end':
281
- {
282
- status = 'end';
283
- if (fields.redelivered) break;
284
- logger.debug(`<${id}> completed`);
285
- counters.completed++;
286
- broker.publish('run', 'run.leave', content);
287
- publishEvent('end', content);
288
- break;
289
- }
299
+ break;
300
+ }
290
301
 
291
- case 'run.discarded':
292
- {
293
- status = 'discarded';
294
- if (fields.redelivered) break;
295
- counters.discarded++;
296
- broker.publish('run', 'run.leave', content);
297
- break;
298
- }
302
+ case 'run.start':
303
+ {
304
+ this._debug('start');
305
+
306
+ this[kStatus] = 'start';
307
+
308
+ this._publishEvent('start', content);
309
+
310
+ break;
311
+ }
312
+
313
+ case 'run.execute':
314
+ {
315
+ this[kStatus] = 'executing';
316
+ const executeMessage = (0, _messageHelper.cloneMessage)(message);
299
317
 
300
- case 'run.leave':
301
- {
302
- status = undefined;
303
- broker.cancel('_process-api');
304
- publishEvent('leave');
305
- break;
318
+ if (fields.redelivered && !exec.execution) {
319
+ executeMessage.fields.redelivered = undefined;
306
320
  }
307
- }
308
321
 
309
- ack();
322
+ this[kExecuteMessage] = message;
323
+ this.broker.getQueue('execution-q').assertConsumer(this[kMessageHandlers].onExecutionMessage, {
324
+ exclusive: true,
325
+ consumerTag: '_process-execution'
326
+ });
327
+ const execution = exec.execution = exec.execution || new _ProcessExecution.default(this, this.context);
328
+ return execution.execute(executeMessage);
329
+ }
310
330
 
311
- function onResumeMessage() {
312
- message.ack();
331
+ case 'run.error':
332
+ {
333
+ this[kStatus] = 'errored';
313
334
 
314
- switch (stateMessage.fields.routingKey) {
315
- case 'run.enter':
316
- case 'run.start':
317
- case 'run.discarded':
318
- case 'run.end':
319
- case 'run.leave':
320
- break;
335
+ this._publishEvent('error', (0, _messageHelper.cloneContent)(content, {
336
+ error: fields.redelivered ? (0, _Errors.makeErrorFromMessage)(message) : content.error
337
+ }));
321
338
 
322
- default:
323
- return;
339
+ break;
324
340
  }
325
341
 
326
- if (!stateMessage.fields.redelivered) return;
327
- logger.debug(`<${id}> resume from ${status}`);
328
- return broker.publish('run', stateMessage.fields.routingKey, (0, _messageHelper.cloneContent)(stateMessage.content), stateMessage.properties);
329
- }
330
- }
342
+ case 'run.end':
343
+ {
344
+ this[kStatus] = 'end';
345
+ if (fields.redelivered) break;
331
346
 
332
- function onExecutionMessage(routingKey, message) {
333
- const content = message.content;
334
- const messageType = message.properties.type;
335
- message.ack();
347
+ this._debug('completed');
336
348
 
337
- switch (messageType) {
338
- case 'stopped':
339
- {
340
- return onStop();
341
- }
349
+ this[kCounters].completed++;
350
+ this.broker.publish('run', 'run.leave', content);
342
351
 
343
- case 'error':
344
- {
345
- broker.publish('run', 'run.error', content);
346
- broker.publish('run', 'run.discarded', content);
347
- break;
348
- }
352
+ this._publishEvent('end', content);
349
353
 
350
- case 'discard':
351
- broker.publish('run', 'run.discarded', content);
352
354
  break;
355
+ }
353
356
 
354
- default:
355
- {
356
- broker.publish('run', 'run.end', content);
357
- }
358
- }
357
+ case 'run.discarded':
358
+ {
359
+ this[kStatus] = 'discarded';
360
+ if (fields.redelivered) break;
361
+ this[kCounters].discarded++;
362
+ this.broker.publish('run', 'run.leave', content);
359
363
 
360
- if (postponedMessage) {
361
- const ackMessage = postponedMessage;
362
- postponedMessage = null;
363
- ackMessage.ack();
364
- }
365
- }
364
+ this._publishEvent('discarded', content);
366
365
 
367
- function publishEvent(state, content) {
368
- if (!content) content = createMessage();
369
- broker.publish('event', `process.${state}`, { ...content,
370
- state
371
- }, {
372
- type: state,
373
- mandatory: state === 'error'
374
- });
375
- }
366
+ break;
367
+ }
376
368
 
377
- function sendMessage(message) {
378
- const messageContent = message.content;
379
- if (!messageContent) return;
380
- let targetsFound = false;
381
-
382
- if (messageContent.target && messageContent.target.id && getActivityById(messageContent.target.id)) {
383
- targetsFound = true;
384
- } else if (messageContent.message && getStartActivities({
385
- referenceId: messageContent.message.id,
386
- referenceType: messageContent.message.messageType
387
- }).length) {
388
- targetsFound = true;
389
- }
390
-
391
- if (!targetsFound) return;
392
- if (!status) run();
393
- getApi().sendApiMessage(message.properties.type || 'message', (0, _messageHelper.cloneContent)(messageContent), {
394
- delegate: true
395
- });
396
- }
369
+ case 'run.leave':
370
+ {
371
+ this[kStatus] = undefined;
372
+ this.broker.cancel('_process-api');
373
+ const {
374
+ output,
375
+ ...rest
376
+ } = content; // eslint-disable-line no-unused-vars
397
377
 
398
- function getActivityById(childId) {
399
- if (execution) return execution.getActivityById(childId);
400
- return context.getActivityById(childId);
401
- }
378
+ this._publishEvent('leave', rest);
402
379
 
403
- function getActivities() {
404
- if (execution) return execution.getActivities();
405
- return context.getActivities(id);
380
+ break;
381
+ }
406
382
  }
407
383
 
408
- function getStartActivities(filterOptions) {
409
- return context.getStartActivities(filterOptions, id);
410
- }
384
+ message.ack();
385
+ };
411
386
 
412
- function getSequenceFlows() {
413
- if (execution) return execution.getSequenceFlows();
414
- return context.getSequenceFlows();
415
- }
387
+ proto._onResumeMessage = function onResumeMessage(message) {
388
+ message.ack();
389
+ const stateMessage = this[kStateMessage];
390
+
391
+ switch (stateMessage.fields.routingKey) {
392
+ case 'run.enter':
393
+ case 'run.start':
394
+ case 'run.discarded':
395
+ case 'run.end':
396
+ case 'run.leave':
397
+ break;
416
398
 
417
- function getPostponed(...args) {
418
- if (execution) return execution.getPostponed(...args);
419
- return [];
399
+ default:
400
+ return;
420
401
  }
421
402
 
422
- function onApiMessage(routingKey, message) {
423
- const messageType = message.properties.type;
403
+ if (!stateMessage.fields.redelivered) return;
424
404
 
425
- switch (messageType) {
426
- case 'stop':
427
- {
428
- if (execution && !execution.completed) return;
429
- onStop();
430
- break;
431
- }
432
- }
433
- }
405
+ this._debug(`resume from ${this.status}`);
406
+
407
+ return this.broker.publish('run', stateMessage.fields.routingKey, (0, _messageHelper.cloneContent)(stateMessage.content), stateMessage.properties);
408
+ };
409
+
410
+ proto._onExecutionMessage = function onExecutionMessage(routingKey, message) {
411
+ const content = message.content;
412
+ const messageType = message.properties.type;
413
+ message.ack();
434
414
 
435
- function onStop() {
436
- stopped = true;
437
- deactivateRunConsumers();
438
- return publishEvent('stop');
415
+ switch (messageType) {
416
+ case 'stopped':
417
+ {
418
+ return this._onStop();
419
+ }
420
+
421
+ case 'error':
422
+ {
423
+ this.broker.publish('run', 'run.error', content);
424
+ this.broker.publish('run', 'run.discarded', content);
425
+ break;
426
+ }
427
+
428
+ case 'discard':
429
+ this.broker.publish('run', 'run.discarded', content);
430
+ break;
431
+
432
+ default:
433
+ {
434
+ this.broker.publish('run', 'run.end', content);
435
+ }
439
436
  }
440
437
 
441
- function createMessage(override = {}) {
442
- return {
443
- id,
444
- type,
445
- name,
446
- parent: { ...parent
447
- },
448
- ...override
449
- };
438
+ const executeMessage = this[kExecuteMessage];
439
+ this[kExecuteMessage] = null;
440
+ executeMessage.ack();
441
+ };
442
+
443
+ proto._publishEvent = function publishEvent(state, content) {
444
+ const eventContent = this._createMessage({ ...content,
445
+ state
446
+ });
447
+
448
+ this.broker.publish('event', `process.${state}`, eventContent, {
449
+ type: state,
450
+ mandatory: state === 'error'
451
+ });
452
+ };
453
+
454
+ proto.sendMessage = function sendMessage(message) {
455
+ const messageContent = message && message.content;
456
+ if (!messageContent) return;
457
+ let targetsFound = false;
458
+
459
+ if (messageContent.target && messageContent.target.id && this.getActivityById(messageContent.target.id)) {
460
+ targetsFound = true;
461
+ } else if (messageContent.message && this.getStartActivities({
462
+ referenceId: messageContent.message.id,
463
+ referenceType: messageContent.message.messageType
464
+ }).length) {
465
+ targetsFound = true;
450
466
  }
451
467
 
452
- function getState() {
453
- return createMessage({
454
- executionId,
455
- status,
456
- stopped,
457
- counters: { ...counters
458
- },
459
- broker: broker.getState(true),
460
- execution: execution && execution.getState()
461
- });
468
+ if (!targetsFound) return;
469
+ if (!this.status) this.run();
470
+ this.getApi().sendApiMessage(message.properties.type || 'message', (0, _messageHelper.cloneContent)(messageContent), {
471
+ delegate: true
472
+ });
473
+ };
474
+
475
+ proto.getActivityById = function getActivityById(childId) {
476
+ const execution = this.execution;
477
+ if (execution) return execution.getActivityById(childId);
478
+ return this.context.getActivityById(childId);
479
+ };
480
+
481
+ proto.getActivities = function getActivities() {
482
+ const execution = this.execution;
483
+ if (execution) return execution.getActivities();
484
+ return this.context.getActivities(this.id);
485
+ };
486
+
487
+ proto.getStartActivities = function getStartActivities(filterOptions) {
488
+ return this.context.getStartActivities(filterOptions, this.id);
489
+ };
490
+
491
+ proto.getSequenceFlows = function getSequenceFlows() {
492
+ const execution = this.execution;
493
+ if (execution) return execution.getSequenceFlows();
494
+ return this.context.getSequenceFlows();
495
+ };
496
+
497
+ proto.getPostponed = function getPostponed(...args) {
498
+ const execution = this.execution;
499
+ if (!execution) return [];
500
+ return execution.getPostponed(...args);
501
+ };
502
+
503
+ proto._onApiMessage = function onApiMessage(routingKey, message) {
504
+ const messageType = message.properties.type;
505
+
506
+ switch (messageType) {
507
+ case 'stop':
508
+ {
509
+ if (this.execution && !this.execution.completed) return;
510
+
511
+ this._onStop();
512
+
513
+ break;
514
+ }
462
515
  }
463
- }
516
+ };
517
+
518
+ proto._onStop = function onStop() {
519
+ this[kStopped] = true;
520
+
521
+ this._deactivateRunConsumers();
522
+
523
+ return this._publishEvent('stop');
524
+ };
525
+
526
+ proto._createMessage = function createMessage(override) {
527
+ return {
528
+ id: this.id,
529
+ type: this.type,
530
+ name: this.name,
531
+ executionId: this.executionId,
532
+ parent: { ...this.parent
533
+ },
534
+ ...override
535
+ };
536
+ };
537
+
538
+ proto._debug = function debug(msg) {
539
+ this.logger.debug(`<${this.id}> ${msg}`);
540
+ };