bpmn-elements 9.1.2 → 9.1.3

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/CHANGELOG.md CHANGED
@@ -1,6 +1,10 @@
1
1
  Changelog
2
2
  =========
3
3
 
4
+ # 9.1.3
5
+
6
+ - type declare execution scope
7
+
4
8
  # 9.1.2
5
9
 
6
10
  - allow type IScripts.register to return undefined
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bpmn-elements",
3
- "version": "9.1.2",
3
+ "version": "9.1.3",
4
4
  "description": "Executable workflow elements based on BPMN 2.0",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -1,215 +1,215 @@
1
- import Activity from '../activity/Activity.js';
2
- import {cloneContent} from '../messageHelper.js';
3
-
4
- const kCompleted = Symbol.for('completed');
5
- const kExecuteMessage = Symbol.for('executeMessage');
6
- const kReferenceElement = Symbol.for('referenceElement');
7
- const kReferenceInfo = Symbol.for('referenceInfo');
8
-
9
- export default function ReceiveTask(activityDef, context) {
10
- const task = new Activity(ReceiveTaskBehaviour, activityDef, context);
11
-
12
- task.broker.assertQueue('message', {autoDelete: false, durable: true});
13
- task.broker.bindQueue('message', 'api', '*.message.#', {durable: true});
14
-
15
- return task;
16
- }
17
-
18
- export function ReceiveTaskBehaviour(activity) {
19
- const {id, type, behaviour} = activity;
20
-
21
- this.id = id;
22
- this.type = type;
23
-
24
- const reference = this.reference = {
25
- name: 'anonymous',
26
- ...behaviour.messageRef,
27
- referenceType: 'message',
28
- };
29
-
30
- this.loopCharacteristics = behaviour.loopCharacteristics && new behaviour.loopCharacteristics.Behaviour(activity, behaviour.loopCharacteristics);
31
- this.activity = activity;
32
- this.broker = activity.broker;
33
-
34
- this[kReferenceElement] = reference.id && activity.getActivityById(reference.id);
35
- }
36
-
37
- ReceiveTaskBehaviour.prototype.execute = function execute(executeMessage) {
38
- return new ReceiveTaskExecution(this).execute(executeMessage);
39
- };
40
-
41
- function ReceiveTaskExecution(parent) {
42
- const {activity, broker, loopCharacteristics, reference} = parent;
43
-
44
- this.id = activity.id;
45
- this.logger = activity.logger;
46
- this.reference = reference;
47
- this.broker = broker;
48
- this.loopCharacteristics = loopCharacteristics;
49
- this.referenceElement = parent[kReferenceElement];
50
-
51
- this[kCompleted] = false;
52
- }
53
-
54
- ReceiveTaskExecution.prototype.execute = function execute(executeMessage) {
55
- this[kExecuteMessage] = executeMessage;
56
-
57
- const executeContent = executeMessage.content;
58
- const {executionId, isRootScope} = executeContent;
59
- this.executionId = executionId;
60
-
61
- const info = this[kReferenceInfo] = this._getReferenceInfo(executeMessage);
62
-
63
- if (isRootScope) {
64
- this._setupMessageHandling(executionId);
65
- }
66
-
67
- const loopCharacteristics = this.loopCharacteristics;
68
- if (loopCharacteristics && executeMessage.content.isRootScope) {
69
- return loopCharacteristics.execute(executeMessage);
70
- }
71
-
72
- const broker = this.broker;
73
- broker.consume('message', this._onCatchMessage.bind(this), {
74
- noAck: true,
75
- consumerTag: `_onmessage-${executionId}`,
76
- });
77
-
78
- if (this[kCompleted]) return;
79
-
80
- broker.subscribeTmp('api', `activity.#.${executionId}`, this._onApiMessage.bind(this), {
81
- noAck: true,
82
- consumerTag: `_api-${executionId}`,
83
- priority: 400,
84
- });
85
-
86
- this._debug(`expect ${info.description}`);
87
-
88
- broker.publish('event', 'activity.wait', cloneContent(executeContent, {message: {...info.message}}));
89
- };
90
-
91
- ReceiveTaskExecution.prototype._onCatchMessage = function onCatchMessage(routingKey, message) {
92
- const content = message.content;
93
-
94
- const {id: signalId, executionId: signalExecutionId} = content.message || {};
95
- const {message: referenceMessage, description} = this[kReferenceInfo];
96
-
97
- if (!referenceMessage.id && signalId || signalExecutionId) {
98
- if (this.loopCharacteristics && signalExecutionId !== this.executionId) return;
99
- if (signalId !== this.id && signalExecutionId !== this.executionId) return;
100
- this._debug('caught direct message');
101
- } else if (referenceMessage.id !== signalId) return;
102
- else {
103
- this._debug(`caught ${description}`);
104
- }
105
-
106
- const {type: messageType, correlationId} = message.properties;
107
- const broker = this.broker;
108
- const executeContent = this[kExecuteMessage].content;
109
-
110
- broker.publish('event', 'activity.consumed', cloneContent(executeContent, {message: {...message.content.message}}), {correlationId, type: messageType});
111
- broker.publish('event', 'activity.catch', cloneContent(executeContent, {message: message.content.message}), {type: 'catch', correlationId});
112
-
113
- this._complete(message.content.message, {correlationId});
114
- };
115
-
116
- ReceiveTaskExecution.prototype._onApiMessage = function onApiMessage(routingKey, message) {
117
- const {type: messageType, correlationId} = message.properties;
118
- switch (messageType) {
119
- case 'message':
120
- case 'signal': {
121
- return this._complete(message.content.message, {correlationId});
122
- }
123
- case 'discard': {
124
- this[kCompleted] = true;
125
- this._stop();
126
- return this.broker.publish('execution', 'execute.discard', cloneContent(this[kExecuteMessage].content), {correlationId});
127
- }
128
- case 'stop': {
129
- return this._stop();
130
- }
131
- }
132
- };
133
-
134
- ReceiveTaskExecution.prototype._complete = function complete(output, options) {
135
- this[kCompleted] = true;
136
- this._stop();
137
- return this.broker.publish('execution', 'execute.completed', cloneContent(this[kExecuteMessage].content, {output}), options);
138
- };
139
-
140
- ReceiveTaskExecution.prototype._stop = function stop() {
141
- const broker = this.broker, executionId = this.executionId;
142
- broker.cancel(`_onmessage-${executionId}`);
143
- broker.cancel(`_api-${executionId}`);
144
- };
145
-
146
- ReceiveTaskExecution.prototype._setupMessageHandling = function setupMessageHandling(executionId) {
147
- const broker = this.broker;
148
- broker.subscribeTmp('api', '#.signal.*', this._onDelegateMessage.bind(this), {
149
- noAck: true,
150
- consumerTag: `_api-delegated-${executionId}`,
151
- }, {
152
- noAck: true,
153
- });
154
- broker.subscribeTmp('api', `activity.stop.${executionId}`, this._onStopApiMessage.bind(this), {
155
- noAck: true,
156
- consumerTag: `_api-stop-${executionId}`,
157
- priority: 400,
158
- });
159
- broker.subscribeTmp('execution', 'execute.#', this._onExecutionComplete.bind(this), {
160
- noAck: true,
161
- consumerTag: `_execution-complete-${executionId}`,
162
- }, {
163
- noAck: true,
164
- });
165
- };
166
-
167
- ReceiveTaskExecution.prototype._onDelegateMessage = function onDelegateMessage(_, message) {
168
- if (!message.properties.delegate) return;
169
- this.broker.sendToQueue('message', message.content, message.properties);
170
- };
171
-
172
- ReceiveTaskExecution.prototype._onStopApiMessage = function onStopApiMessage() {
173
- this._stopMessageHandling(true);
174
- };
175
-
176
- ReceiveTaskExecution.prototype._onExecutionComplete = function onExecutionComplete(routingKey, {content}) {
177
- if (!content.isRootScope) return;
178
- switch (routingKey) {
179
- case 'execute.completed':
180
- case 'execute.error':
181
- case 'execute.discard':
182
- this._stopMessageHandling();
183
- break;
184
- }
185
- };
186
-
187
- ReceiveTaskExecution.prototype._stopMessageHandling = function stop(keepMessageQ) {
188
- const broker = this.broker, executionId = this.executionId;
189
- broker.cancel(`_api-delegated-${executionId}`);
190
- broker.cancel(`_api-stop-${executionId}`);
191
- broker.cancel(`_execution-complete-${executionId}`);
192
- if (!keepMessageQ) broker.purgeQueue('message');
193
- };
194
-
195
- ReceiveTaskExecution.prototype._getReferenceInfo = function getReferenceInfo(message) {
196
- const referenceElement = this.referenceElement;
197
- if (!referenceElement) {
198
- return {
199
- message: {...this.reference},
200
- description: 'anonymous message',
201
- };
202
- }
203
-
204
- const result = {
205
- message: referenceElement.resolve(message),
206
- };
207
-
208
- result.description = `${result.message.name} <${result.message.id}>`;
209
-
210
- return result;
211
- };
212
-
213
- ReceiveTaskExecution.prototype._debug = function debug(msg) {
214
- this.logger.debug(`<${this.executionId} (${this.id})> ${msg}`);
215
- };
1
+ import Activity from '../activity/Activity.js';
2
+ import {cloneContent} from '../messageHelper.js';
3
+
4
+ const kCompleted = Symbol.for('completed');
5
+ const kExecuteMessage = Symbol.for('executeMessage');
6
+ const kReferenceElement = Symbol.for('referenceElement');
7
+ const kReferenceInfo = Symbol.for('referenceInfo');
8
+
9
+ export default function ReceiveTask(activityDef, context) {
10
+ const task = new Activity(ReceiveTaskBehaviour, activityDef, context);
11
+
12
+ task.broker.assertQueue('message', {autoDelete: false, durable: true});
13
+ task.broker.bindQueue('message', 'api', '*.message.#', {durable: true});
14
+
15
+ return task;
16
+ }
17
+
18
+ export function ReceiveTaskBehaviour(activity) {
19
+ const {id, type, behaviour} = activity;
20
+
21
+ this.id = id;
22
+ this.type = type;
23
+
24
+ const reference = this.reference = {
25
+ name: 'anonymous',
26
+ ...behaviour.messageRef,
27
+ referenceType: 'message',
28
+ };
29
+
30
+ this.loopCharacteristics = behaviour.loopCharacteristics && new behaviour.loopCharacteristics.Behaviour(activity, behaviour.loopCharacteristics);
31
+ this.activity = activity;
32
+ this.broker = activity.broker;
33
+
34
+ this[kReferenceElement] = reference.id && activity.getActivityById(reference.id);
35
+ }
36
+
37
+ ReceiveTaskBehaviour.prototype.execute = function execute(executeMessage) {
38
+ return new ReceiveTaskExecution(this).execute(executeMessage);
39
+ };
40
+
41
+ function ReceiveTaskExecution(parent) {
42
+ const {activity, broker, loopCharacteristics, reference} = parent;
43
+
44
+ this.id = activity.id;
45
+ this.logger = activity.logger;
46
+ this.reference = reference;
47
+ this.broker = broker;
48
+ this.loopCharacteristics = loopCharacteristics;
49
+ this.referenceElement = parent[kReferenceElement];
50
+
51
+ this[kCompleted] = false;
52
+ }
53
+
54
+ ReceiveTaskExecution.prototype.execute = function execute(executeMessage) {
55
+ this[kExecuteMessage] = executeMessage;
56
+
57
+ const executeContent = executeMessage.content;
58
+ const {executionId, isRootScope} = executeContent;
59
+ this.executionId = executionId;
60
+
61
+ const info = this[kReferenceInfo] = this._getReferenceInfo(executeMessage);
62
+
63
+ if (isRootScope) {
64
+ this._setupMessageHandling(executionId);
65
+ }
66
+
67
+ const loopCharacteristics = this.loopCharacteristics;
68
+ if (loopCharacteristics && executeMessage.content.isRootScope) {
69
+ return loopCharacteristics.execute(executeMessage);
70
+ }
71
+
72
+ const broker = this.broker;
73
+ broker.consume('message', this._onCatchMessage.bind(this), {
74
+ noAck: true,
75
+ consumerTag: `_onmessage-${executionId}`,
76
+ });
77
+
78
+ if (this[kCompleted]) return;
79
+
80
+ broker.subscribeTmp('api', `activity.#.${executionId}`, this._onApiMessage.bind(this), {
81
+ noAck: true,
82
+ consumerTag: `_api-${executionId}`,
83
+ priority: 400,
84
+ });
85
+
86
+ this._debug(`expect ${info.description}`);
87
+
88
+ broker.publish('event', 'activity.wait', cloneContent(executeContent, {message: {...info.message}}));
89
+ };
90
+
91
+ ReceiveTaskExecution.prototype._onCatchMessage = function onCatchMessage(routingKey, message) {
92
+ const content = message.content;
93
+
94
+ const {id: signalId, executionId: signalExecutionId} = content.message || {};
95
+ const {message: referenceMessage, description} = this[kReferenceInfo];
96
+
97
+ if (!referenceMessage.id && signalId || signalExecutionId) {
98
+ if (this.loopCharacteristics && signalExecutionId !== this.executionId) return;
99
+ if (signalId !== this.id && signalExecutionId !== this.executionId) return;
100
+ this._debug('caught direct message');
101
+ } else if (referenceMessage.id !== signalId) return;
102
+ else {
103
+ this._debug(`caught ${description}`);
104
+ }
105
+
106
+ const {type: messageType, correlationId} = message.properties;
107
+ const broker = this.broker;
108
+ const executeContent = this[kExecuteMessage].content;
109
+
110
+ broker.publish('event', 'activity.consumed', cloneContent(executeContent, {message: {...message.content.message}}), {correlationId, type: messageType});
111
+ broker.publish('event', 'activity.catch', cloneContent(executeContent, {message: message.content.message}), {type: 'catch', correlationId});
112
+
113
+ this._complete(message.content.message, {correlationId});
114
+ };
115
+
116
+ ReceiveTaskExecution.prototype._onApiMessage = function onApiMessage(routingKey, message) {
117
+ const {type: messageType, correlationId} = message.properties;
118
+ switch (messageType) {
119
+ case 'message':
120
+ case 'signal': {
121
+ return this._complete(message.content.message, {correlationId});
122
+ }
123
+ case 'discard': {
124
+ this[kCompleted] = true;
125
+ this._stop();
126
+ return this.broker.publish('execution', 'execute.discard', cloneContent(this[kExecuteMessage].content), {correlationId});
127
+ }
128
+ case 'stop': {
129
+ return this._stop();
130
+ }
131
+ }
132
+ };
133
+
134
+ ReceiveTaskExecution.prototype._complete = function complete(output, options) {
135
+ this[kCompleted] = true;
136
+ this._stop();
137
+ return this.broker.publish('execution', 'execute.completed', cloneContent(this[kExecuteMessage].content, {output}), options);
138
+ };
139
+
140
+ ReceiveTaskExecution.prototype._stop = function stop() {
141
+ const broker = this.broker, executionId = this.executionId;
142
+ broker.cancel(`_onmessage-${executionId}`);
143
+ broker.cancel(`_api-${executionId}`);
144
+ };
145
+
146
+ ReceiveTaskExecution.prototype._setupMessageHandling = function setupMessageHandling(executionId) {
147
+ const broker = this.broker;
148
+ broker.subscribeTmp('api', '#.signal.*', this._onDelegateMessage.bind(this), {
149
+ noAck: true,
150
+ consumerTag: `_api-delegated-${executionId}`,
151
+ }, {
152
+ noAck: true,
153
+ });
154
+ broker.subscribeTmp('api', `activity.stop.${executionId}`, this._onStopApiMessage.bind(this), {
155
+ noAck: true,
156
+ consumerTag: `_api-stop-${executionId}`,
157
+ priority: 400,
158
+ });
159
+ broker.subscribeTmp('execution', 'execute.#', this._onExecutionComplete.bind(this), {
160
+ noAck: true,
161
+ consumerTag: `_execution-complete-${executionId}`,
162
+ }, {
163
+ noAck: true,
164
+ });
165
+ };
166
+
167
+ ReceiveTaskExecution.prototype._onDelegateMessage = function onDelegateMessage(_, message) {
168
+ if (!message.properties.delegate) return;
169
+ this.broker.sendToQueue('message', message.content, message.properties);
170
+ };
171
+
172
+ ReceiveTaskExecution.prototype._onStopApiMessage = function onStopApiMessage() {
173
+ this._stopMessageHandling(true);
174
+ };
175
+
176
+ ReceiveTaskExecution.prototype._onExecutionComplete = function onExecutionComplete(routingKey, {content}) {
177
+ if (!content.isRootScope) return;
178
+ switch (routingKey) {
179
+ case 'execute.completed':
180
+ case 'execute.error':
181
+ case 'execute.discard':
182
+ this._stopMessageHandling();
183
+ break;
184
+ }
185
+ };
186
+
187
+ ReceiveTaskExecution.prototype._stopMessageHandling = function stop(keepMessageQ) {
188
+ const broker = this.broker, executionId = this.executionId;
189
+ broker.cancel(`_api-delegated-${executionId}`);
190
+ broker.cancel(`_api-stop-${executionId}`);
191
+ broker.cancel(`_execution-complete-${executionId}`);
192
+ if (!keepMessageQ) broker.purgeQueue('message');
193
+ };
194
+
195
+ ReceiveTaskExecution.prototype._getReferenceInfo = function getReferenceInfo(message) {
196
+ const referenceElement = this.referenceElement;
197
+ if (!referenceElement) {
198
+ return {
199
+ message: {...this.reference},
200
+ description: 'anonymous message',
201
+ };
202
+ }
203
+
204
+ const result = {
205
+ message: referenceElement.resolve(message),
206
+ };
207
+
208
+ result.description = `${result.message.name} <${result.message.id}>`;
209
+
210
+ return result;
211
+ };
212
+
213
+ ReceiveTaskExecution.prototype._debug = function debug(msg) {
214
+ this.logger.debug(`<${this.executionId} (${this.id})> ${msg}`);
215
+ };
package/types/index.d.ts CHANGED
@@ -2,7 +2,7 @@ declare module 'bpmn-elements' {
2
2
  import { Broker } from 'smqp';
3
3
  import { BrokerState } from 'smqp/types/Broker';
4
4
  import { Consumer } from 'smqp/types/Queue';
5
- import { MessageMessage } from 'smqp/types/Message';
5
+ import { MessageMessage, MessageFields, MessageProperties } from 'smqp/types/Message';
6
6
  import { SerializableContext, SerializableElement } from 'moddle-context-serializer';
7
7
 
8
8
  interface ElementBroker<T> extends Broker {
@@ -25,16 +25,16 @@ declare module 'bpmn-elements' {
25
25
  [x: string]: any,
26
26
  }
27
27
 
28
- type elementContent = {
29
- id?: string,
30
- type?: string,
31
- executionId?: string,
32
- parent?: ElementParent,
33
- [x: string]: any,
28
+ interface ElementMessageContent {
29
+ id?: string;
30
+ type?: string;
31
+ executionId?: string;
32
+ parent?: ElementParent;
33
+ [x: string]: any;
34
34
  }
35
35
 
36
36
  interface ElementBrokerMessage extends MessageMessage {
37
- content: elementContent,
37
+ content: ElementMessageContent,
38
38
  }
39
39
 
40
40
  interface EventDefinition {
@@ -211,8 +211,31 @@ declare module 'bpmn-elements' {
211
211
  createMessage(content?: Record<string, any>): any;
212
212
  }
213
213
 
214
+ interface ExecutionScope {
215
+ /** Calling element id */
216
+ id: string;
217
+ /** Calling element type */
218
+ type: string;
219
+ /** Execution message fields */
220
+ fields: MessageFields;
221
+ /** Execution message content */
222
+ content: ElementMessageContent;
223
+ /** Execution message properties */
224
+ properties: MessageProperties;
225
+ environment: Environment;
226
+ /** Calling element logger instance */
227
+ logger?: ILogger;
228
+ /**
229
+ * Resolve expression with the current scope
230
+ * @param expression expression string
231
+ * @returns Whatever the expression returns
232
+ */
233
+ resolveExpression: (expression: string) => any;
234
+ ActivityError: ActivityError;
235
+ }
236
+
214
237
  interface Script {
215
- execute(executionContext: any, callback: CallableFunction): void;
238
+ execute(executionContext: ExecutionScope, callback: CallableFunction): void;
216
239
  }
217
240
 
218
241
  abstract class MessageElement {
@@ -601,12 +624,14 @@ declare module 'bpmn-elements' {
601
624
  class Signal extends MessageElement {}
602
625
  class Escalation extends MessageElement {}
603
626
 
604
- interface ActivityError extends Error {
627
+ class ActivityError extends Error {
605
628
  type: string;
606
629
  description: string;
607
630
  /** Activity that threw error */
608
631
  source?: ElementBrokerMessage;
609
632
  /** Original error */
610
633
  inner?: Error;
634
+ code?: string;
635
+ constructor(description: string, sourceMessage: MessageMessage, inner?: Error);
611
636
  }
612
637
  }