@logicflow/engine 0.0.10-beta.0 → 0.0.11-beta.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 (189) hide show
  1. package/README.md +2 -5
  2. package/cjs/EventEmitter.js +70 -0
  3. package/cjs/FlowModel.js +277 -0
  4. package/cjs/Scheduler.js +252 -0
  5. package/{lib/constant/logCode.js → cjs/constant/LogCode.js} +13 -16
  6. package/{lib/constant/index.js → cjs/constant/constant.js} +3 -4
  7. package/cjs/expression/browserVm.js +81 -0
  8. package/cjs/expression/index.js +57 -0
  9. package/cjs/index.js +210 -0
  10. package/cjs/nodes/BaseNode.js +252 -0
  11. package/cjs/nodes/StartNode.js +27 -0
  12. package/cjs/nodes/TaskNode.js +27 -0
  13. package/cjs/recorder/index.js +168 -0
  14. package/cjs/util/ID.js +16 -0
  15. package/cjs/util/global.js +32 -0
  16. package/{lib/utils → cjs/util}/storage.js +17 -18
  17. package/es/EventEmitter.d.ts +4 -34
  18. package/es/EventEmitter.js +48 -70
  19. package/es/FlowModel.d.ts +73 -75
  20. package/es/FlowModel.js +173 -130
  21. package/es/Scheduler.d.ts +34 -50
  22. package/es/Scheduler.js +209 -134
  23. package/es/constant/LogCode.js +28 -0
  24. package/es/constant/{index.js → constant.js} +7 -8
  25. package/es/expression/browserVm.d.ts +4 -0
  26. package/es/expression/browserVm.js +76 -0
  27. package/es/expression/index.d.ts +1 -1
  28. package/es/expression/index.js +53 -16
  29. package/es/index.d.ts +38 -135
  30. package/es/index.js +144 -92
  31. package/es/nodes/BaseNode.d.ts +110 -0
  32. package/es/nodes/BaseNode.js +250 -0
  33. package/es/nodes/{start.d.ts → StartNode.d.ts} +2 -3
  34. package/es/nodes/StartNode.js +25 -0
  35. package/es/nodes/{task.d.ts → TaskNode.d.ts} +2 -3
  36. package/es/nodes/TaskNode.js +25 -0
  37. package/es/recorder/index.d.ts +10 -36
  38. package/es/recorder/index.js +135 -82
  39. package/es/util/ID.js +13 -0
  40. package/es/util/global.d.ts +5 -0
  41. package/es/util/global.js +26 -0
  42. package/es/{utils → util}/storage.js +16 -17
  43. package/lib/main.js +1 -0
  44. package/package.json +71 -30
  45. package/dist/index.js +0 -2
  46. package/dist/index.js.map +0 -1
  47. package/es/EventEmitter.js.map +0 -1
  48. package/es/FlowModel.js.map +0 -1
  49. package/es/Scheduler.js.map +0 -1
  50. package/es/constant/index.js.map +0 -1
  51. package/es/constant/logCode.js +0 -29
  52. package/es/constant/logCode.js.map +0 -1
  53. package/es/expression/brewserVm.d.ts +0 -2
  54. package/es/expression/brewserVm.js +0 -30
  55. package/es/expression/brewserVm.js.map +0 -1
  56. package/es/expression/index.js.map +0 -1
  57. package/es/expression/nodeVm.d.ts +0 -4
  58. package/es/expression/nodeVm.js +0 -10
  59. package/es/expression/nodeVm.js.map +0 -1
  60. package/es/index.js.map +0 -1
  61. package/es/nodes/base.d.ts +0 -108
  62. package/es/nodes/base.js +0 -149
  63. package/es/nodes/base.js.map +0 -1
  64. package/es/nodes/index.d.ts +0 -3
  65. package/es/nodes/index.js +0 -4
  66. package/es/nodes/index.js.map +0 -1
  67. package/es/nodes/start.js +0 -11
  68. package/es/nodes/start.js.map +0 -1
  69. package/es/nodes/task.js +0 -11
  70. package/es/nodes/task.js.map +0 -1
  71. package/es/platform/browser/browserVm.d.ts +0 -4
  72. package/es/platform/browser/browserVm.js +0 -44
  73. package/es/platform/browser/browserVm.js.map +0 -1
  74. package/es/platform/browser/index.d.ts +0 -4
  75. package/es/platform/browser/index.js +0 -23
  76. package/es/platform/browser/index.js.map +0 -1
  77. package/es/platform/index.d.ts +0 -1
  78. package/es/platform/index.js +0 -2
  79. package/es/platform/index.js.map +0 -1
  80. package/es/platform/node/index.d.ts +0 -4
  81. package/es/platform/node/index.js +0 -23
  82. package/es/platform/node/index.js.map +0 -1
  83. package/es/platform/node/nodeVm.d.ts +0 -1
  84. package/es/platform/node/nodeVm.js +0 -9
  85. package/es/platform/node/nodeVm.js.map +0 -1
  86. package/es/recorder/index.js.map +0 -1
  87. package/es/utils/global.d.ts +0 -5
  88. package/es/utils/global.js +0 -27
  89. package/es/utils/global.js.map +0 -1
  90. package/es/utils/id.js +0 -14
  91. package/es/utils/id.js.map +0 -1
  92. package/es/utils/index.d.ts +0 -4
  93. package/es/utils/index.js +0 -5
  94. package/es/utils/index.js.map +0 -1
  95. package/es/utils/storage.js.map +0 -1
  96. package/lib/EventEmitter.d.ts +0 -37
  97. package/lib/EventEmitter.js +0 -94
  98. package/lib/EventEmitter.js.map +0 -1
  99. package/lib/FlowModel.d.ts +0 -146
  100. package/lib/FlowModel.js +0 -236
  101. package/lib/FlowModel.js.map +0 -1
  102. package/lib/Scheduler.d.ts +0 -78
  103. package/lib/Scheduler.js +0 -179
  104. package/lib/Scheduler.js.map +0 -1
  105. package/lib/constant/index.d.ts +0 -16
  106. package/lib/constant/index.js.map +0 -1
  107. package/lib/constant/logCode.d.ts +0 -12
  108. package/lib/constant/logCode.js.map +0 -1
  109. package/lib/expression/brewserVm.d.ts +0 -2
  110. package/lib/expression/brewserVm.js +0 -33
  111. package/lib/expression/brewserVm.js.map +0 -1
  112. package/lib/expression/index.d.ts +0 -2
  113. package/lib/expression/index.js +0 -20
  114. package/lib/expression/index.js.map +0 -1
  115. package/lib/expression/nodeVm.d.ts +0 -4
  116. package/lib/expression/nodeVm.js +0 -13
  117. package/lib/expression/nodeVm.js.map +0 -1
  118. package/lib/index.d.ts +0 -157
  119. package/lib/index.js +0 -159
  120. package/lib/index.js.map +0 -1
  121. package/lib/nodes/base.d.ts +0 -108
  122. package/lib/nodes/base.js +0 -152
  123. package/lib/nodes/base.js.map +0 -1
  124. package/lib/nodes/index.d.ts +0 -3
  125. package/lib/nodes/index.js +0 -7
  126. package/lib/nodes/index.js.map +0 -1
  127. package/lib/nodes/start.d.ts +0 -6
  128. package/lib/nodes/start.js +0 -15
  129. package/lib/nodes/start.js.map +0 -1
  130. package/lib/nodes/task.d.ts +0 -6
  131. package/lib/nodes/task.js +0 -15
  132. package/lib/nodes/task.js.map +0 -1
  133. package/lib/platform/browser/browserVm.d.ts +0 -4
  134. package/lib/platform/browser/browserVm.js +0 -49
  135. package/lib/platform/browser/browserVm.js.map +0 -1
  136. package/lib/platform/browser/index.d.ts +0 -4
  137. package/lib/platform/browser/index.js +0 -28
  138. package/lib/platform/browser/index.js.map +0 -1
  139. package/lib/platform/index.d.ts +0 -1
  140. package/lib/platform/index.js +0 -5
  141. package/lib/platform/index.js.map +0 -1
  142. package/lib/platform/node/index.d.ts +0 -4
  143. package/lib/platform/node/index.js +0 -28
  144. package/lib/platform/node/index.js.map +0 -1
  145. package/lib/platform/node/nodeVm.d.ts +0 -1
  146. package/lib/platform/node/nodeVm.js +0 -13
  147. package/lib/platform/node/nodeVm.js.map +0 -1
  148. package/lib/recorder/index.d.ts +0 -46
  149. package/lib/recorder/index.js +0 -117
  150. package/lib/recorder/index.js.map +0 -1
  151. package/lib/utils/global.d.ts +0 -5
  152. package/lib/utils/global.js +0 -31
  153. package/lib/utils/global.js.map +0 -1
  154. package/lib/utils/id.d.ts +0 -3
  155. package/lib/utils/id.js +0 -20
  156. package/lib/utils/id.js.map +0 -1
  157. package/lib/utils/index.d.ts +0 -4
  158. package/lib/utils/index.js +0 -9
  159. package/lib/utils/index.js.map +0 -1
  160. package/lib/utils/storage.d.ts +0 -7
  161. package/lib/utils/storage.js.map +0 -1
  162. package/src/EventEmitter.ts +0 -103
  163. package/src/FlowModel.ts +0 -325
  164. package/src/Scheduler.ts +0 -244
  165. package/src/constant/index.ts +0 -23
  166. package/src/constant/logCode.ts +0 -34
  167. package/src/expression/brewserVm.ts +0 -36
  168. package/src/expression/index.ts +0 -17
  169. package/src/expression/nodeVm.ts +0 -14
  170. package/src/index.ts +0 -300
  171. package/src/nodes/base.ts +0 -234
  172. package/src/nodes/index.ts +0 -3
  173. package/src/nodes/start.ts +0 -8
  174. package/src/nodes/task.ts +0 -8
  175. package/src/platform/browser/browserVm.ts +0 -52
  176. package/src/platform/browser/index.ts +0 -28
  177. package/src/platform/index.ts +0 -1
  178. package/src/platform/node/index.ts +0 -28
  179. package/src/platform/node/nodeVm.ts +0 -12
  180. package/src/recorder/index.ts +0 -137
  181. package/src/typings.d.ts +0 -0
  182. package/src/utils/global.ts +0 -41
  183. package/src/utils/id.ts +0 -16
  184. package/src/utils/index.ts +0 -5
  185. package/src/utils/storage.ts +0 -55
  186. /package/es/constant/{logCode.d.ts → LogCode.d.ts} +0 -0
  187. /package/es/constant/{index.d.ts → constant.d.ts} +0 -0
  188. /package/es/{utils/id.d.ts → util/ID.d.ts} +0 -0
  189. /package/es/{utils → util}/storage.d.ts +0 -0
