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
package/src/io/BpmnIO.js CHANGED
@@ -1,19 +1,24 @@
1
1
  export default function BpmnIO(activity, context) {
2
- const {ioSpecification: ioSpecificationDef, properties: propertiesDef} = activity.behaviour;
2
+ this.activity = activity;
3
+ this.context = context;
3
4
 
4
- const ioSpecification = ioSpecificationDef && ioSpecificationDef.Behaviour(activity, ioSpecificationDef, context);
5
- const bpmnProperties = propertiesDef && propertiesDef.Behaviour(activity, propertiesDef, context);
5
+ const {
6
+ ioSpecification: ioSpecificationDef,
7
+ properties: propertiesDef,
8
+ } = activity.behaviour;
6
9
 
7
- if (!ioSpecification && !bpmnProperties) return;
8
-
9
- return {
10
- activate(message) {
11
- if (bpmnProperties) bpmnProperties.activate(message);
12
- if (ioSpecification) ioSpecification.activate(message);
13
- },
14
- deactivate(message) {
15
- if (bpmnProperties) bpmnProperties.deactivate(message);
16
- if (ioSpecification) ioSpecification.deactivate(message);
17
- },
18
- };
10
+ this.specification = ioSpecificationDef && new ioSpecificationDef.Behaviour(activity, ioSpecificationDef, context);
11
+ this.properties = propertiesDef && new propertiesDef.Behaviour(activity, propertiesDef, context);
19
12
  }
13
+
14
+ BpmnIO.prototype.activate = function activate(message) {
15
+ const properties = this.properties, specification = this.specification;
16
+ if (properties) properties.activate(message);
17
+ if (specification) specification.activate(message);
18
+ };
19
+
20
+ BpmnIO.prototype.deactivate = function deactivate(message) {
21
+ const properties = this.properties, specification = this.specification;
22
+ if (properties) properties.deactivate(message);
23
+ if (specification) specification.deactivate(message);
24
+ };
@@ -1,22 +1,33 @@
1
1
  export default function EnvironmentDataObject(dataObjectDef, {environment}) {
2
2
  const {id, type, name, behaviour, parent} = dataObjectDef;
3
+ this.id = id;
4
+ this.type = type;
5
+ this.name = name;
6
+ this.behaviour = behaviour;
7
+ this.parent = parent;
8
+ this.environment = environment;
9
+ }
3
10
 
4
- const source = {
5
- id,
6
- name,
7
- type,
8
- behaviour,
9
- parent,
10
- read(broker, exchange, routingKeyPrefix, messageProperties) {
11
- const value = environment.variables._data && environment.variables._data[id];
12
- return broker.publish(exchange, `${routingKeyPrefix}response`, {id, name, type, value}, messageProperties);
13
- },
14
- write(broker, exchange, routingKeyPrefix, value, messageProperties) {
15
- environment.variables._data = environment.variables._data || {};
16
- environment.variables._data[id] = value;
17
- return broker.publish(exchange, `${routingKeyPrefix}response`, {id, name, type, value}, messageProperties);
18
- },
19
- };
11
+ EnvironmentDataObject.prototype.read = function read(broker, exchange, routingKeyPrefix, messageProperties) {
12
+ const environment = this.environment;
13
+ const value = environment.variables._data && environment.variables._data[this.id];
14
+ const content = this._createContent(value);
15
+ return broker.publish(exchange, `${routingKeyPrefix}response`, content, messageProperties);
16
+ };
20
17
 
21
- return source;
22
- }
18
+ EnvironmentDataObject.prototype.write = function write(broker, exchange, routingKeyPrefix, value, messageProperties) {
19
+ const environment = this.environment;
20
+ environment.variables._data = environment.variables._data || {};
21
+ environment.variables._data[this.id] = value;
22
+ const content = this._createContent(value);
23
+ return broker.publish(exchange, `${routingKeyPrefix}response`, content, messageProperties);
24
+ };
25
+
26
+ EnvironmentDataObject.prototype._createContent = function createContent(value) {
27
+ return {
28
+ id: this.id,
29
+ type: this.type,
30
+ name: this.name,
31
+ value,
32
+ };
33
+ };
@@ -1,22 +1,33 @@
1
1
  export default function EnvironmentDataStore(dataStoreDef, {environment}) {
2
2
  const {id, type, name, behaviour, parent} = dataStoreDef;
3
+ this.id = id;
4
+ this.type = type;
5
+ this.name = name;
6
+ this.behaviour = behaviour;
7
+ this.parent = parent;
8
+ this.environment = environment;
9
+ }
3
10
 
