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,79 +20,52 @@ var _messageHelper = require("../messageHelper");
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 kMessageHandlers = Symbol.for('messageHandlers');
28
+ const kStateMessage = Symbol.for('stateMessage');
29
+ const kStatus = Symbol.for('status');
30
+ const kStopped = Symbol.for('stopped');
23
31
  var _default = Definition;
24
32
  exports.default = _default;
25
33
 
26
34
  function Definition(context, options) {
35
+ if (!(this instanceof Definition)) return new Definition(context, options);
27
36
  if (!context) throw new Error('No context');
28
37
  const {
29
38
  id,
30
39
  name,
31
40
  type = 'definition'
32
41
  } = context;
33
- let environment = context.environment;
42
+ this.id = id;
43
+ this.type = type;
44
+ this.name = name;
45
+ let environment;
34
46
 
35
47
  if (options) {
36
- environment = environment.clone(options);
37
- context = context.clone(environment);
48
+ environment = this.environment = context.environment.clone(options);
49
+ this.context = context.clone(environment);
50
+ } else {
51
+ environment = this.environment = context.environment;
52
+ this.context = context;
38
53
  }
39
54
 
40
- const logger = environment.Logger(type.toLowerCase());
41
- let execution, executionId, processes, executableProcesses, postponedMessage, stateMessage, stopped, consumingRunQ;
42
- let status;
43
- let counters = {
55
+ this[kCounters] = {
44
56
  completed: 0,
45
57
  discarded: 0
46
58
  };
47
- const definitionApi = {
48
- id,
49
- name,
50
- type,
51
- logger,
52
- context,
53
-
54
- get counters() {
55
- return { ...counters
56
- };
57
- },
58
-
59
- get executionId() {
60
- return executionId;
61
- },
62
-
63
- get status() {
64
- return status;
65
- },
66
-
67
- get execution() {
68
- return execution;
69
- },
70
-
71
- get isRunning() {
72
- if (!consumingRunQ) return false;
73
- return !!status;
74
- },
75
-
76
- get environment() {
77
- return environment;
78
- },
79
-
80
- run,
81
- getApi,
82
- getState,
83
- getActivityById,
84
- getElementById,
85
- getPostponed,
86
- getProcesses,
87
- getExecutableProcesses,
88
- getProcessById,
89
- sendMessage,
90
- recover,
91
- resume,
92
- shake,
93
- signal,
94
- cancelActivity,
95
- stop
59
+ this[kStopped] = false;
60
+ this[kExec] = {};
61
+
62
+ const onBrokerReturn = this._onBrokerReturnFn.bind(this);
63
+
64
+ this[kMessageHandlers] = {
65
+ onBrokerReturn,
66
+ onApiMessage: this._onApiMessage.bind(this),
67
+ onRunMessage: this._onRunMessage.bind(this),
68
+ onExecutionMessage: this._onExecutionMessage.bind(this)
96
69
  };
97
70
  const {
98
71
  broker,
@@ -101,479 +74,560 @@ function Definition(context, options) {
101
74
  waitFor,
102
75
  emit,
103
76
  emitFatal
104
- } = (0, _EventBroker.DefinitionBroker)(definitionApi, onBrokerReturn);
105
- definitionApi.on = on;
106
- definitionApi.once = once;
107
- definitionApi.waitFor = waitFor;
108
- definitionApi.emit = emit;
109
- definitionApi.emitFatal = emitFatal;
110
- const runQ = broker.getQueue('run-q');
111
- const executionQ = broker.getQueue('execution-q');
112
- Object.defineProperty(definitionApi, 'broker', {
113
- enumerable: true,
114
- get: () => broker
115
- });
116
- Object.defineProperty(definitionApi, 'stopped', {
117
- enumerable: true,
118
- get: () => execution && execution.stopped
119
- });
120
- return definitionApi;
121
-
122
- function run(optionsOrCallback, optionalCallback) {
123
- const [runOptions, callback] = (0, _shared.getOptionsAndCallback)(optionsOrCallback, optionalCallback);
77
+ } = (0, _EventBroker.DefinitionBroker)(this, onBrokerReturn);
78
+ this.broker = broker;
79
+ this.on = on;
80
+ this.once = once;
81
+ this.waitFor = waitFor;
82
+ this.emit = emit;
83
+ this.emitFatal = emitFatal;
84
+ this.logger = environment.Logger(type.toLowerCase());
85
+ }
86
+
87
+ const proto = Definition.prototype;
88
+ Object.defineProperty(proto, 'counters', {
89
+ enumerable: true,
90
+
91
+ get() {
92
+ return { ...this[kCounters]
93
+ };
94
+ }
124
95
 
125
- if (definitionApi.isRunning) {
126
- const err = new Error('definition is already running');
127
- if (callback) return callback(err);
128
- throw err;
129
- }
96
+ });
97
+ Object.defineProperty(proto, 'execution', {
98
+ enumerable: true,
130
99
 
131
- addConsumerCallbacks(callback);
132
- executionId = (0, _shared.getUniqueId)(id);
133
- const content = createMessage({ ...runOptions,
134
- executionId
135
- });
136
- broker.publish('run', 'run.enter', content);
137
- broker.publish('run', 'run.start', (0, _messageHelper.cloneContent)(content));
138
- broker.publish('run', 'run.execute', (0, _messageHelper.cloneContent)(content));
139
- logger.debug(`<${executionId} (${id})> run`);
140
- activateRunConsumers();
141
- return definitionApi;
100
+ get() {
101
+ return this[kExec].execution;
142
102
  }
143
103
 
144
- function resume(callback) {
145
- if (definitionApi.isRunning) {
146
- const err = new Error('cannot resume running definition');
147
- if (callback) return callback(err);
148
- throw err;
149
- }
104
+ });
105
+ Object.defineProperty(proto, 'executionId', {
106
+ enumerable: true,
150
107
 
151
- stopped = false;
152
- if (!status) return definitionApi;
153
- addConsumerCallbacks(callback);
154
- logger.debug(`<${executionId} (${id})> resume`);
155
- const content = createMessage({
156
- executionId
157
- });
158
- broker.publish('run', 'run.resume', content, {
159
- persistent: false
160
- });
161
- activateRunConsumers();
162
- return definitionApi;
108
+ get() {
109
+ return this[kExec].executionId;
163
110
  }
164
111
 
165
- function recover(state) {
166
- if (definitionApi.isRunning) throw new Error('cannot recover running definition');
167
- if (!state) return definitionApi;
168
- stopped = state.stopped;
169
- status = state.status;
170
- executionId = state.executionId;
171
-
172
- if (state.counters) {
173
- counters = { ...counters,
174
- ...state.counters
175
- };
176
- }
112
+ });
113
+ Object.defineProperty(proto, 'isRunning', {
114
+ enumerable: true,
177
115
 
178
- environment.recover(state.environment);
116
+ get() {
117
+ if (!this[kConsuming]) return false;
118
+ return !!this.status;
119
+ }
179
120
 
180
- if (state.execution) {
181
- execution = (0, _DefinitionExecution.default)(definitionApi, context).recover(state.execution);
182
- }
121
+ });
122
+ Object.defineProperty(proto, 'status', {
123
+ enumerable: true,
183
124
 
184
- broker.recover(state.broker);
185
- return definitionApi;
125
+ get() {
126
+ return this[kStatus];
186
127
  }
187
128
 
188
- function shake(startId) {
189
- let result = {};
190
- let bps;
129
+ });
130
+ Object.defineProperty(proto, 'stopped', {
131
+ enumerable: true,
191
132
 
192
- if (startId) {
193
- const startActivity = getActivityById(startId);
194
- if (!startActivity) return;
195
- const bp = getProcessById(startActivity.parent.id);
196
- if (!bp) return;
197
- bps = [bp];
198
- } else bps = getProcesses();
133
+ get() {
134
+ return this[kStopped];
135
+ }
199
136
 
200
- bps.forEach(shakeProcess);
201
- return result;
137
+ });
202
138
 
203
- function shakeProcess(shakeBp) {
204
- let shovel;
139
+ proto.run = function run(optionsOrCallback, optionalCallback) {
140
+ const [runOptions, callback] = (0, _shared.getOptionsAndCallback)(optionsOrCallback, optionalCallback);
205
141
 
206
- if (!shakeBp.isRunning) {
207
- shovel = shakeBp.broker.createShovel('shaker', {
208
- exchange: 'event',
209
- pattern: '*.shake#'
210
- }, {
211
- broker,
212
- exchange: 'event'
213
- });
214
- }
142
+ if (this.isRunning) {
143
+ const err = new Error('definition is already running');
144
+ if (callback) return callback(err);
145
+ throw err;
146
+ }
215
147
 
216
- const shakeResult = shakeBp.shake(startId);
217
- if (shovel) shakeBp.broker.closeShovel('shaker');
218
- result = { ...result,
219
- ...shakeResult
220
- };
221
- }
148
+ if (callback) {
149
+ addConsumerCallbacks(this, callback);
222
150
  }
223
151
 
224
- function activateRunConsumers() {
225
- consumingRunQ = true;
226
- broker.subscribeTmp('api', `definition.*.${executionId}`, onApiMessage, {
227
- noAck: true,
228
- consumerTag: '_definition-api'
229
- });
230
- runQ.assertConsumer(onRunMessage, {
231
- exclusive: true,
232
- consumerTag: '_definition-run'
233
- });
152
+ const exec = this[kExec];
153
+ exec.executionId = (0, _shared.getUniqueId)(this.id);
154
+
155
+ const content = this._createMessage({ ...runOptions
156
+ });
157
+
158
+ const broker = this.broker;
159
+ broker.publish('run', 'run.enter', content);
160
+ broker.publish('run', 'run.start', (0, _messageHelper.cloneContent)(content));
161
+ broker.publish('run', 'run.execute', (0, _messageHelper.cloneContent)(content));
162
+ this.logger.debug(`<${this.executionId} (${this.id})> run`);
163
+
164
+ this._activateRunConsumers();
165
+
166
+ return this;
167
+ };
168
+
169
+ proto.resume = function resume(callback) {
170
+ if (this.isRunning) {
171
+ const err = new Error('cannot resume running definition');
172
+ if (callback) return callback(err);
173
+ throw err;
234
174
  }
235
175
 
236
- function deactivateRunConsumers() {
237
- broker.cancel('_definition-api');
238
- broker.cancel('_definition-run');
239
- broker.cancel('_definition-execution');
240
- consumingRunQ = false;
176
+ this[kStopped] = false;
177
+ if (!this.status) return this;
178
+
179
+ if (callback) {
180
+ addConsumerCallbacks(this, callback);
241
181
  }
242
182
 
243
- function stop() {
244
- if (!definitionApi.isRunning) return;
245
- getApi().stop();
183
+ this.logger.debug(`<${this.executionId} (${this.id})> resume`);
184
+
185
+ const content = this._createMessage();
186
+
187
+ this.broker.publish('run', 'run.resume', content, {
188
+ persistent: false
189
+ });
190
+
191
+ this._activateRunConsumers();
192
+
193
+ return this;
194
+ };
195
+
196
+ proto.recover = function recover(state) {
197
+ if (this.isRunning) throw new Error('cannot recover running definition');
198
+ if (!state) return this;
199
+ this[kStopped] = !!state.stopped;
200
+ this[kStatus] = state.status;
201
+ const exec = this[kExec];
202
+ exec.executionId = state.executionId;
203
+
204
+ if (state.counters) {
205
+ this[kCounters] = { ...this[kCounters],
206
+ ...state.counters
207
+ };
246
208
  }
247
209
 
248
- function addConsumerCallbacks(callback) {
249
- if (!callback) return;
250
- broker.off('return', onBrokerReturn);
251
- clearConsumers();
252
- broker.subscribeOnce('event', 'definition.stop', cbLeave, {
253
- consumerTag: '_definition-callback-stop'
254
- });
255
- broker.subscribeOnce('event', 'definition.leave', cbLeave, {
256
- consumerTag: '_definition-callback-leave'
257
- });
258
- broker.subscribeOnce('event', 'definition.error', cbError, {
259
- consumerTag: '_definition-callback-error'
260
- });
261
-
262
- function cbLeave(_, message) {
263
- clearConsumers();
264
- return callback(null, getApi(message));
265
- }
210
+ this.environment.recover(state.environment);
266
211
 
267
- function cbError(_, message) {
268
- clearConsumers();
269
- reset();
270
- const err = (0, _Errors.makeErrorFromMessage)(message);
271
- return callback(err);
272
- }
212
+ if (state.execution) {
213
+ exec.execution = new _DefinitionExecution.default(this, this.context).recover(state.execution);
214
+ }
273
215
 
274
- function clearConsumers() {
275
- broker.cancel('_definition-callback-stop');
276
- broker.cancel('_definition-callback-leave');
277
- broker.cancel('_definition-callback-error');
278
- broker.on('return', onBrokerReturn);
216
+ this.broker.recover(state.broker);
217
+ return this;
218
+ };
219
+
220
+ proto.shake = function shake(startId) {
221
+ let result = {};
222
+ const broker = this.broker;
223
+ let bps;
224
+
225
+ if (startId) {
226
+ const startActivity = this.getActivityById(startId);
227
+ if (!startActivity) return;
228
+ const bp = this.getProcessById(startActivity.parent.id);
229
+ if (!bp) return;
230
+ bps = [bp];
231
+ } else bps = this.getProcesses();
232
+
233
+ bps.forEach(shakeProcess);
234
+ return result;
235
+
236
+ function shakeProcess(shakeBp) {
237
+ let shovel;
238
+
239
+ if (!shakeBp.isRunning) {
240
+ shovel = shakeBp.broker.createShovel('shaker', {
241
+ exchange: 'event',
242
+ pattern: '*.shake#'
243
+ }, {
244
+ broker,
245
+ exchange: 'event'
246
+ });
279
247
  }
248
+
249
+ const shakeResult = shakeBp.shake(startId);
250
+ if (shovel) shakeBp.broker.closeShovel('shaker');
251
+ result = { ...result,
252
+ ...shakeResult
253
+ };
280
254
  }
255
+ };
256
+
257
+ proto.getState = function getState() {
258
+ return this._createMessage({
259
+ status: this.status,
260
+ stopped: this.stopped,
261
+ counters: this.counters,
262
+ environment: this.environment.getState(),
263
+ execution: this.execution && this.execution.getState(),
264
+ broker: this.broker.getState(true)
265
+ });
266
+ };
267
+
268
+ proto.getProcesses = function getProcesses() {
269
+ const execution = this.execution;
270
+ if (execution) return execution.getProcesses();
271
+ return this.context.getProcesses();
272
+ };
273
+
274
+ proto.getExecutableProcesses = function getExecutableProcesses() {
275
+ const execution = this.execution;
276
+ if (execution) return execution.getExecutableProcesses();
277
+ return this.context.getExecutableProcesses();
278
+ };
279
+
280
+ proto.getRunningProcesses = function getRunningProcesses() {
281
+ const execution = this.execution;
282
+ if (!execution) return [];
283
+ return execution.getRunningProcesses();
284
+ };
285
+
286
+ proto.getProcessById = function getProcessById(processId) {
287
+ return this.getProcesses().find(p => p.id === processId);
288
+ };
289
+
290
+ proto.getActivityById = function getActivityById(childId) {
291
+ const bps = this.getProcesses();
292
+
293
+ for (const bp of bps) {
294
+ const child = bp.getActivityById(childId);
295
+ if (child) return child;
296
+ }
297
+
298
+ return null;
299
+ };
300
+
301
+ proto.getElementById = function getElementById(elementId) {
302
+ return this.context.getActivityById(elementId);
303
+ };
304
+
305
+ proto.getPostponed = function getPostponed(...args) {
306
+ const execution = this.execution;
307
+ if (!execution) return [];
308
+ return execution.getPostponed(...args);
309
+ };
310
+
311
+ proto.getApi = function getApi(message) {
312
+ const execution = this.execution;
313
+ if (execution) return execution.getApi(message);
314
+ message = message || this[kStateMessage];
315
+ if (!message) throw new Error('Definition is not running');
316
+ return (0, _Api.DefinitionApi)(this.broker, message);
317
+ };
318
+
319
+ proto.signal = function signal(message) {
320
+ return this.getApi().signal(message, {
321
+ delegate: true
322
+ });
323
+ };
324
+
325
+ proto.cancelActivity = function cancelActivity(message) {
326
+ return this.getApi().cancel(message, {
327
+ delegate: true
328
+ });
329
+ };
330
+
331
+ proto.sendMessage = function sendMessage(message) {
332
+ const messageContent = {
333
+ message
334
+ };
335
+ let messageType = 'message';
336
+ const reference = message && message.id && this.getElementById(message.id);
281
337
 
282
- function createMessage(override) {
283
- return {
284
- id,
285
- type,
286
- name,
287
- ...override
338
+ if (reference && reference.resolve) {
339
+ const resolvedReference = reference.resolve(this._createMessage({
340
+ message
341
+ }));
342
+ messageType = resolvedReference.messageType || messageType;
343
+ messageContent.message = { ...message,
344
+ ...resolvedReference
288
345
  };
289
346
  }
290
347
 
291
- function onBrokerReturn(message) {
292
- if (message.properties.type === 'error') {
293
- deactivateRunConsumers();
294
- const err = (0, _Errors.makeErrorFromMessage)(message);
295
- throw err;
296
- }
348
+ return this.getApi().sendApiMessage(messageType, messageContent, {
349
+ delegate: true
350
+ });
351
+ };
352
+
353
+ proto.stop = function stop() {
354
+ if (!this.isRunning) return;
355
+ this.getApi().stop();
356
+ };
357
+
358
+ proto._activateRunConsumers = function activateRunConsumers() {
359
+ this[kConsuming] = true;
360
+ const broker = this.broker;
361
+ const {
362
+ onApiMessage,
363
+ onRunMessage
364
+ } = this[kMessageHandlers];
365
+ broker.subscribeTmp('api', `definition.*.${this.executionId}`, onApiMessage, {
366
+ noAck: true,
367
+ consumerTag: '_definition-api'
368
+ });
369
+ broker.getQueue('run-q').assertConsumer(onRunMessage, {
370
+ exclusive: true,
371
+ consumerTag: '_definition-run'
372
+ });
373
+ };
374
+
375
+ proto._deactivateRunConsumers = function deactivateRunConsumers() {
376
+ const broker = this.broker;
377
+ broker.cancel('_definition-api');
378
+ broker.cancel('_definition-run');
379
+ broker.cancel('_definition-execution');
380
+ this[kConsuming] = false;
381
+ };
382
+
383
+ proto._createMessage = function createMessage(override) {
384
+ return {
385
+ id: this.id,
386
+ type: this.type,
387
+ name: this.name,
388
+ executionId: this.executionId,
389
+ ...override
390
+ };
391
+ };
392
+
393
+ proto._onRunMessage = function onRunMessage(routingKey, message) {
394
+ const {
395
+ content,
396
+ fields
397
+ } = message;
398
+
399
+ if (routingKey === 'run.resume') {
400
+ return this._onResumeMessage(message);
297
401
  }
298
402
 
299
- function onRunMessage(routingKey, message) {
300
- const {
301
- content,
302
- ack,
303
- fields
304
- } = message;
403
+ const exec = this[kExec];
404
+ this[kStateMessage] = message;
305
405
 
306
- if (routingKey === 'run.resume') {
307
- return onResumeMessage();
308
- }
406
+ switch (routingKey) {
407
+ case 'run.enter':
408
+ {
409
+ this.logger.debug(`<${this.executionId} (${this.id})> enter`);
410
+ this[kStatus] = 'entered';
411
+ if (fields.redelivered) break;
412
+ exec.execution = undefined;
309
413
 
310
- stateMessage = message;
311
-
312
- switch (routingKey) {
313
- case 'run.enter':
314
- {
315
- logger.debug(`<${executionId} (${id})> enter`);
316
- status = 'entered';
317
- if (fields.redelivered) break;
318
- execution = undefined;
319
- publishEvent('enter', content);
320
- break;
321
- }
414
+ this._publishEvent('enter', content);
322
415
 
323
- case 'run.start':
324
- {
325
- logger.debug(`<${executionId} (${id})> start`);
326
- status = 'start';
327
- publishEvent('start', content);
328
- break;
329
- }
416
+ break;
417
+ }
330
418
 
331
- case 'run.execute':
332
- {
333
- status = 'executing';
334
- const executeMessage = (0, _messageHelper.cloneMessage)(message);
419
+ case 'run.start':
420
+ {
421
+ this.logger.debug(`<${this.executionId} (${this.id})> start`);
422
+ this[kStatus] = 'start';
335
423
 
336
- if (fields.redelivered && !execution) {
337
- executeMessage.fields.redelivered = undefined;
338
- }
424
+ this._publishEvent('start', content);
339
425
 
340
- postponedMessage = message;
341
- executionQ.assertConsumer(onExecutionMessage, {
342
- exclusive: true,
343
- consumerTag: '_definition-execution'
344
- });
345
- execution = execution || (0, _DefinitionExecution.default)(definitionApi, context);
426
+ break;
427
+ }
346
428
 
347
- if (executeMessage.fields.redelivered) {
348
- publishEvent('resume', content);
349
- }
429
+ case 'run.execute':
430
+ {
431
+ this[kStatus] = 'executing';
432
+ const executeMessage = (0, _messageHelper.cloneMessage)(message);
350
433
 
351
- return execution.execute(executeMessage);
434
+ if (fields.redelivered && !exec.execution) {
435
+ executeMessage.fields.redelivered = undefined;
352
436
  }
353
437
 
354
- case 'run.end':
355
- {
356
- if (status === 'end') break;
357
- counters.completed++;
358
- logger.debug(`<${executionId} (${id})> completed`);
359
- status = 'end';
360
- broker.publish('run', 'run.leave', content);
361
- publishEvent('end', content);
362
- break;
363
- }
438
+ this[kExecuteMessage] = message;
439
+ this.broker.getQueue('execution-q').assertConsumer(this[kMessageHandlers].onExecutionMessage, {
440
+ exclusive: true,
441
+ consumerTag: '_definition-execution'
442
+ });
443
+ exec.execution = exec.execution || new _DefinitionExecution.default(this, this.context);
364
444
 
365
- case 'run.discarded':
366
- {
367
- if (status === 'discarded') break;
368
- counters.discarded++;
369
- status = 'discarded';
370
- broker.publish('run', 'run.leave', content);
371
- break;
445
+ if (executeMessage.fields.redelivered) {
446
+ this._publishEvent('resume', content);
372
447
  }
373
448
 
374
- case 'run.error':
375
- {
376
- publishEvent('error', (0, _messageHelper.cloneContent)(content, {
377
- error: fields.redelivered ? (0, _Errors.makeErrorFromMessage)(message) : content.error
378
- }), {
379
- mandatory: true
380
- });
381
- break;
382
- }
449
+ return exec.execution.execute(executeMessage);
450
+ }
383
451
 
384
- case 'run.leave':
385
- {
386
- ack();
387
- status = undefined;
388
- deactivateRunConsumers();
389
- publishEvent('leave');
390
- break;
391
- }
392
- }
452
+ case 'run.end':
453
+ {
454
+ if (this[kStatus] === 'end') break;
455
+ this[kCounters].completed++;
456
+ this.logger.debug(`<${this.executionId} (${this.id})> completed`);
457
+ this[kStatus] = 'end';
458
+ this.broker.publish('run', 'run.leave', content);
393
459
 
394
- ack();
460
+ this._publishEvent('end', content);
395
461
 
396
- function onResumeMessage() {
397
- message.ack();
462
+ break;
463
+ }
398
464
 
399
- switch (stateMessage.fields.routingKey) {
400
- case 'run.enter':
401
- case 'run.start':
402
- case 'run.discarded':
403
- case 'run.end':
404
- case 'run.leave':
405
- break;
465
+ case 'run.error':
466
+ {
467
+ this._publishEvent('error', { ...content,
468
+ error: fields.redelivered ? (0, _Errors.makeErrorFromMessage)(message) : content.error
469
+ }, {
470
+ mandatory: true
471
+ });
406
472
 
407
- default:
408
- return;
473
+ break;
409
474
  }
410
475
 
411
- if (!stateMessage.fields.redelivered) return;
412
- logger.debug(`<${id}> resume from ${status}`);
413
- return broker.publish('run', stateMessage.fields.routingKey, (0, _messageHelper.cloneContent)(stateMessage.content), stateMessage.properties);
414
- }
415
- }
476
+ case 'run.discarded':
477
+ {
478
+ if (this[kStatus] === 'discarded') break;
479
+ this[kCounters].discarded++;
480
+ this[kStatus] = 'discarded';
481
+ this.broker.publish('run', 'run.leave', content);
482
+ break;
483
+ }
416
484
 
417
- function onExecutionMessage(routingKey, message) {
418
- const {
419
- content,
420
- properties
421
- } = message;
422
- const messageType = properties.type;
423
- message.ack();
424
-
425
- switch (messageType) {
426
- case 'stopped':
427
- {
428
- deactivateRunConsumers();
429
- return publishEvent('stop');
430
- }
485
+ case 'run.leave':
486
+ {
487
+ message.ack();
488
+ this[kStatus] = undefined;
431
489
 
432
- case 'error':
433
- {
434
- broker.publish('run', 'run.error', content);
435
- broker.publish('run', 'run.discarded', content);
436
- break;
437
- }
490
+ this._deactivateRunConsumers();
438
491
 
439
- default:
440
- {
441
- broker.publish('run', 'run.end', content);
442
- }
443
- }
492
+ this._publishEvent('leave', this._createMessage());
444
493
 
445
- if (postponedMessage) {
446
- const ackMessage = postponedMessage;
447
- postponedMessage = null;
448
- ackMessage.ack();
449
- }
494
+ return;
495
+ }
450
496
  }
451
497
 
452
- function publishEvent(action, content = {}, msgOpts) {
453
- broker.publish('event', `definition.${action}`, execution ? execution.createMessage(content) : content, {
454
- type: action,
455
- ...msgOpts
456
- });
457
- }
498
+ message.ack();
499
+ };
458
500
 
459
- function getState() {
460
- return createMessage({
461
- executionId,
462
- status,
463
- stopped,
464
- counters: { ...counters
465
- },
466
- environment: environment.getState(),
467
- execution: execution && execution.getState(),
468
- broker: broker.getState(true)
469
- });
470
- }
501
+ proto._onResumeMessage = function onResumeMessage(message) {
502
+ message.ack();
503
+ const stateMessage = this[kStateMessage];
471
504
 
472
- function getProcesses() {
473
- if (!processes) loadProcesses();
474
- return processes;
475
- }
505
+ switch (stateMessage.fields.routingKey) {
506
+ case 'run.discarded':
507
+ case 'run.end':
508
+ case 'run.leave':
509
+ break;
476
510
 
477
- function getExecutableProcesses() {
478
- if (!processes) loadProcesses();
479
- return executableProcesses;
511
+ default:
512
+ return;
480
513
  }
481
514
 
482
- function getProcessById(processId) {
483
- return getProcesses().find(p => p.id === processId);
484
- }
515
+ if (!stateMessage.fields.redelivered) return;
485
516
 
486
- function loadProcesses() {
487
- if (processes) return processes;
488
- executableProcesses = context.getExecutableProcesses() || [];
489
- processes = context.getProcesses() || [];
490
- logger.debug(`<${id}> found ${processes.length} processes`);
491
- }
517
+ this._debug(`resume from ${this.status}`);
492
518
 
493
- function getActivityById(childId) {
494
- let child;
495
- const siblings = getProcesses();
519
+ return this.broker.publish('run', stateMessage.fields.routingKey, (0, _messageHelper.cloneContent)(stateMessage.content), stateMessage.properties);
520
+ };
496
521
 
497
- for (let i = 0; i < siblings.length; i++) {
498
- child = siblings[i].getActivityById(childId);
499
- if (child) return child;
500
- }
522
+ proto._onExecutionMessage = function onExecutionMessage(routingKey, message) {
523
+ const {
524
+ content,
525
+ properties
526
+ } = message;
527
+ const messageType = properties.type;
528
+ message.ack();
529
+
530
+ switch (messageType) {
531
+ case 'stopped':
532
+ {
533
+ return this._onStop();
534
+ }
501
535
 
502
- return child;
503
- }
536
+ case 'error':
537
+ {
538
+ this.broker.publish('run', 'run.error', content);
539
+ this.broker.publish('run', 'run.discarded', content);
540
+ break;
541
+ }
504
542
 
505
- function getElementById(elementId) {
506
- return context.getActivityById(elementId);
543
+ default:
544
+ {
545
+ this.broker.publish('run', 'run.end', content);
546
+ }
507
547
  }
508
548
 
509
- function getPostponed(...args) {
510
- if (!execution) return [];
511
- return execution.getPostponed(...args);
512
- }
549
+ const executeMessage = this[kExecuteMessage];
550
+ this[kExecuteMessage] = null;
551
+ executeMessage.ack();
552
+ };
513
553
 
514
- function getApi(message) {
515
- if (execution) return execution.getApi(message);
516
- if (!message || !stateMessage) throw new Error('Definition is not running');
517
- return (0, _Api.DefinitionApi)(broker, message || stateMessage);
518
- }
554
+ proto._onApiMessage = function onApiMessage(routingKey, message) {
555
+ if (message.properties.type === 'stop') {
556
+ const execution = this.execution;
519
557
 
520
- function signal(message) {
521
- return getApi().signal(message, {
522
- delegate: true
523
- });
558
+ if (!execution || execution.completed) {
559
+ this._onStop();
560
+ }
524
561
  }
562
+ };
525
563
 
526
- function cancelActivity(message) {
527
- return getApi().cancel(message, {
528
- delegate: true
529
- });
530
- }
564
+ proto._publishEvent = function publishEvent(action, content, msgOpts) {
565
+ const execution = this.execution;
566
+ this.broker.publish('event', `definition.${action}`, execution ? execution._createMessage(content) : (0, _messageHelper.cloneContent)(content), {
567
+ type: action,
568
+ ...msgOpts
569
+ });
570
+ };
531
571
 
532
- function sendMessage(message) {
533
- const messageContent = {
534
- message
535
- };
536
- let messageType = 'message';
537
- const reference = message && message.id && getElementById(message.id);
538
-
539
- if (reference && reference.resolve) {
540
- const resolvedReference = reference.resolve(createMessage({
541
- message
542
- }));
543
- messageType = resolvedReference.messageType || messageType;
544
- messageContent.message = { ...message,
545
- ...resolvedReference
546
- };
547
- }
572
+ proto._onStop = function onStop() {
573
+ this[kStopped] = true;
574
+
575
+ this._deactivateRunConsumers();
548
576
 
549
- return getApi().sendApiMessage(messageType, messageContent, {
550
- delegate: true
551
- });
577
+ return this._publishEvent('stop', this._createMessage());
578
+ };
579
+
580
+ proto._onBrokerReturnFn = function onBrokerReturn(message) {
581
+ if (message.properties.type === 'error') {
582
+ this._deactivateRunConsumers();
583
+
584
+ const err = (0, _Errors.makeErrorFromMessage)(message);
585
+ throw err;
552
586
  }
587
+ };
553
588
 
554
- function onApiMessage(routingKey, message) {
555
- const messageType = message.properties.type;
589
+ proto._reset = function reset() {
590
+ this[kExec].executionId = undefined;
556
591
 
557
- switch (messageType) {
558
- case 'stop':
559
- {
560
- if (execution && !execution.completed) return;
561
- onStop();
562
- break;
563
- }
564
- }
592
+ this._deactivateRunConsumers();
593
+
594
+ this.broker.purgeQueue('run-q');
595
+ this.broker.purgeQueue('execution-q');
596
+ };
597
+
598
+ proto._debug = function debug(msg) {
599
+ this.logger.debug(`<${this.id}> ${msg}`);
600
+ };
601
+
602
+ function addConsumerCallbacks(definition, callback) {
603
+ const broker = definition.broker;
604
+ clearConsumers();
605
+ broker.subscribeOnce('event', 'definition.stop', cbLeave, {
606
+ consumerTag: '_definition-callback-stop'
607
+ });
608
+ broker.subscribeOnce('event', 'definition.leave', cbLeave, {
609
+ consumerTag: '_definition-callback-leave'
610
+ });
611
+ broker.subscribeOnce('event', 'definition.error', cbError, {
612
+ consumerTag: '_definition-callback-error'
613
+ });
614
+
615
+ function cbLeave(_, message) {
616
+ clearConsumers();
617
+ return callback(null, definition.getApi(message));
565
618
  }
566
619
 
567
- function onStop() {
568
- stopped = true;
569
- deactivateRunConsumers();
570
- return publishEvent('stop');
620
+ function cbError(_, message) {
621
+ clearConsumers();
622
+
623
+ definition._reset();
624
+
625
+ return callback((0, _Errors.makeErrorFromMessage)(message));
571
626
  }
572
627
 
573
- function reset() {
574
- executionId = undefined;
575
- deactivateRunConsumers();
576
- runQ.purge();
577
- executionQ.purge();
628
+ function clearConsumers() {
629
+ broker.cancel('_definition-callback-stop');
630
+ broker.cancel('_definition-callback-leave');
631
+ broker.cancel('_definition-callback-error');
578
632
  }
579
633
  }