bpmn-elements 5.1.3 → 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 (119) hide show
  1. package/CHANGELOG.md +322 -0
  2. package/README.md +9 -3
  3. package/dist/index.js +71 -39
  4. package/dist/src/Api.js +77 -76
  5. package/dist/src/Context.js +169 -164
  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/Timers.js +4 -6
  12. package/dist/src/activity/Activity.js +1108 -901
  13. package/dist/src/activity/ActivityExecution.js +342 -297
  14. package/dist/src/activity/Dummy.js +3 -3
  15. package/dist/src/definition/Definition.js +498 -444
  16. package/dist/src/definition/DefinitionExecution.js +722 -409
  17. package/dist/src/error/Errors.js +17 -7
  18. package/dist/src/eventDefinitions/CancelEventDefinition.js +190 -150
  19. package/dist/src/eventDefinitions/CompensateEventDefinition.js +194 -161
  20. package/dist/src/eventDefinitions/ConditionalEventDefinition.js +197 -135
  21. package/dist/src/eventDefinitions/ErrorEventDefinition.js +207 -165
  22. package/dist/src/eventDefinitions/EscalationEventDefinition.js +175 -141
  23. package/dist/src/eventDefinitions/EventDefinitionExecution.js +157 -129
  24. package/dist/src/eventDefinitions/LinkEventDefinition.js +174 -149
  25. package/dist/src/eventDefinitions/MessageEventDefinition.js +213 -176
  26. package/dist/src/eventDefinitions/SignalEventDefinition.js +203 -161
  27. package/dist/src/eventDefinitions/TerminateEventDefinition.js +21 -23
  28. package/dist/src/eventDefinitions/TimerEventDefinition.js +243 -228
  29. package/dist/src/events/BoundaryEvent.js +180 -144
  30. package/dist/src/events/EndEvent.js +18 -23
  31. package/dist/src/events/IntermediateCatchEvent.js +44 -58
  32. package/dist/src/events/IntermediateThrowEvent.js +18 -23
  33. package/dist/src/events/StartEvent.js +109 -94
  34. package/dist/src/flows/Association.js +94 -101
  35. package/dist/src/flows/MessageFlow.js +86 -103
  36. package/dist/src/flows/SequenceFlow.js +172 -184
  37. package/dist/src/gateways/EventBasedGateway.js +88 -84
  38. package/dist/src/gateways/ExclusiveGateway.js +13 -16
  39. package/dist/src/gateways/InclusiveGateway.js +11 -14
  40. package/dist/src/gateways/ParallelGateway.js +11 -14
  41. package/dist/src/getPropertyValue.js +34 -34
  42. package/dist/src/io/BpmnIO.js +31 -0
  43. package/dist/src/io/EnvironmentDataObject.js +33 -29
  44. package/dist/src/io/EnvironmentDataStore.js +52 -0
  45. package/dist/src/io/EnvironmentDataStoreReference.js +52 -0
  46. package/dist/src/io/InputOutputSpecification.js +177 -168
  47. package/dist/src/io/Properties.js +252 -0
  48. package/dist/src/messageHelper.js +1 -1
  49. package/dist/src/process/Process.js +433 -359
  50. package/dist/src/process/ProcessExecution.js +744 -645
  51. package/dist/src/shared.js +3 -6
  52. package/dist/src/tasks/CallActivity.js +160 -0
  53. package/dist/src/tasks/LoopCharacteristics.js +309 -330
  54. package/dist/src/tasks/ReceiveTask.js +233 -182
  55. package/dist/src/tasks/ScriptTask.js +35 -41
  56. package/dist/src/tasks/ServiceImplementation.js +13 -20
  57. package/dist/src/tasks/ServiceTask.js +82 -75
  58. package/dist/src/tasks/SignalTask.js +97 -93
  59. package/dist/src/tasks/StandardLoopCharacteristics.js +1 -1
  60. package/dist/src/tasks/SubProcess.js +195 -175
  61. package/dist/src/tasks/Task.js +17 -19
  62. package/index.js +8 -0
  63. package/package.json +16 -15
  64. package/src/Api.js +65 -59
  65. package/src/Context.js +142 -132
  66. package/src/Environment.js +88 -100
  67. package/src/EventBroker.js +67 -68
  68. package/src/ExtensionsMapper.js +2 -2
  69. package/src/MessageFormatter.js +132 -74
  70. package/src/Timers.js +4 -4
  71. package/src/activity/Activity.js +916 -757
  72. package/src/activity/ActivityExecution.js +293 -247
  73. package/src/activity/Dummy.js +2 -2
  74. package/src/definition/Definition.js +436 -401
  75. package/src/definition/DefinitionExecution.js +603 -343
  76. package/src/error/Errors.js +11 -6
  77. package/src/eventDefinitions/CancelEventDefinition.js +164 -121
  78. package/src/eventDefinitions/CompensateEventDefinition.js +158 -124
  79. package/src/eventDefinitions/ConditionalEventDefinition.js +147 -104
  80. package/src/eventDefinitions/ErrorEventDefinition.js +190 -131
  81. package/src/eventDefinitions/EscalationEventDefinition.js +139 -101
  82. package/src/eventDefinitions/EventDefinitionExecution.js +127 -95
  83. package/src/eventDefinitions/LinkEventDefinition.js +160 -129
  84. package/src/eventDefinitions/MessageEventDefinition.js +178 -121
  85. package/src/eventDefinitions/SignalEventDefinition.js +162 -106
  86. package/src/eventDefinitions/TerminateEventDefinition.js +19 -19
  87. package/src/eventDefinitions/TimerEventDefinition.js +202 -167
  88. package/src/events/BoundaryEvent.js +156 -115
  89. package/src/events/EndEvent.js +15 -18
  90. package/src/events/IntermediateCatchEvent.js +40 -44
  91. package/src/events/IntermediateThrowEvent.js +15 -18
  92. package/src/events/StartEvent.js +84 -50
  93. package/src/flows/Association.js +98 -113
  94. package/src/flows/MessageFlow.js +81 -97
  95. package/src/flows/SequenceFlow.js +145 -163
  96. package/src/gateways/EventBasedGateway.js +75 -68
  97. package/src/gateways/ExclusiveGateway.js +8 -13
  98. package/src/gateways/InclusiveGateway.js +8 -13
  99. package/src/gateways/ParallelGateway.js +8 -13
  100. package/src/getPropertyValue.js +34 -33
  101. package/src/io/BpmnIO.js +20 -0
  102. package/src/io/EnvironmentDataObject.js +29 -18
  103. package/src/io/EnvironmentDataStore.js +33 -0
  104. package/src/io/EnvironmentDataStoreReference.js +33 -0
  105. package/src/io/InputOutputSpecification.js +154 -157
  106. package/src/io/Properties.js +199 -0
  107. package/src/process/Process.js +374 -333
  108. package/src/process/ProcessExecution.js +606 -554
  109. package/src/shared.js +1 -5
  110. package/src/tasks/CallActivity.js +130 -0
  111. package/src/tasks/LoopCharacteristics.js +290 -289
  112. package/src/tasks/ReceiveTask.js +174 -107
  113. package/src/tasks/ScriptTask.js +27 -30
  114. package/src/tasks/ServiceImplementation.js +13 -18
  115. package/src/tasks/ServiceTask.js +67 -60
  116. package/src/tasks/SignalTask.js +77 -52
  117. package/src/tasks/StandardLoopCharacteristics.js +1 -1
  118. package/src/tasks/SubProcess.js +184 -157
  119. 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 consumingSymbol = Symbol.for('consuming');