4
- const source = {
5
- id,
6
- name,
7
- type,
8
- behaviour,
9
- parent,
10
- read(broker, exchange, routingKeyPrefix, messageProperties) {
11
- const value = environment.variables._data && environment.variables._data[id];
12
- return broker.publish(exchange, `${routingKeyPrefix}response`, {id, name, type, value}, messageProperties);
13
- },
14
- write(broker, exchange, routingKeyPrefix, value, messageProperties) {
15
- environment.variables._data = environment.variables._data || {};
16
- environment.variables._data[id] = value;
17
- return broker.publish(exchange, `${routingKeyPrefix}response`, {id, name, type, value}, messageProperties);
18
- },
19
- };
11
+ EnvironmentDataStore.prototype.read = function read(broker, exchange, routingKeyPrefix, messageProperties) {
12
+ const environment = this.environment;
13
+ const value = environment.variables._data && environment.variables._data[this.id];
14
+ const content = this._createContent(value);
15
+ return broker.publish(exchange, `${routingKeyPrefix}response`, content, messageProperties);
16
+ };
20
17
 
21
- return source;
22
- }
18
+ EnvironmentDataStore.prototype.write = function write(broker, exchange, routingKeyPrefix, value, messageProperties) {
19
+ const environment = this.environment;
20
+ environment.variables._data = environment.variables._data || {};
21
+ environment.variables._data[this.id] = value;
22
+ const content = this._createContent(value);
23
+ return broker.publish(exchange, `${routingKeyPrefix}response`, content, messageProperties);
24
+ };
25
+
26
+ EnvironmentDataStore.prototype._createContent = function createContent(value) {
27
+ return {
28
+ id: this.id,
29
+ type: this.type,
30
+ name: this.name,
31
+ value,
32
+ };
33
+ };
@@ -1,22 +1,33 @@
1
- export default function EnvironmentDataStoreReference(dataStoreRefDef, {environment}) {
2
- const {id, type, name, behaviour, parent} = dataStoreRefDef;
1
+ export default function EnvironmentDataStoreReference(dataObjectDef, {environment}) {
2
+ const {id, type, name, behaviour, parent} = dataObjectDef;
3
+ this.id = id;
4
+ this.type = type;
5
+ this.name = name;
6
+ this.behaviour = behaviour;
7
+ this.parent = parent;
8
+ this.environment = environment;
9
+ }
3
10
 
4
- const source = {
5
- id,
6
- name,
7
- type,
8
- behaviour,
9
- parent,
10
- read(broker, exchange, routingKeyPrefix, messageProperties) {
11
- const value = environment.variables._data && environment.variables._data[id];
12
- return broker.publish(exchange, `${routingKeyPrefix}response`, {id, name, type, value}, messageProperties);
13
- },
14
- write(broker, exchange, routingKeyPrefix, value, messageProperties) {
15
- environment.variables._data = environment.variables._data || {};
16
- environment.variables._data[id] = value;
17
- return broker.publish(exchange, `${routingKeyPrefix}response`, {id, name, type, value}, messageProperties);
18
- },
19
- };
11
+ EnvironmentDataStoreReference.prototype.read = function read(broker, exchange, routingKeyPrefix, messageProperties) {
12
+ const environment = this.environment;
13
+ const value = environment.variables._data && environment.variables._data[this.id];
14
+ const content = this._createContent(value);
15
+ return broker.publish(exchange, `${routingKeyPrefix}response`, content, messageProperties);
16
+ };
20
17
 
21
- return source;
22
- }
18
+ EnvironmentDataStoreReference.prototype.write = function write(broker, exchange, routingKeyPrefix, value, messageProperties) {
19
+ const environment = this.environment;
20
+ environment.variables._data = environment.variables._data || {};
21
+ environment.variables._data[this.id] = value;
22
+ const content = this._createContent(value);
23
+ return broker.publish(exchange, `${routingKeyPrefix}response`, content, messageProperties);
24
+ };
25
+
26
+ EnvironmentDataStoreReference.prototype._createContent = function createContent(value) {
27
+ return {
28
+ id: this.id,
29
+ type: this.type,
30
+ name: this.name,
31
+ value,
32
+ };
33
+ };
@@ -1,186 +1,184 @@
1
1
  import getPropertyValue from '../getPropertyValue';
2
2
  import {brokerSafeId} from '../shared';
3
3
 
