bpmn-elements 17.3.0 → 18.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/dist/Api.js +83 -0
- package/dist/Context.js +228 -22
- package/dist/Environment.js +111 -31
- package/dist/EventBroker.js +57 -1
- package/dist/Expressions.js +3 -4
- package/dist/MessageFormatter.js +29 -16
- package/dist/Timers.js +13 -9
- package/dist/Tracker.js +1 -0
- package/dist/activity/Activity.js +434 -233
- package/dist/activity/ActivityExecution.js +113 -40
- package/dist/activity/Dummy.js +6 -1
- package/dist/activity/Escalation.js +36 -24
- package/dist/activity/ExecutionScope.js +1 -1
- package/dist/activity/Message.js +36 -24
- package/dist/activity/Signal.js +36 -24
- package/dist/activity/outbound-evaluator.js +1 -1
- package/dist/condition.js +12 -6
- package/dist/constants.js +27 -0
- package/dist/definition/Definition.js +187 -64
- package/dist/definition/DefinitionExecution.js +198 -84
- package/dist/error/BpmnError.js +12 -1
- package/dist/error/Errors.js +50 -9
- package/dist/eventDefinitions/CancelEventDefinition.js +29 -11
- package/dist/eventDefinitions/CompensateEventDefinition.js +51 -31
- package/dist/eventDefinitions/ConditionalEventDefinition.js +21 -9
- package/dist/eventDefinitions/ErrorEventDefinition.js +46 -30
- package/dist/eventDefinitions/EscalationEventDefinition.js +44 -27
- package/dist/eventDefinitions/EventDefinitionExecution.js +30 -23
- package/dist/eventDefinitions/LinkEventDefinition.js +45 -120
- package/dist/eventDefinitions/MessageEventDefinition.js +44 -29
- package/dist/eventDefinitions/SignalEventDefinition.js +46 -31
- package/dist/eventDefinitions/TerminateEventDefinition.js +10 -1
- package/dist/eventDefinitions/TimerEventDefinition.js +57 -37
- package/dist/eventDefinitions/index.js +20 -21
- package/dist/events/BoundaryEvent.js +52 -40
- package/dist/events/EndEvent.js +22 -8
- package/dist/events/IntermediateCatchEvent.js +26 -8
- package/dist/events/IntermediateThrowEvent.js +24 -9
- package/dist/events/StartEvent.js +30 -14
- package/dist/events/index.js +10 -11
- package/dist/flows/Association.js +50 -7
- package/dist/flows/MessageFlow.js +49 -10
- package/dist/flows/SequenceFlow.js +93 -22
- package/dist/flows/index.js +6 -7
- package/dist/gateways/EventBasedGateway.js +29 -15
- package/dist/gateways/ExclusiveGateway.js +20 -5
- package/dist/gateways/InclusiveGateway.js +21 -5
- package/dist/gateways/ParallelGateway.js +253 -15
- package/dist/gateways/index.js +8 -9
- package/dist/getPropertyValue.js +1 -1
- package/dist/index.js +42 -43
- package/dist/io/BpmnIO.js +15 -1
- package/dist/io/EnvironmentDataObject.js +29 -1
- package/dist/io/EnvironmentDataStore.js +24 -1
- package/dist/io/EnvironmentDataStoreReference.js +24 -1
- package/dist/io/InputOutputSpecification.js +21 -11
- package/dist/io/Properties.js +28 -17
- package/dist/messageHelper.js +41 -4
- package/dist/process/Lane.js +15 -4
- package/dist/process/Process.js +176 -77
- package/dist/process/ProcessExecution.js +397 -178
- package/dist/shared.js +2 -0
- package/dist/tasks/CallActivity.js +19 -4
- package/dist/tasks/LoopCharacteristics.js +94 -9
- package/dist/tasks/ReceiveTask.js +36 -21
- package/dist/tasks/ScriptTask.js +22 -6
- package/dist/tasks/ServiceImplementation.js +7 -4
- package/dist/tasks/ServiceTask.js +19 -4
- package/dist/tasks/SignalTask.js +19 -4
- package/dist/tasks/StandardLoopCharacteristics.js +8 -4
- package/dist/tasks/SubProcess.js +44 -29
- package/dist/tasks/Task.js +19 -4
- package/dist/tasks/Transaction.js +8 -4
- package/dist/tasks/index.js +16 -18
- package/package.json +31 -13
- package/src/Api.js +70 -0
- package/src/Context.js +200 -19
- package/src/Environment.js +99 -30
- package/src/EventBroker.js +46 -1
- package/src/Expressions.js +2 -3
- package/src/MessageFormatter.js +24 -16
- package/src/Timers.js +12 -9
- package/src/Tracker.js +1 -0
- package/src/activity/Activity.js +372 -218
- package/src/activity/ActivityExecution.js +93 -42
- package/src/activity/Dummy.js +6 -1
- package/src/activity/Escalation.js +25 -18
- package/src/activity/ExecutionScope.js +1 -1
- package/src/activity/Message.js +25 -18
- package/src/activity/Signal.js +25 -18
- package/src/activity/outbound-evaluator.js +1 -1
- package/src/condition.js +11 -5
- package/src/constants.js +21 -0
- package/src/definition/Definition.js +165 -63
- package/src/definition/DefinitionExecution.js +164 -85
- package/src/error/BpmnError.js +11 -1
- package/src/error/Errors.js +44 -5
- package/src/eventDefinitions/CancelEventDefinition.js +27 -13
- package/src/eventDefinitions/CompensateEventDefinition.js +48 -32
- package/src/eventDefinitions/ConditionalEventDefinition.js +20 -10
- package/src/eventDefinitions/ErrorEventDefinition.js +44 -33
- package/src/eventDefinitions/EscalationEventDefinition.js +39 -26
- package/src/eventDefinitions/EventDefinitionExecution.js +30 -24
- package/src/eventDefinitions/LinkEventDefinition.js +34 -120
- package/src/eventDefinitions/MessageEventDefinition.js +42 -31
- package/src/eventDefinitions/SignalEventDefinition.js +43 -32
- package/src/eventDefinitions/TerminateEventDefinition.js +9 -1
- package/src/eventDefinitions/TimerEventDefinition.js +53 -35
- package/src/eventDefinitions/index.js +10 -23
- package/src/events/BoundaryEvent.js +50 -39
- package/src/events/EndEvent.js +19 -7
- package/src/events/IntermediateCatchEvent.js +24 -8
- package/src/events/IntermediateThrowEvent.js +24 -8
- package/src/events/StartEvent.js +25 -14
- package/src/events/index.js +5 -18
- package/src/flows/Association.js +43 -9
- package/src/flows/MessageFlow.js +41 -10
- package/src/flows/SequenceFlow.js +82 -19
- package/src/flows/index.js +3 -4
- package/src/gateways/EventBasedGateway.js +27 -15
- package/src/gateways/ExclusiveGateway.js +16 -3
- package/src/gateways/InclusiveGateway.js +16 -3
- package/src/gateways/ParallelGateway.js +301 -10
- package/src/gateways/index.js +4 -4
- package/src/getPropertyValue.js +1 -1
- package/src/index.js +19 -19
- package/src/io/BpmnIO.js +13 -1
- package/src/io/EnvironmentDataObject.js +26 -1
- package/src/io/EnvironmentDataStore.js +22 -1
- package/src/io/EnvironmentDataStoreReference.js +22 -1
- package/src/io/InputOutputSpecification.js +17 -8
- package/src/io/Properties.js +23 -13
- package/src/messageHelper.js +36 -4
- package/src/process/Lane.js +14 -4
- package/src/process/Process.js +157 -74
- package/src/process/ProcessExecution.js +363 -176
- package/src/shared.js +1 -0
- package/src/tasks/CallActivity.js +16 -2
- package/src/tasks/LoopCharacteristics.js +77 -11
- package/src/tasks/ReceiveTask.js +33 -22
- package/src/tasks/ScriptTask.js +17 -3
- package/src/tasks/ServiceImplementation.js +6 -3
- package/src/tasks/ServiceTask.js +16 -2
- package/src/tasks/SignalTask.js +16 -2
- package/src/tasks/StandardLoopCharacteristics.js +7 -3
- package/src/tasks/SubProcess.js +37 -23
- package/src/tasks/Task.js +16 -2
- package/src/tasks/Transaction.js +7 -3
- package/src/tasks/index.js +8 -9
- package/types/bundle-errors.d.ts +1 -0
- package/types/bundle.d.ts +97 -0
- package/types/index.d.ts +2619 -84
- package/types/interfaces.d.ts +638 -0
- package/types/types.d.ts +0 -765
|
@@ -1,50 +1,57 @@
|
|
|
1
|
-
import DefinitionExecution from './DefinitionExecution.js';
|
|
1
|
+
import { DefinitionExecution } from './DefinitionExecution.js';
|
|
2
2
|
import { DefinitionApi } from '../Api.js';
|
|
3
3
|
import { DefinitionBroker } from '../EventBroker.js';
|
|
4
4
|
import { getUniqueId, getOptionsAndCallback } from '../shared.js';
|
|
5
5
|
import { makeErrorFromMessage } from '../error/Errors.js';
|
|
6
6
|
import { cloneMessage, cloneContent } from '../messageHelper.js';
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
7
|
+
import {
|
|
8
|
+
K_CONSUMING,
|
|
9
|
+
K_COUNTERS,
|
|
10
|
+
K_EXECUTE_MESSAGE,
|
|
11
|
+
K_EXECUTION,
|
|
12
|
+
K_MESSAGE_HANDLERS,
|
|
13
|
+
K_STATE_MESSAGE,
|
|
14
|
+
K_STATUS,
|
|
15
|
+
K_STOPPED,
|
|
16
|
+
STATE_VERSION,
|
|
17
|
+
} from '../constants.js';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Top-level wrapper for an executable BPMN definition. Owns its DefinitionExecution and
|
|
21
|
+
* mediates inter-process messaging.
|
|
22
|
+
* @param {import('../Context.js').ContextInstance} context
|
|
23
|
+
* @param {import('#types').EnvironmentOptions} [options] When provided, environment is cloned and settings merged
|
|
24
|
+
*/
|
|
19
25
|
export function Definition(context, options) {
|
|
20
|
-
if (!(this instanceof Definition)) return new Definition(context, options);
|
|
21
26
|
if (!context) throw new Error('No context');
|
|
22
27
|
|
|
23
28
|
const { id, name, type = 'definition' } = context;
|
|
24
29
|
|
|
25
30
|
this.id = id;
|
|
31
|
+
/** @type {string} */
|
|
26
32
|
this.type = type;
|
|
27
33
|
this.name = name;
|
|
28
34
|
|
|
29
|
-
|
|
35
|
+
/** @type {import('../Environment.js').Environment} */
|
|
36
|
+
this.environment = undefined;
|
|
30
37
|
if (options) {
|
|
31
|
-
|
|
32
|
-
this.context = context.clone(environment);
|
|
38
|
+
this.environment = context.environment.clone(options).assignSettings(options.settings);
|
|
39
|
+
this.context = context.clone(this.environment);
|
|
33
40
|
} else {
|
|
34
|
-
|
|
41
|
+
this.environment = context.environment;
|
|
35
42
|
this.context = context;
|
|
36
43
|
}
|
|
37
44
|
|
|
38
|
-
this[
|
|
45
|
+
this[K_COUNTERS] = {
|
|
39
46
|
completed: 0,
|
|
40
47
|
discarded: 0,
|
|
41
48
|
};
|
|
42
49
|
|
|
43
|
-
this[
|
|
44
|
-
this[
|
|
50
|
+
this[K_STOPPED] = false;
|
|
51
|
+
this[K_EXECUTION] = new Map();
|
|
45
52
|
|
|
46
53
|
const onBrokerReturn = this._onBrokerReturnFn.bind(this);
|
|
47
|
-
this[
|
|
54
|
+
this[K_MESSAGE_HANDLERS] = {
|
|
48
55
|
onBrokerReturn,
|
|
49
56
|
onApiMessage: this._onApiMessage.bind(this),
|
|
50
57
|
onRunMessage: this._onRunMessage.bind(this),
|
|
@@ -60,49 +67,58 @@ export function Definition(context, options) {
|
|
|
60
67
|
this.emit = emit;
|
|
61
68
|
this.emitFatal = emitFatal;
|
|
62
69
|
|
|
63
|
-
|
|
70
|
+
/** @type {import('#types').ILogger} */
|
|
71
|
+
this.logger = this.environment.Logger(type.toLowerCase());
|
|
64
72
|
}
|
|
65
73
|
|
|
66
74
|
Object.defineProperties(Definition.prototype, {
|
|
67
75
|
counters: {
|
|
68
76
|
get() {
|
|
69
|
-
return { ...this[
|
|
77
|
+
return { ...this[K_COUNTERS] };
|
|
70
78
|
},
|
|
71
79
|
},
|
|
72
80
|
execution: {
|
|
73
81
|
get() {
|
|
74
|
-
return this[
|
|
82
|
+
return this[K_EXECUTION].get('execution');
|
|
75
83
|
},
|
|
76
84
|
},
|
|
77
85
|
executionId: {
|
|
78
86
|
get() {
|
|
79
|
-
return this[
|
|
87
|
+
return this[K_EXECUTION].get('executionId');
|
|
80
88
|
},
|
|
81
89
|
},
|
|
82
90
|
isRunning: {
|
|
83
91
|
get() {
|
|
84
|
-
if (!this[
|
|
92
|
+
if (!this[K_CONSUMING]) return false;
|
|
85
93
|
return !!this.status;
|
|
86
94
|
},
|
|
87
95
|
},
|
|
88
96
|
status: {
|
|
89
97
|
get() {
|
|
90
|
-
return this[
|
|
98
|
+
return this[K_STATUS];
|
|
91
99
|
},
|
|
92
100
|
},
|
|
93
101
|
stopped: {
|
|
94
102
|
get() {
|
|
95
|
-
return this[
|
|
103
|
+
return this[K_STOPPED];
|
|
96
104
|
},
|
|
97
105
|
},
|
|
98
106
|
activityStatus: {
|
|
99
107
|
get() {
|
|
100
|
-
const execution = this[
|
|
108
|
+
const execution = this[K_EXECUTION].get('execution');
|
|
101
109
|
return execution?.activityStatus || 'idle';
|
|
102
110
|
},
|
|
103
111
|
},
|
|
104
112
|
});
|
|
105
113
|
|
|
114
|
+
/**
|
|
115
|
+
* Start running the definition. Accepts run options, a callback, or both.
|
|
116
|
+
* The callback fires once on leave, stop, or error.
|
|
117
|
+
* @param {Record<string, any> | import('#types').runCallback} [optionsOrCallback]
|
|
118
|
+
* @param {import('#types').runCallback} [optionalCallback]
|
|
119
|
+
* @returns {this}
|
|
120
|
+
* @throws {Error} when already running and no callback is supplied
|
|
121
|
+
*/
|
|
106
122
|
Definition.prototype.run = function run(optionsOrCallback, optionalCallback) {
|
|
107
123
|
const [runOptions, callback] = getOptionsAndCallback(optionsOrCallback, optionalCallback);
|
|
108
124
|
if (this.isRunning) {
|
|
@@ -115,7 +131,7 @@ Definition.prototype.run = function run(optionsOrCallback, optionalCallback) {
|
|
|
115
131
|
addConsumerCallbacks(this, callback);
|
|
116
132
|
}
|
|
117
133
|
|
|
118
|
-
const exec = this[
|
|
134
|
+
const exec = this[K_EXECUTION];
|
|
119
135
|
const executionId = getUniqueId(this.id);
|
|
120
136
|
exec.set('executionId', executionId);
|
|
121
137
|
const content = this._createMessage({ ...runOptions });
|
|
@@ -132,6 +148,12 @@ Definition.prototype.run = function run(optionsOrCallback, optionalCallback) {
|
|
|
132
148
|
return this;
|
|
133
149
|
};
|
|
134
150
|
|
|
151
|
+
/**
|
|
152
|
+
* Resume after recover by republishing the last run message. The callback fires once on
|
|
153
|
+
* leave, stop, or error.
|
|
154
|
+
* @param {import('#types').runCallback} [callback]
|
|
155
|
+
* @returns {this}
|
|
156
|
+
*/
|
|
135
157
|
Definition.prototype.resume = function resume(callback) {
|
|
136
158
|
if (this.isRunning) {
|
|
137
159
|
const err = new Error('cannot resume running definition');
|
|
@@ -139,7 +161,7 @@ Definition.prototype.resume = function resume(callback) {
|
|
|
139
161
|
throw err;
|
|
140
162
|
}
|
|
141
163
|
|
|
142
|
-
this[
|
|
164
|
+
this[K_STOPPED] = false;
|
|
143
165
|
if (!this.status) return this;
|
|
144
166
|
|
|
145
167
|
if (callback) {
|
|
@@ -154,8 +176,13 @@ Definition.prototype.resume = function resume(callback) {
|
|
|
154
176
|
return this;
|
|
155
177
|
};
|
|
156
178
|
|
|
179
|
+
/**
|
|
180
|
+
* Snapshot definition state for recover.
|
|
181
|
+
* @returns {import('#types').DefinitionState}
|
|
182
|
+
*/
|
|
157
183
|
Definition.prototype.getState = function getState() {
|
|
158
184
|
return this._createMessage({
|
|
185
|
+
stateVersion: STATE_VERSION,
|
|
159
186
|
status: this.status,
|
|
160
187
|
stopped: this.stopped,
|
|
161
188
|
counters: this.counters,
|
|
@@ -165,23 +192,34 @@ Definition.prototype.getState = function getState() {
|
|
|
165
192
|
});
|
|
166
193
|
};
|
|
167
194
|
|
|
195
|
+
/**
|
|
196
|
+
* Restore definition state captured by getState.
|
|
197
|
+
* @param {import('#types').DefinitionState} [state]
|
|
198
|
+
* @returns {this}
|
|
199
|
+
* @throws {Error} when called on a running definition
|
|
200
|
+
*/
|
|
168
201
|
Definition.prototype.recover = function recover(state) {
|
|
169
202
|
if (this.isRunning) throw new Error('cannot recover running definition');
|
|
170
203
|
if (!state) return this;
|
|
171
204
|
|
|
172
|
-
|
|
173
|
-
|
|
205
|
+
const recoveredVersion = state.stateVersion || 0;
|
|
206
|
+
if (recoveredVersion !== STATE_VERSION) {
|
|
207
|
+
this.logger.debug(`<${this.id}> recover state version ${recoveredVersion} into runtime state version ${STATE_VERSION}`);
|
|
208
|
+
}
|
|
174
209
|
|
|
175
|
-
|
|
210
|
+
this[K_STOPPED] = !!state.stopped;
|
|
211
|
+
this[K_STATUS] = state.status;
|
|
212
|
+
|
|
213
|
+
const exec = this[K_EXECUTION];
|
|
176
214
|
exec.set('executionId', state.executionId);
|
|
177
215
|
if (state.counters) {
|
|
178
|
-
this[
|
|
216
|
+
this[K_COUNTERS] = { ...this[K_COUNTERS], ...state.counters };
|
|
179
217
|
}
|
|
180
218
|
|
|
181
219
|
this.environment.recover(state.environment);
|
|
182
220
|
|
|
183
221
|
if (state.execution) {
|
|
184
|
-
exec.set('execution', new DefinitionExecution(this, this.context).recover(state.execution));
|
|
222
|
+
exec.set('execution', new DefinitionExecution(this, this.context).recover(state.execution, recoveredVersion));
|
|
185
223
|
}
|
|
186
224
|
|
|
187
225
|
this.broker.recover(state.broker);
|
|
@@ -189,7 +227,14 @@ Definition.prototype.recover = function recover(state) {
|
|
|
189
227
|
return this;
|
|
190
228
|
};
|
|
191
229
|
|
|
230
|
+
/**
|
|
231
|
+
* Walk activity graphs to discover sequences. Limited to the activity's owning process
|
|
232
|
+
* when startId is given, otherwise all processes are shaken.
|
|
233
|
+
* @param {string} [startId]
|
|
234
|
+
* @returns {import('#types').ShakeResult | undefined}
|
|
235
|
+
*/
|
|
192
236
|
Definition.prototype.shake = function shake(startId) {
|
|
237
|
+
/** @type {import('#types').ShakeResult} */
|
|
193
238
|
let result = {};
|
|
194
239
|
let bps;
|
|
195
240
|
if (startId) {
|
|
@@ -207,6 +252,7 @@ Definition.prototype.shake = function shake(startId) {
|
|
|
207
252
|
return result;
|
|
208
253
|
};
|
|
209
254
|
|
|
255
|
+
/** @internal */
|
|
210
256
|
Definition.prototype._shakeProcess = function shakeProcess(shakeBp, startId) {
|
|
211
257
|
let shovel;
|
|
212
258
|
if (!shakeBp.isRunning) {
|
|
@@ -229,28 +275,44 @@ Definition.prototype._shakeProcess = function shakeProcess(shakeBp, startId) {
|
|
|
229
275
|
return shakeResult;
|
|
230
276
|
};
|
|
231
277
|
|
|
278
|
+
/**
|
|
279
|
+
* Get every process in the definition.
|
|
280
|
+
*/
|
|
232
281
|
Definition.prototype.getProcesses = function getProcesses() {
|
|
233
282
|
const execution = this.execution;
|
|
234
283
|
if (execution) return execution.getProcesses();
|
|
235
284
|
return this.context.getProcesses();
|
|
236
285
|
};
|
|
237
286
|
|
|
287
|
+
/**
|
|
288
|
+
* Get processes flagged executable in the definition.
|
|
289
|
+
*/
|
|
238
290
|
Definition.prototype.getExecutableProcesses = function getExecutableProcesses() {
|
|
239
291
|
const execution = this.execution;
|
|
240
292
|
if (execution) return execution.getExecutableProcesses();
|
|
241
293
|
return this.context.getExecutableProcesses();
|
|
242
294
|
};
|
|
243
295
|
|
|
296
|
+
/**
|
|
297
|
+
* Get processes that are currently running.
|
|
298
|
+
*/
|
|
244
299
|
Definition.prototype.getRunningProcesses = function getRunningProcesses() {
|
|
245
300
|
const execution = this.execution;
|
|
246
301
|
if (!execution) return [];
|
|
247
302
|
return execution.getRunningProcesses();
|
|
248
303
|
};
|
|
249
304
|
|
|
305
|
+
/**
|
|
306
|
+
* @param {string} processId
|
|
307
|
+
*/
|
|
250
308
|
Definition.prototype.getProcessById = function getProcessById(processId) {
|
|
251
309
|
return this.getProcesses().find((p) => p.id === processId);
|
|
252
310
|
};
|
|
253
311
|
|
|
312
|
+
/**
|
|
313
|
+
* Find an activity by id across all processes in the definition.
|
|
314
|
+
* @param {string} childId
|
|
315
|
+
*/
|
|
254
316
|
Definition.prototype.getActivityById = function getActivityById(childId) {
|
|
255
317
|
const bps = this.getProcesses();
|
|
256
318
|
for (const bp of bps) {
|
|
@@ -260,32 +322,57 @@ Definition.prototype.getActivityById = function getActivityById(childId) {
|
|
|
260
322
|
return null;
|
|
261
323
|
};
|
|
262
324
|
|
|
325
|
+
/**
|
|
326
|
+
* Lookup any element (activity, flow, etc.) in the parsed definition by id.
|
|
327
|
+
* @param {string} elementId
|
|
328
|
+
*/
|
|
263
329
|
Definition.prototype.getElementById = function getElementById(elementId) {
|
|
264
330
|
return this.context.getActivityById(elementId);
|
|
265
331
|
};
|
|
266
332
|
|
|
333
|
+
/**
|
|
334
|
+
* List currently postponed activities as Api wrappers.
|
|
335
|
+
* @param {import('#types').filterPostponed} [filterFn]
|
|
336
|
+
*/
|
|
267
337
|
Definition.prototype.getPostponed = function getPostponed(...args) {
|
|
268
|
-
|
|
269
|
-
if (!execution) return [];
|
|
270
|
-
return execution.getPostponed(...args);
|
|
338
|
+
return this.execution?.getPostponed(...args) || [];
|
|
271
339
|
};
|
|
272
340
|
|
|
341
|
+
/**
|
|
342
|
+
* Resolve a Definition Api wrapper, preferring the running execution if any.
|
|
343
|
+
* @param {import('#types').ElementBrokerMessage} [message]
|
|
344
|
+
* @returns {import('#types').IApi<this>}
|
|
345
|
+
* @throws {Error} when the definition is not running and no message is given
|
|
346
|
+
*/
|
|
273
347
|
Definition.prototype.getApi = function getApi(message) {
|
|
274
348
|
const execution = this.execution;
|
|
275
349
|
if (execution) return execution.getApi(message);
|
|
276
|
-
message = message || this[
|
|
350
|
+
message = message || this[K_STATE_MESSAGE];
|
|
277
351
|
if (!message) throw new Error('Definition is not running');
|
|
278
352
|
return DefinitionApi(this.broker, message);
|
|
279
353
|
};
|
|
280
354
|
|
|
355
|
+
/**
|
|
356
|
+
* Send a delegated signal to the running definition.
|
|
357
|
+
* @param {import('#types').signalMessage} [message]
|
|
358
|
+
*/
|
|
281
359
|
Definition.prototype.signal = function signal(message) {
|
|
282
360
|
return this.getApi().signal(message, { delegate: true });
|
|
283
361
|
};
|
|
284
362
|
|
|
363
|
+
/**
|
|
364
|
+
* Cancel a running activity inside the definition by delegated api message.
|
|
365
|
+
* @param {import('#types').signalMessage} [message]
|
|
366
|
+
*/
|
|
285
367
|
Definition.prototype.cancelActivity = function cancelActivity(message) {
|
|
286
368
|
return this.getApi().cancel(message, { delegate: true });
|
|
287
369
|
};
|
|
288
370
|
|
|
371
|
+
/**
|
|
372
|
+
* Deliver a message to a referenced element. Resolves the message reference when the
|
|
373
|
+
* target element exposes a `resolve` method (e.g. message-, signal-, escalation events).
|
|
374
|
+
* @param {{ id?: string, [x: string]: any }} message
|
|
375
|
+
*/
|
|
289
376
|
Definition.prototype.sendMessage = function sendMessage(message) {
|
|
290
377
|
const messageContent = { message };
|
|
291
378
|
let messageType = 'message';
|
|
@@ -299,15 +386,19 @@ Definition.prototype.sendMessage = function sendMessage(message) {
|
|
|
299
386
|
return this.getApi().sendApiMessage(messageType, messageContent, { delegate: true });
|
|
300
387
|
};
|
|
301
388
|
|
|
389
|
+
/**
|
|
390
|
+
* Stop the definition if running.
|
|
391
|
+
*/
|
|
302
392
|
Definition.prototype.stop = function stop() {
|
|
303
393
|
if (!this.isRunning) return;
|
|
304
394
|
this.getApi().stop();
|
|
305
395
|
};
|
|
306
396
|
|
|
397
|
+
/** @internal */
|
|
307
398
|
Definition.prototype._activateRunConsumers = function activateRunConsumers() {
|
|
308
|
-
this[
|
|
399
|
+
this[K_CONSUMING] = true;
|
|
309
400
|
const broker = this.broker;
|
|
310
|
-
const { onApiMessage, onRunMessage } = this[
|
|
401
|
+
const { onApiMessage, onRunMessage } = this[K_MESSAGE_HANDLERS];
|
|
311
402
|
broker.subscribeTmp('api', `definition.*.${this.executionId}`, onApiMessage, {
|
|
312
403
|
noAck: true,
|
|
313
404
|
consumerTag: '_definition-api',
|
|
@@ -318,14 +409,16 @@ Definition.prototype._activateRunConsumers = function activateRunConsumers() {
|
|
|
318
409
|
});
|
|
319
410
|
};
|
|
320
411
|
|
|
412
|
+
/** @internal */
|
|
321
413
|
Definition.prototype._deactivateRunConsumers = function deactivateRunConsumers() {
|
|
322
414
|
const broker = this.broker;
|
|
323
415
|
broker.cancel('_definition-api');
|
|
324
416
|
broker.cancel('_definition-run');
|
|
325
417
|
broker.cancel('_definition-execution');
|
|
326
|
-
this[
|
|
418
|
+
this[K_CONSUMING] = false;
|
|
327
419
|
};
|
|
328
420
|
|
|
421
|
+
/** @internal */
|
|
329
422
|
Definition.prototype._createMessage = function createMessage(override) {
|
|
330
423
|
return {
|
|
331
424
|
id: this.id,
|
|
@@ -336,20 +429,21 @@ Definition.prototype._createMessage = function createMessage(override) {
|
|
|
336
429
|
};
|
|
337
430
|
};
|
|
338
431
|
|
|
432
|
+
/** @internal */
|
|
339
433
|
Definition.prototype._onRunMessage = function onRunMessage(routingKey, message) {
|
|
340
434
|
const { content, fields } = message;
|
|
341
435
|
if (routingKey === 'run.resume') {
|
|
342
436
|
return this._onResumeMessage(message);
|
|
343
437
|
}
|
|
344
438
|
|
|
345
|
-
const exec = this[
|
|
346
|
-
this[
|
|
439
|
+
const exec = this[K_EXECUTION];
|
|
440
|
+
this[K_STATE_MESSAGE] = message;
|
|
347
441
|
|
|
348
442
|
switch (routingKey) {
|
|
349
443
|
case 'run.enter': {
|
|
350
444
|
this.logger.debug(`<${this.executionId} (${this.id})> enter`);
|
|
351
445
|
|
|
352
|
-
this[
|
|
446
|
+
this[K_STATUS] = 'entered';
|
|
353
447
|
if (fields.redelivered) break;
|
|
354
448
|
|
|
355
449
|
exec.delete('execution');
|
|
@@ -358,19 +452,19 @@ Definition.prototype._onRunMessage = function onRunMessage(routingKey, message)
|
|
|
358
452
|
}
|
|
359
453
|
case 'run.start': {
|
|
360
454
|
this.logger.debug(`<${this.executionId} (${this.id})> start`);
|
|
361
|
-
this[
|
|
455
|
+
this[K_STATUS] = 'start';
|
|
362
456
|
this._publishEvent('start', content);
|
|
363
457
|
break;
|
|
364
458
|
}
|
|
365
459
|
case 'run.execute': {
|
|
366
|
-
this[
|
|
460
|
+
this[K_STATUS] = 'executing';
|
|
367
461
|
const executeMessage = cloneMessage(message);
|
|
368
462
|
let execution = exec.get('execution');
|
|
369
463
|
if (fields.redelivered && !execution) {
|
|
370
464
|
executeMessage.fields.redelivered = undefined;
|
|
371
465
|
}
|
|
372
|
-
this[
|
|
373
|
-
this.broker.getQueue('execution-q').assertConsumer(this[
|
|
466
|
+
this[K_EXECUTE_MESSAGE] = message;
|
|
467
|
+
this.broker.getQueue('execution-q').assertConsumer(this[K_MESSAGE_HANDLERS].onExecutionMessage, {
|
|
374
468
|
exclusive: true,
|
|
375
469
|
consumerTag: '_definition-execution',
|
|
376
470
|
});
|
|
@@ -387,12 +481,12 @@ Definition.prototype._onRunMessage = function onRunMessage(routingKey, message)
|
|
|
387
481
|
return execution.execute(executeMessage);
|
|
388
482
|
}
|
|
389
483
|
case 'run.end': {
|
|
390
|
-
if (this[
|
|
484
|
+
if (this[K_STATUS] === 'end') break;
|
|
391
485
|
|
|
392
|
-
this[
|
|
486
|
+
this[K_COUNTERS].completed++;
|
|
393
487
|
|
|
394
488
|
this.logger.debug(`<${this.executionId} (${this.id})> completed`);
|
|
395
|
-
this[
|
|
489
|
+
this[K_STATUS] = 'end';
|
|
396
490
|
this.broker.publish('run', 'run.leave', content);
|
|
397
491
|
this._publishEvent('end', content);
|
|
398
492
|
break;
|
|
@@ -409,17 +503,17 @@ Definition.prototype._onRunMessage = function onRunMessage(routingKey, message)
|
|
|
409
503
|
break;
|
|
410
504
|
}
|
|
411
505
|
case 'run.discarded': {
|
|
412
|
-
if (this[
|
|
506
|
+
if (this[K_STATUS] === 'discarded') break;
|
|
413
507
|
|
|
414
|
-
this[
|
|
508
|
+
this[K_COUNTERS].discarded++;
|
|
415
509
|
|
|
416
|
-
this[
|
|
510
|
+
this[K_STATUS] = 'discarded';
|
|
417
511
|
this.broker.publish('run', 'run.leave', content);
|
|
418
512
|
break;
|
|
419
513
|
}
|
|
420
514
|
case 'run.leave': {
|
|
421
515
|
message.ack();
|
|
422
|
-
this[
|
|
516
|
+
this[K_STATUS] = undefined;
|
|
423
517
|
this._deactivateRunConsumers();
|
|
424
518
|
|
|
425
519
|
this._publishEvent('leave', this._createMessage());
|
|
@@ -430,10 +524,11 @@ Definition.prototype._onRunMessage = function onRunMessage(routingKey, message)
|
|
|
430
524
|
message.ack();
|
|
431
525
|
};
|
|
432
526
|
|
|
527
|
+
/** @internal */
|
|
433
528
|
Definition.prototype._onResumeMessage = function onResumeMessage(message) {
|
|
434
529
|
message.ack();
|
|
435
530
|
|
|
436
|
-
const stateMessage = this[
|
|
531
|
+
const stateMessage = this[K_STATE_MESSAGE];
|
|
437
532
|
|
|
438
533
|
switch (stateMessage.fields.routingKey) {
|
|
439
534
|
case 'run.discarded':
|
|
@@ -451,6 +546,7 @@ Definition.prototype._onResumeMessage = function onResumeMessage(message) {
|
|
|
451
546
|
return this.broker.publish('run', stateMessage.fields.routingKey, cloneContent(stateMessage.content), stateMessage.properties);
|
|
452
547
|
};
|
|
453
548
|
|
|
549
|
+
/** @internal */
|
|
454
550
|
Definition.prototype._onExecutionMessage = function onExecutionMessage(routingKey, message) {
|
|
455
551
|
const { content, properties } = message;
|
|
456
552
|
const messageType = properties.type;
|
|
@@ -471,11 +567,12 @@ Definition.prototype._onExecutionMessage = function onExecutionMessage(routingKe
|
|
|
471
567
|
}
|
|
472
568
|
}
|
|
473
569
|
|
|
474
|
-
const executeMessage = this[
|
|
475
|
-
this[
|
|
570
|
+
const executeMessage = this[K_EXECUTE_MESSAGE];
|
|
571
|
+
this[K_EXECUTE_MESSAGE] = null;
|
|
476
572
|
executeMessage.ack();
|
|
477
573
|
};
|
|
478
574
|
|
|
575
|
+
/** @internal */
|
|
479
576
|
Definition.prototype._onApiMessage = function onApiMessage(routingKey, message) {
|
|
480
577
|
if (message.properties.type === 'stop') {
|
|
481
578
|
const execution = this.execution;
|
|
@@ -485,6 +582,7 @@ Definition.prototype._onApiMessage = function onApiMessage(routingKey, message)
|
|
|
485
582
|
}
|
|
486
583
|
};
|
|
487
584
|
|
|
585
|
+
/** @internal */
|
|
488
586
|
Definition.prototype._publishEvent = function publishEvent(action, content, msgOpts) {
|
|
489
587
|
const execution = this.execution;
|
|
490
588
|
this.broker.publish('event', `definition.${action}`, execution ? execution._createMessage(content) : cloneContent(content), {
|
|
@@ -493,12 +591,14 @@ Definition.prototype._publishEvent = function publishEvent(action, content, msgO
|
|
|
493
591
|
});
|
|
494
592
|
};
|
|
495
593
|
|
|
594
|
+
/** @internal */
|
|
496
595
|
Definition.prototype._onStop = function onStop() {
|
|
497
|
-
this[
|
|
596
|
+
this[K_STOPPED] = true;
|
|
498
597
|
this._deactivateRunConsumers();
|
|
499
598
|
return this._publishEvent('stop', this._createMessage());
|
|
500
599
|
};
|
|
501
600
|
|
|
601
|
+
/** @internal */
|
|
502
602
|
Definition.prototype._onBrokerReturnFn = function onBrokerReturn(message) {
|
|
503
603
|
if (message.properties.type === 'error') {
|
|
504
604
|
this._deactivateRunConsumers();
|
|
@@ -507,13 +607,15 @@ Definition.prototype._onBrokerReturnFn = function onBrokerReturn(message) {
|
|
|
507
607
|
}
|
|
508
608
|
};
|
|
509
609
|
|
|
610
|
+
/** @internal */
|
|
510
611
|
Definition.prototype._reset = function reset() {
|
|
511
|
-
this[
|
|
612
|
+
this[K_EXECUTION].delete('executionId');
|
|
512
613
|
this._deactivateRunConsumers();
|
|
513
614
|
this.broker.purgeQueue('run-q');
|
|
514
615
|
this.broker.purgeQueue('execution-q');
|
|
515
616
|
};
|
|
516
617
|
|
|
618
|
+
/** @internal */
|
|
517
619
|
Definition.prototype._debug = function debug(msg) {
|
|
518
620
|
this.logger.debug(`<${this.id}> ${msg}`);
|
|
519
621
|
};
|