24
+ const countersSymbol = Symbol.for('counters');
25
+ const execSymbol = Symbol.for('exec');
26
+ const executeMessageSymbol = Symbol.for('executeMessage');
27
+ const messageHandlersSymbol = Symbol.for('messageHandlers');
28
+ const stateMessageSymbol = Symbol.for('stateMessage');
29
+ const statusSymbol = Symbol.for('status');
30
+ const stoppedSymbol = 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[countersSymbol] = {
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[stoppedSymbol] = false;
60
+ this[execSymbol] = {};
61
+
62
+ const onBrokerReturn = this._onBrokerReturnFn.bind(this);
63
+
64
+ this[messageHandlersSymbol] = {
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[countersSymbol]
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[execSymbol].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[execSymbol].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[consumingSymbol]) 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[statusSymbol];
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[stoppedSymbol];
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[execSymbol];
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[stoppedSymbol] = 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[stoppedSymbol] = !!state.stopped;
200
+ this[statusSymbol] = state.status;
201
+ const exec = this[execSymbol];
202
+ exec.executionId = state.executionId;
203
+
204
+ if (state.counters) {
205
+ this[countersSymbol] = { ...this[countersSymbol],
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 siblings = this.getProcesses();
292
+
293
+ for (const sibling of siblings) {
294
+ const child = sibling.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[stateMessageSymbol];
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[consumingSymbol] = true;
360
+ const broker = this.broker;
361
+ const {
362
+ onApiMessage,
363
+ onRunMessage
364
+ } = this[messageHandlersSymbol];
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[consumingSymbol] = 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[execSymbol];
404
+ this[stateMessageSymbol] = 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[statusSymbol] = '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[statusSymbol] = '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[statusSymbol] = '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[executeMessageSymbol] = message;
439
+ this.broker.getQueue('execution-q').assertConsumer(this[messageHandlersSymbol].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.status === 'end') break;
455
+ this[countersSymbol].completed++;
456
+ this.logger.debug(`<${this.executionId} (${this.id})> completed`);
457
+ this[statusSymbol] = '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.status === 'discarded') break;
479
+ this[countersSymbol].discarded++;
480
+ this[statusSymbol] = '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[statusSymbol] = 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()
469
- });
470
- }
501
+ proto._onResumeMessage = function onResumeMessage(message) {
502
+ message.ack();
503
+ const stateMessage = this[stateMessageSymbol];
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[executeMessageSymbol];
550
+ this[executeMessageSymbol] = 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[stoppedSymbol] = 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[execSymbol].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
  }