4
+ const kConsuming = Symbol.for('consuming');
5
+
4
6
  export default function IoSpecification(activity, ioSpecificationDef, context) {
5
7
  const {id, type = 'iospecification', behaviour = {}} = ioSpecificationDef;
6
- const {broker} = activity;
7
-
8
- const safeType = brokerSafeId(type).toLowerCase();
9
- let activityConsumer;
10
-
11
- const {dataInputs, dataOutputs} = behaviour;
8
+ this.id = id;
9
+ this.type = type;
10
+ this.behaviour = behaviour;
11
+ this.activity = activity;
12
+ this.broker = activity.broker;
13
+ this.context = context;
14
+ }
12
15
 
13
- const ioApi = {
14
- id,
15
- type,
16
- behaviour,
17
- activate,
18
- deactivate,
19
- };
16
+ const proto = IoSpecification.prototype;
20
17
 
21
- return ioApi;
18
+ proto.activate = function activate() {
19
+ if (this[kConsuming]) return;
20
+ this[kConsuming] = this.broker.subscribeTmp('event', 'activity.#', this._onActivityEvent.bind(this), {noAck: true});
21
+ };
22
22
 
23
- function activate() {
24
- if (activityConsumer) return;
25
- activityConsumer = broker.subscribeTmp('event', 'activity.#', onActivityEvent, {noAck: true});
26
- }
23
+ proto.deactivate = function deactivate() {
24
+ if (this[kConsuming]) this[kConsuming] = this[kConsuming].cancel();
25
+ };
27
26
 