package/es/FlowModel.js CHANGED
@@ -1,23 +1,71 @@
1
- import { __awaiter } from "tslib";
2
- import { Scheduler } from './Scheduler';
3
- import { createExecId } from './utils';
4
- import { EVENT_INSTANCE_COMPLETE, EVENT_INSTANCE_INTERRUPTED, EVENT_INSTANCE_ERROR, } from './constant';
5
- import { ErrorCode, getErrorMsg } from './constant/logCode';
6
- export class FlowModel {
7
- constructor({ nodeModelMap, recorder, context = {}, globalData = {}, startNodeType = 'StartNode', }) {
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
13
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
14
+ return new (P || (P = Promise))(function (resolve, reject) {
15
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
16
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
17
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
18
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
19
+ });
20
+ };
21
+ var __generator = (this && this.__generator) || function (thisArg, body) {
22
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
23
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
24
+ function verb(n) { return function (v) { return step([n, v]); }; }
25
+ function step(op) {
26
+ if (f) throw new TypeError("Generator is already executing.");
27
+ while (_) try {
28
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
29
+ if (y = 0, t) op = [op[0] & 2, t.value];
30
+ switch (op[0]) {
31
+ case 0: case 1: t = op; break;
32
+ case 4: _.label++; return { value: op[1], done: false };
33
+ case 5: _.label++; y = op[1]; op = [0]; continue;
34
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
35
+ default:
36
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
37
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
38
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
39
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
40
+ if (t[2]) _.ops.pop();
41
+ _.trys.pop(); continue;
42
+ }
43
+ op = body.call(thisArg, _);
44
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
45
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
46
+ }
47
+ };
48
+ import { EVENT_INSTANCE_COMPLETE, EVENT_INSTANCE_INTERRUPTED, EVENT_INSTANCE_ERROR, } from './constant/constant';
49
+ import { createExecId } from './util/ID';
50
+ import Scheduler from './Scheduler';
51
+ import { ErrorCode, getErrorMsg } from './constant/LogCode';
52
+ var FlowModel = /** @class */ (function () {
53
+ function FlowModel(_a) {
54
+ var _this = this;
55
+ var nodeModelMap = _a.nodeModelMap, recorder = _a.recorder, _b = _a.context, context = _b === void 0 ? {} : _b, _c = _a.globalData, globalData = _c === void 0 ? {} : _c, _d = _a.startNodeType, startNodeType = _d === void 0 ? 'StartNode' : _d;
8
56
  /**
9
- * 当前流程模型中的所有节点,边会被转换成节点的 incoming outgoing 属性
57
+ * 当前流程模型中的所有节点,边会被转换成节点的incoming和outgoing属性。
10
58
  */
11
59
  this.nodeConfigMap = new Map();
12
60
  /**
13
- * 当前流程中开始节点组成的数组
61
+ * 当前流程中开始节点组成的数组。
14
62
  */
15
63
  this.startNodes = [];
16
64
  /**
17
- * 用于存储全局数据,最终会传递给每个节点
65
+ * 用于存储全局数据,可以在流程中共享。
18
66
  */
19
67
  this.globalData = {};
20
- // 流程包含的节点类型 ??? 在 load 代码中又初始化了一遍,为什么要传进来
68
+ // 流程包含的节点类型
21
69
  this.nodeModelMap = nodeModelMap;
22
70
  // 需要执行的队列
23
71
  this.executeList = [];
@@ -25,25 +73,28 @@ export class FlowModel {
25
73
  this.executingInstance = null;
26
74
  // 外部传入的上下文,最终会传递给每个节点
27
75
  this.context = context;
28
- // 用于存储全局数据,可以在流程中共享
76
+ // 用于存储全局数据,可以在流程中共享。
29
77
  this.globalData = globalData;
30
- // 开始节点类型,在执行流程时,会从这些节点开始执行
78
+ // 开始节点类型,在执行流程时,会从这些节点开始执行。
31
79
  this.startNodeType = startNodeType;
32
80
  this.isRunning = false;
33
81
  this.scheduler = new Scheduler({
34
82
  flowModel: this,
35
- recorder,
83
+ recorder: recorder,
36
84
  });
37
- this.scheduler.on(EVENT_INSTANCE_COMPLETE, (result) => {
38
- this.onExecuteFinished(result);
85
+ this.scheduler.on(EVENT_INSTANCE_COMPLETE, function (result) {
86
+ _this.onExecuteFinished(result);
39
87
  });
40
- this.scheduler.on(EVENT_INSTANCE_INTERRUPTED, (result) => {
41
- this.onExecuteFinished(result);
88
+ this.scheduler.on(EVENT_INSTANCE_INTERRUPTED, function (result) {
89
+ _this.onExecuteFinished(result);
42
90
  });
43
- this.scheduler.on(EVENT_INSTANCE_ERROR, (result) => {
44
- this.onExecuteFinished(result);
91
+ this.scheduler.on(EVENT_INSTANCE_ERROR, function (result) {
92
+ _this.onExecuteFinished(result);
45
93
  });
46
94
  }
95
+ FlowModel.prototype.setStartNodeType = function (startNodeType) {
96
+ this.startNodeType = startNodeType;
97
+ };
47
98
  /**
48
99
  * 解析LogicFlow图数据,将nodes和edges转换成节点格式。
49
100
  * 例如:
@@ -77,29 +128,32 @@ export class FlowModel {
77
128
  * 同时此方法还会找到所有的开始节点,方便后续执行时,从开始节点开始执行。
78
129
  * @param graphData 流程图数据
79
130
  */
80
- load(graphData) {
81
- const { nodes = [], edges = [] } = graphData;
82
- nodes.forEach((node) => {
83
- if (this.nodeModelMap.has(node.type)) {
84
- const nodeConfig = {
131
+ FlowModel.prototype.load = function (graphData) {
132
+ var _this = this;
133
+ var _a = graphData.nodes, nodes = _a === void 0 ? [] : _a, _b = graphData.edges, edges = _b === void 0 ? [] : _b;
134
+ this.startNodes = [];
135
+ this.nodeConfigMap = new Map();
136
+ nodes.forEach(function (node) {
137
+ if (_this.nodeModelMap.has(node.type)) {
138
+ var nodeConfig = {
85
139
  id: node.id,
86
140
  type: node.type,
87
141
  properties: node.properties,
88
142
  incoming: [],
89
143
  outgoing: [],
90
144
  };
91
- this.nodeConfigMap.set(node.id, nodeConfig);
92
- if (node.type === this.startNodeType) {
93
- this.startNodes.push(nodeConfig);
145
+ _this.nodeConfigMap.set(node.id, nodeConfig);
146
+ if (node.type === _this.startNodeType) {
147
+ _this.startNodes.push(nodeConfig);
94
148
  }
95
149
  }
96
150
  else {
97
- console.warn(`未识别的节点类型:${node.type}`);
151
+ console.warn("\u672A\u8BC6\u522B\u7684\u8282\u70B9\u7C7B\u578B: " + node.type);
98
152
  }
99
153
  });
100
- edges.forEach((edge) => {
101
- const sourceNode = this.nodeConfigMap.get(edge.sourceNodeId);
102
- const targetNode = this.nodeConfigMap.get(edge.targetNodeId);
154
+ edges.forEach(function (edge) {
155
+ var sourceNode = _this.nodeConfigMap.get(edge.sourceNodeId);
156
+ var targetNode = _this.nodeConfigMap.get(edge.targetNodeId);
103
157
  if (sourceNode) {
104
158
  sourceNode.outgoing.push({
105
159
  id: edge.id,
@@ -107,7 +161,7 @@ export class FlowModel {
107
161
  target: edge.targetNodeId,
108
162
  });
109
163
  }
110
- if (targetNode && targetNode.type !== this.startNodeType) {
164
+ if (targetNode && targetNode.type !== _this.startNodeType) {
111
165
  targetNode.incoming.push({
112
166
  id: edge.id,
113
167
  properties: edge.properties,
@@ -115,118 +169,107 @@ export class FlowModel {
115
169
  });
116
170
  }
117
171
  });
118
- }
172
+ };
173
+ /**
174
+ * 执行流程, 每次执行都会生成一个唯一的executionId,用于区分不同的执行。
175
+ * 同一次执行,这次执行内部的节点执行顺序为并行。内部并行是为了避免异步节点阻塞其他节点的执行。
176
+ * 多次执行,多次执行之间为串行,这里选择串行的原因是避免多次执行之间的数据冲突。
177
+ * example:
178
+ * 一个流程存在着两个开始节点,A和B,A和B的下一个节点都是C,C的下两个节点是D和E。
179
+ * 外部分别触发了A和B的执行,那么A和B的执行是串行的(也就是需要A执行完成后再执行B),但是D和E的执行是并行的。
180
+ * 如果希望A和B的执行是并行的,就不能使用同一个流程模型执行,应该初始化两个。
181
+ * TODO: 去掉此处的对列,直接使用调度器的队列。
182
+ */
183
+ FlowModel.prototype.execute = function (params) {
184
+ return __awaiter(this, void 0, void 0, function () {
185
+ return __generator(this, function (_a) {
186
+ this.createExecution(params);
187
+ return [2 /*return*/];
188
+ });
189
+ });
190
+ };
191
+ FlowModel.prototype.resume = function (params) {
192
+ return __awaiter(this, void 0, void 0, function () {
193
+ return __generator(this, function (_a) {
194
+ this.createExecution(params);
195
+ return [2 /*return*/];
196
+ });
197
+ });
198
+ };
199
+ /**
200
+ * 创建节点实例, 每个节点实例都会有一个唯一的actionId。
201
+ * 通过executionId、nodeId、actionId可以唯一确定一个节点的某一次执行。
202
+ * @param nodeId 节点Id
203
+ * @returns 节点示例
204
+ */
205
+ FlowModel.prototype.createAction = function (nodeId) {
206
+ var nodeConfig = this.nodeConfigMap.get(nodeId);
207
+ var NodeModel = this.nodeModelMap.get(nodeConfig.type);
208
+ var action = new NodeModel({
209
+ nodeConfig: nodeConfig,
210
+ globalData: this.globalData,
211
+ context: this.context,
212
+ });
213
+ return action;
214
+ };
215
+ /**
216
+ * 更新流程全局数据
217
+ */
218
+ FlowModel.prototype.updateGlobalData = function (data) {
219
+ this.globalData = __assign(__assign({}, this.globalData), data);
220
+ };
221
+ /**
222
+ * 在执行完成后,通知外部此次执行完成。
223
+ * 如果还存在待执行的任务,那么继续执行。
224
+ */
225
+ FlowModel.prototype.onExecuteFinished = function (result) {
226
+ var index = this.executeList.findIndex(function (i) { return i.executionId === result.executionId; });
227
+ if (index !== -1) {
228
+ var callback = this.executeList[index].callback;
229
+ this.executeList.splice(index, 1);
230
+ callback && callback(result);
231
+ }
232
+ };
119
233
  /**
120
234
  * 从待执行队列中取出需要执行的内容。
121
- * 会依次判断是否有 actionId、nodeId、executionId。
122
- * 若存在 actionId,那么表示恢复执行
123
- * 若存在 nodeId,那么表示从指定节点开始执行
124
- * 若都不存在,那么新建一个 executionId,从开始节点开始执行
125
- * @private
235
+ * 会依次判断是否有taskId、nodeId、executionId。
236
+ * 若存在taskId,那么表示恢复执行。
237
+ * 若存在nodeId,那么表示从指定节点开始执行。
238
+ * 若都不存在,那么新建一个executionId,从开始节点开始执行。
126
239
  */
127
- createExecution(execParam) {
128
- var _a;
129
- this.executeList.push(execParam);
130
- // 如果有 actionId,则表示恢复执行
131
- // TODO: 待测试,确认该流程
132
- if (execParam.actionId && execParam.nodeId && execParam.executionId) {
240
+ FlowModel.prototype.createExecution = function (execParams) {
241
+ var _this = this;
242
+ this.executeList.push(execParams);
243
+ // 如果有taskId,那么表示恢复执行
244
+ if (execParams.actionId && execParams.executionId && execParams.nodeId) {
133
245
  this.scheduler.resume({
134
- executionId: execParam.executionId,
135
- actionId: execParam.actionId,
136
- nodeId: execParam.nodeId,
137
- data: execParam.data,
246
+ executionId: execParams.executionId,
247
+ actionId: execParams.actionId,
248
+ nodeId: execParams.nodeId,
249
+ data: execParams.data,
138
250
  });
139
251
  return;
140
252
  }
141
- // 否则,判断 executionId 是否存在,使用 executionId 或创建新的 execution,从开始节点开始执行
142
- // const executionId = execParam?.executionId || createExecId()
143
- const executionId = createExecId();
144
- execParam.executionId = executionId;
145
- // 当指定了具体需要执行的节点时,执行下面方法
146
- if (execParam === null || execParam === void 0 ? void 0 : execParam.nodeId) {
147
- const nodeConfig = this.nodeConfigMap.get(execParam.nodeId);
253
+ var executionId = createExecId();
254
+ execParams.executionId = executionId;
255
+ if (execParams.nodeId) {
256
+ var nodeConfig = this.nodeConfigMap.get(execParams.nodeId);
148
257
  if (!nodeConfig) {
149
- (_a = execParam === null || execParam === void 0 ? void 0 : execParam.onError) === null || _a === void 0 ? void 0 : _a.call(execParam, new Error(`${getErrorMsg(ErrorCode.NONE_NODE_ID)}(${execParam.nodeId})`));
258
+ execParams.onError(new Error(getErrorMsg(ErrorCode.NONE_NODE_ID) + "(" + execParams.nodeId + ")"));
150
259
  return;
151
260
  }
152
- // 当指定了开始节点,且该节点存在,则直接以这个节点开始执行
153
261
  this.startNodes = [nodeConfig];
154
262
  }
155
- this.startNodes.forEach((startNode) => {
156
- this.scheduler.addAction({
157
- executionId,
263
+ this.startNodes.forEach(function (startNode) {
264
+ _this.scheduler.addAction({
265
+ executionId: executionId,
158
266
  nodeId: startNode.id,
159
267
  });
160
268
  });
161
- // 所有的开始节点都执行
162
269
  this.scheduler.run({
163
- executionId,
270
+ executionId: executionId,
164
271
  });
165
- }
166
- /**
167
- * 执行流程,每次执行都会生成一个唯一的 executionId,用于区分不同的执行。
168
- * 同一次执行,这次执行内部的节点执行顺序为并行。内部并行是为了避免异步节点阻塞其他节点的执行
169
- * 多次执行,多次执行之间为串行,这里选择串行的原因是避免多次执行之间的数据冲突。
170
- * 例如:
171
- * 一个流程存在两个开始节点,A 和 B,A 和 B 的下一个节点都是 C,C 的下两个节点是 D 和 E
172
- * 外部分别触发了 A 和 B 的执行,那么 A 和 B 的执行是串行(即 A 执行完再执行 B),但是 D 和 E 的执行是并行的。
173
- * 如果希望 A 和 B 的执行时并行的,就不能使用同一个流程模型执行,应该初始化两个。
174
- * 下面直接使用调度器的队列
175
- * @param params
176
- */
177
- execute(params) {
178
- return __awaiter(this, void 0, void 0, function* () {
179
- this.createExecution(params);
180
- });
181
- }
182
- resume(params) {
183
- return __awaiter(this, void 0, void 0, function* () {
184
- this.createExecution(params);
185
- });
186
- }
187
- /**
188
- * 创建节点实例,每个节点实例都会有一个唯一的 actionId
189
- * 通过 executionId, nodeId, actionId 可以唯一确定一个节点的某一次执行
190
- * @param nodeId
191
- * @returns 节点实例
192
- */
193
- // TODO: 确认下面这种场景,类型如何定义
194
- createAction(nodeId) {
195
- const nodeConfig = this.nodeConfigMap.get(nodeId);
196
- if (nodeConfig) {
197
- const NodeModel = this.nodeModelMap.get(nodeConfig.type);
198
- if (!NodeModel) {
199
- throw new Error('该 NodeModel 不存在,抛出异常');
200
- }
201
- return new NodeModel({
202
- nodeConfig,
203
- globalData: this.globalData,
204
- context: this.context,
205
- });
206
- }
207
- }
208
- setStartNodeType(type) {
209
- this.startNodeType = type;
210
- }
211
- updateGlobalData(data) {
212
- // TODO: 数据的合并,是否考虑子项的合并(默认值的替换)
213
- this.globalData = Object.assign(Object.assign({}, this.globalData), data);
214
- return this.globalData;
215
- }
216
- /**
217
- * 在执行完成后,通知外部此次之行完成
218
- * 如果还存在待执行的任务,那么继续执行
219
- * @param result
220
- * @private
221
- */
222
- onExecuteFinished(result) {
223
- const index = this.executeList.findIndex((i) => i.executionId === result.executionId);
224
- if (index > -1) {
225
- const { callback } = this.executeList[index];
226
- this.executeList.splice(index, 1);
227
- callback === null || callback === void 0 ? void 0 : callback(result);
228
- }
229
- }
230
- }
272
+ };
273
+ return FlowModel;
274
+ }());
231
275
  export default FlowModel;
232
- //# sourceMappingURL=FlowModel.js.map
package/es/Scheduler.d.ts CHANGED
@@ -1,78 +1,62 @@
1
- import { Engine } from '.';
2
- import Recorder from './recorder';
3
- import FlowModel from './FlowModel';
4
1
  import EventEmitter from './EventEmitter';
2
+ import type { ActionParam, NodeParam, ResumeParam } from './types.d';
3
+ import type FlowModel from './FlowModel';
4
+ import type Recorder from './recorder';
5
+ declare type ActionParamMap = Map<string, ActionParam>;
6
+ declare type ExecutionId = string;
5
7
  /**
6
8
  * 调度器
7
9
  * 通过一个队列维护需要执行的节点,一个集合维护正在执行的节点
8
10
  */
9
- export declare class Scheduler extends EventEmitter {
11
+ export default class Scheduler extends EventEmitter {
10
12
  /**
11
13
  * 当前需要执行的节点队列
12
14
  */
13
- nodeQueueMap: Map<Engine.Key, Engine.NodeParam[]>;
15
+ nodeQueueMap: Map<ExecutionId, NodeParam[]>;
14
16
  /**
15
17
  * 当前正在执行的节点集合
16
- * 在每个节点执行完成后,会从集合中删除
17
- * 同时会判断次集合中是否还存在和此节点相同的 executionId,如果不存在,说明该流程已经执行完成
18
+ * 在每个节点执行完成后,会从集合中删除。
19
+ * 同时会判断此集合中是否还存在和此节点相同的executionId,如果不存在,说明此流程已经执行完成。
18
20
  */
19
- actionRunningMap: Map<Engine.Key, Scheduler.ActionParamMap>;
21
+ actionRunningMap: Map<ExecutionId, ActionParamMap>;
20
22
  /**
21
- * 流程模型,用于创建节点模型
23
+ * 流程模型,用于创建节点模型。
22
24
  */
23
25
  flowModel: FlowModel;
24
26
  /**
25
27
  * 执行记录存储器
26
- * 用于存储节点执行的结果
28
+ * 用于存储节点执行的结果。
27
29
  */
28
- recorder?: Recorder;
29
- constructor(config: Scheduler.ISchedulerProps);
30
+ recorder: Recorder;
31
+ constructor(config: any);
30
32
  /**
31
33
  * 添加一个任务到队列中。
32
- * 1. 由流程模型将所有的开始及诶带你添加到队列中
33
- * 2. 当一个节点执行完成后,将后续的节点添加到队列中
34
- * @param nodeParam
34
+ * 1. 由流程模型将所有的开始节点添加到队列中。
35
+ * 2. 当一个节点执行完成后,将后续的节点添加到队列中。
35
36
  */
36
- addAction(nodeParam: Engine.NodeParam): void;
37
- private pushActionToRunningMap;
38
- private removeActionFromRunningMap;
39
- /**
40
- * 为了防止多次添加导致
41
- * @param actionParam
42
- */
43
- private saveActionResult;
44
- private hasRunningAction;
37
+ addAction(nodeParam: NodeParam): void;
45
38
  /**
46
39
  * 调度器执行下一个任务
47
- * 1. 提供给流程模型,用户开始执行第一个任务
48
- * 2. 内部任务执行完成后,调用此方法继续执行下一个任务
49
- * 3. 当判断没有可以继续执行的任务后,触发流程结束事件
50
- * @param runParam
40
+ * 1. 提供给流程模型,用户开始执行第一个任务。
41
+ * 2. 内部任务执行完成后,调用此方法继续执行下一个任务。
42
+ * 3. 当判断没有可以继续执行的任务后,触发流程结束事件。
51
43
  */
52
- run(runParam: Scheduler.ActionParam): void;
53
- private next;
44
+ run(runParams: {
45
+ executionId: string;
46
+ [key: string]: any;
47
+ }): void;
54
48
  /**
55
- * 恢复某个任务的执行
56
- * 可以自定义节点手动实现流程中断,然后通过此方法恢复流程的执行
57
- * @param resumeParam
49
+ * 恢复某个任务的执行。
50
+ * 可以自定义节点手动实现流程中断,然后通过此方法恢复流程的执行。
58
51
  */
59
- resume(resumeParam: Engine.ResumeParam): Promise<void>;
52
+ resume(resumeParam: ResumeParam): Promise<void>;
53
+ private pushActionToRunningMap;
54
+ private removeActionFromRunningMap;
55
+ private hasRunningAction;
56
+ private exec;
60
57
  private interrupted;
61
58
  private error;
62
- private exec;
63
- }
64
- export declare namespace Scheduler {
65
- type ActionParam = {
66
- executionId: Engine.Key;
67
- actionId?: Engine.Key;
68
- nodeId?: Engine.Key;
69
- data?: Record<string, unknown>;
70
- [key: string]: unknown;
71
- };
72
- type ActionParamMap = Map<Engine.Key, ActionParam>;
73
- interface ISchedulerProps {
74
- flowModel: FlowModel;
75
- recorder?: Recorder;
76
- }
59
+ private next;
60
+ private saveActionResult;
77
61
  }
78
- export default Scheduler;
62
+ export {};