28
- function deactivate() {
29
- if (activityConsumer) activityConsumer = activityConsumer.cancel();
27
+ proto._onActivityEvent = function onActivityEvent(routingKey, message) {
28
+ const {dataInputs, dataOutputs} = this.behaviour;
29
+ if ((dataInputs || dataOutputs) && routingKey === 'activity.enter') {
30
+ return this._onFormatEnter();
30
31
  }
31
32
 
32
- function onActivityEvent(routingKey, message) {
33
- if ((dataInputs || dataOutputs) && routingKey === 'activity.enter') {
34
- return formatOnEnter();
35
- }
36
-
37
- if (dataOutputs && routingKey === 'activity.execution.completed') {
38
- formatOnComplete(message);
39
- }
33
+ if (dataOutputs && routingKey === 'activity.execution.completed') {
34
+ this._onFormatComplete(message);
40
35
  }
41
-
42
- function formatOnEnter() {
43
- const startRoutingKey = `run.onstart.${safeType}`;
44
- if (!dataInputs) {
45
- return broker.publish('format', startRoutingKey, {
46
- ioSpecification: {
47
- dataOutputs: getDataOutputs(),
48
- },
49
- });
50
- }
51
-
52
- const {dataObjects, sources} = dataInputs.reduce((result, ioSource, index) => {
53
- const source = {
54
- id: ioSource.id,
55
- type: ioSource.type,
56
- name: ioSource.name,
57
- };
58
- result.sources.push(source);
59
-
60
- const dataObjectId = getPropertyValue(ioSource, 'behaviour.association.source.dataObject.id');
61
- if (!dataObjectId) return result;
62
- const dataObject = context.getDataObjectById(dataObjectId);
63
- if (!dataObject) return result;
64
- result.dataObjects.push({index, dataObject});
65
- return result;
66
- }, {
67
- dataObjects: [],
68
- sources: [],
69
- });
70
-
71
- if (!dataObjects.length) {
72
- return broker.publish('format', startRoutingKey, {
73
- ioSpecification: {
74
- dataInputs: sources,
75
- dataOutputs: getDataOutputs(),
76
- },
77
- });
78
- }
79
-
80
- const endRoutingKey = `run.onstart.${safeType}.end`;
81
- broker.publish('format', `${startRoutingKey}.begin`, {
82
- endRoutingKey,
36
+ };
37
+
38
+ proto._onFormatEnter = function onFormatOnEnter() {
39
+ const safeType = brokerSafeId(this.type).toLowerCase();
40
+ const startRoutingKey = `run.onstart.${safeType}`;
41
+ const {dataInputs, dataOutputs} = this.behaviour;
42
+ const broker = this.broker;
43
+ if (!dataInputs) {
44
+ return broker.publish('format', startRoutingKey, {
83
45
  ioSpecification: {
84
- dataInputs: sources.map((source) => {
85
- return {...source};
86
- }),
87
- dataOutputs: getDataOutputs(),
46
+ dataOutputs: this._getDataOutputs(dataOutputs),
88
47
  },
89
48
  });
90
-
91
- return read(broker, dataObjects, (_, responses) => {
92
- responses.forEach((response) => {
93
- sources[response.index].value = response.value;
94
- });
95
-
96
- broker.publish('format', endRoutingKey, {
97
- ioSpecification: {
98
- dataInputs: sources,
99
- dataOutputs: getDataOutputs(),
100
- },
101
- });
102
- });
103
49
  }
104
50
 
105
- function formatOnComplete(message) {
106
- const messageInputs = getPropertyValue(message, 'content.ioSpecification.dataInputs');
107
- const messageOutputs = getPropertyValue(message, 'content.output.ioSpecification.dataOutputs') || [];
108
-
109
- const {dataObjects, sources} = dataOutputs.reduce((result, ioSource, index) => {
110
- const {value} = messageOutputs.find((output) => output.id === ioSource.id) || {};
111
- const source = {
112
- id: ioSource.id,
113
- type: ioSource.type,
114
- name: ioSource.name,
115
- value,
116
- };
117
- result.sources.push(source);
118
-
119
- const dataObjectId = getPropertyValue(ioSource, 'behaviour.association.target.dataObject.id');
120
- if (!dataObjectId) return result;
121
- const dataObject = context.getDataObjectById(dataObjectId);
122
- if (!dataObject) return result;
123
- result.dataObjects.push({index, dataObject, value});
124
- return result;
125
- }, {
126
- dataObjects: [],
127
- sources: [],
51
+ const {dataObjects, sources} = dataInputs.reduce((result, ioSource, index) => {
52
+ const source = {
53
+ id: ioSource.id,
54
+ type: ioSource.type,
55
+ name: ioSource.name,
56
+ };
57
+ result.sources.push(source);
58
+
59
+ const dataObjectId = getPropertyValue(ioSource, 'behaviour.association.source.dataObject.id');
60
+ if (!dataObjectId) return result;
61
+ const dataObject = this.context.getDataObjectById(dataObjectId);
62
+ if (!dataObject) return result;
63
+ result.dataObjects.push({index, dataObject});
64
+ return result;
65
+ }, {
66
+ dataObjects: [],
67
+ sources: [],
68
+ });
69
+
70
+ if (!dataObjects.length) {
71
+ return broker.publish('format', startRoutingKey, {
72
+ ioSpecification: {
73
+ dataInputs: sources,
74
+ dataOutputs: this._getDataOutputs(dataOutputs),
75
+ },
128
76
  });
77
+ }
129
78
 
130
- const startRoutingKey = `run.onend.${safeType}`;
131
- if (!dataObjects.length) {
132
- return broker.publish('format', startRoutingKey, {
133
- ioSpecification: {
134
- dataInputs: messageInputs,
135
- dataOutputs: sources,
136
- },
137
- });
138
- }
139
-
140
- const endRoutingKey = `run.onend.${safeType}.end`;
141
- broker.publish('format', `${startRoutingKey}.begin`, {
142
- endRoutingKey,
79
+ const endRoutingKey = `run.onstart.${safeType}.end`;
80
+ broker.publish('format', `${startRoutingKey}.begin`, {
81
+ endRoutingKey,
82
+ ioSpecification: {
83
+ dataInputs: sources.map((source) => {
84
+ return {...source};
85
+ }),
86
+ dataOutputs: this._getDataOutputs(dataOutputs),
87
+ },
88
+ });
89
+
90
+ return read(broker, dataObjects, (_, responses) => {
91
+ for (const response of responses) sources[response.index].value = response.value;
92
+
93
+ broker.publish('format', endRoutingKey, {
143
94
  ioSpecification: {
144
- dataInputs: sources.map((input) => {
145
- return {...input};
146
- }),
147
- dataOutputs: getDataOutputs(),
95
+ dataInputs: sources,
96
+ dataOutputs: this._getDataOutputs(dataOutputs),
148
97
  },
149
98
  });
150
-
151
- return write(broker, dataObjects, (_, responses) => {
152
- responses.forEach((response) => {
153
- sources[response.index].value = response.value;
154
- });
155
-
156
- broker.publish('format', endRoutingKey, {
157
- ioSpecification: {
158
- dataInputs: sources,
159
- dataOutputs: getDataOutputs(),
160
- },
161
- });
99
+ });
100
+ };
101
+
102
+ proto._onFormatComplete = function formatOnComplete(message) {
103
+ const safeType = brokerSafeId(this.type).toLowerCase();
104
+ const messageInputs = getPropertyValue(message, 'content.ioSpecification.dataInputs');
105
+ const messageOutputs = getPropertyValue(message, 'content.output.ioSpecification.dataOutputs') || [];
106
+ const dataOutputs = this.behaviour.dataOutputs;
107
+ const broker = this.broker;
108
+ const context = this.context;
109
+
110
+ const {dataObjects, sources} = dataOutputs.reduce((result, ioSource, index) => {
111
+ const {value} = messageOutputs.find((output) => output.id === ioSource.id) || {};
112
+ const source = {
113
+ id: ioSource.id,
114
+ type: ioSource.type,
115
+ name: ioSource.name,
116
+ value,
117
+ };
118
+ result.sources.push(source);
119
+
120
+
121
+ const dataObjectId = getPropertyValue(ioSource, 'behaviour.association.target.dataObject.id');
122
+ if (!dataObjectId) return result;
123
+ const dataObject = context.getDataObjectById(dataObjectId);
124
+ if (!dataObject) return result;
125
+ result.dataObjects.push({index, dataObject, value});
126
+ return result;
127
+ }, {
128
+ dataObjects: [],
129
+ sources: [],
130
+ });
131
+
132
+ const startRoutingKey = `run.onend.${safeType}`;
133
+ if (!dataObjects.length) {
134
+ return broker.publish('format', startRoutingKey, {
135
+ ioSpecification: {
136
+ dataInputs: messageInputs,
137
+ dataOutputs: sources,
138
+ },
162
139
  });
163
140
  }
164
141
 
165
- function getDataOutputs() {
166
- if (!dataOutputs) return;
167
- return dataOutputs.map((dataOutput) => {
168
- return {
169
- id: dataOutput.id,
170
- type: dataOutput.type,
171
- name: dataOutput.name,
172
- };
142
+ const endRoutingKey = `run.onend.${safeType}.end`;
143
+ broker.publish('format', `${startRoutingKey}.begin`, {
144
+ endRoutingKey,
145
+ ioSpecification: {
146
+ dataInputs: sources.map((input) => {
147
+ return {...input};
148
+ }),
149
+ dataOutputs: this._getDataOutputs(dataOutputs),
150
+ },
151
+ });
152
+
153
+ return write(broker, dataObjects, (_, responses) => {
154
+ for (const response of responses) sources[response.index].value = response.value;
155
+
156
+ broker.publish('format', endRoutingKey, {
157
+ ioSpecification: {
158
+ dataInputs: sources,
159
+ dataOutputs: this._getDataOutputs(dataOutputs),
160
+ },
173
161
  });
174
- }
175
- }
162
+ });
163
+ };
164
+
165
+ proto._getDataOutputs = function getDataOutputs(dataOutputs) {
166
+ if (!dataOutputs) return;
167
+ return dataOutputs.map((dataOutput) => {
168
+ return {
169
+ id: dataOutput.id,
170
+ type: dataOutput.type,
171
+ name: dataOutput.name,
172
+ };
173
+ });
174
+ };
176
175
 
177
176
  function read(broker, dataObjectRefs, callback) {
178
177
  const responses = [];
179
178
  let count = 0;
180
179
  const dataReadConsumer = broker.subscribeTmp('data', 'data.read.#', onDataObjectResponse, {noAck: true});
181
180
 
182
- for (let i = 0; i < dataObjectRefs.length; i++) {
183
- const {dataObject} = dataObjectRefs[i];
181
+ for (const {dataObject} of dataObjectRefs) {
184
182
  dataObject.read(broker, 'data', 'data.read.');
185
183
  }
186
184
 
@@ -200,22 +198,21 @@ function read(broker, dataObjectRefs, callback) {
200
198
  function write(broker, dataObjectRefs, callback) {
201
199
  const responses = [];
202
200
  let count = 0;
203
- const dataWriteConsumer = broker.subscribeTmp('data', 'data.write.#', onDataObjectResponse, {noAck: true});
201
+ broker.subscribeTmp('data', 'data.write.#', onDataObjectResponse, {noAck: true});
204
202
 
205
- for (let i = 0; i < dataObjectRefs.length; i++) {
206
- const {dataObject, value} = dataObjectRefs[i];
203
+ for (const {dataObject, value} of dataObjectRefs) {
207
204
  dataObject.write(broker, 'data', 'data.write.', value);
208
205
  }
209
206
 
210
207
  function onDataObjectResponse(routingKey, message) {
211
- const idx = dataObjectRefs.findIndex((dobj) => dobj.id === message.content.id);
212
- responses[idx] = message.content;
208
+ const idx = dataObjectRefs.findIndex(({dataObject}) => dataObject.id === message.content.id);
209
+ responses[idx] = {index: idx, ...message.content};
213
210
 
214
211
  ++count;
215
212
 
216
213
  if (count < dataObjectRefs.length) return;
217
214
 
218
- dataWriteConsumer.cancel();
215
+ broker.cancel(message.fields.consumerTag);
219
216
  return callback(null, responses);
220
217
  }
221
218
  }