@logicflow/engine 0.0.10 → 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/README.md CHANGED
@@ -1,13 +1,10 @@
1
- # @logicflow/engine
1
+ # engine
2
2
 
3
3
  一个可以在JavaScript环境执行的流程引擎
4
4
 
5
5
  ## 使用方式
6
6
 
7
7
  ```shell
8
- pnpm run test
8
+ npm test
9
9
  ```
10
10
 
11
- ## 参考资料
12
- ![package.json 中 browser. module, main 字段优先级](https://github.com/SunshowerC/blog/issues/8)
13
- ![编写兼容 nodejs/浏览器的库](https://blog.rxliuli.com/p/b8a95af9134a488e9d94463bd18768c9/)
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var EventEmitter = /** @class */ (function () {
4
+ function EventEmitter() {
5
+ this._events = {};
6
+ }
7
+ EventEmitter.prototype.on = function (evKey, callback, once) {
8
+ if (once === void 0) { once = false; }
9
+ evKey = evKey.trim();
10
+ if (!this._events[evKey]) {
11
+ this._events[evKey] = [];
12
+ }
13
+ this._events[evKey].push({
14
+ callback: callback,
15
+ once: !!once,
16
+ });
17
+ };
18
+ EventEmitter.prototype.emit = function (evt, eventArgs) {
19
+ var _this = this;
20
+ var events = this._events[evt] || [];
21
+ // 实际的处理 emit 方法
22
+ var doEmit = function (es) {
23
+ var length = es.length;
24
+ for (var i = 0; i < length; i++) {
25
+ if (!es[i]) {
26
+ // eslint-disable-next-line no-continue
27
+ continue;
28
+ }
29
+ var _a = es[i], callback = _a.callback, once = _a.once;
30
+ if (once) {
31
+ es.splice(i, 1);
32
+ if (es.length === 0) {
33
+ delete _this._events[evt];
34
+ }
35
+ length--;
36
+ i--;
37
+ }
38
+ callback.apply(_this, [eventArgs]);
39
+ }
40
+ };
41
+ doEmit(events);
42
+ };
43
+ EventEmitter.prototype.off = function (evt, callback) {
44
+ if (!evt) {
45
+ // evt 为空全部清除
46
+ this._events = {};
47
+ }
48
+ if (!callback) {
49
+ // evt 存在,callback 为空,清除事件所有方法
50
+ delete this._events[evt];
51
+ }
52
+ else {
53
+ // evt 存在,callback 存在,清除匹配的
54
+ var events = this._events[evt] || [];
55
+ var length_1 = events.length;
56
+ for (var i = 0; i < length_1; i++) {
57
+ if (events[i].callback === callback) {
58
+ events.splice(i, 1);
59
+ length_1--;
60
+ i--;
61
+ }
62
+ }
63
+ if (events.length === 0) {
64
+ delete this._events[evt];
65
+ }
66
+ }
67
+ };
68
+ return EventEmitter;
69
+ }());
70
+ exports.default = EventEmitter;
@@ -0,0 +1,277 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (_) try {
29
+ 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;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ Object.defineProperty(exports, "__esModule", { value: true });
50
+ var constant_1 = require("./constant/constant");
51
+ var ID_1 = require("./util/ID");
52
+ var Scheduler_1 = require("./Scheduler");
53
+ var LogCode_1 = require("./constant/LogCode");
54
+ var FlowModel = /** @class */ (function () {
55
+ function FlowModel(_a) {
56
+ var _this = this;
57
+ 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;
58
+ /**
59
+ * 当前流程模型中的所有节点,边会被转换成节点的incoming和outgoing属性。
60
+ */
61
+ this.nodeConfigMap = new Map();
62
+ /**
63
+ * 当前流程中开始节点组成的数组。
64
+ */
65
+ this.startNodes = [];
66
+ /**
67
+ * 用于存储全局数据,可以在流程中共享。
68
+ */
69
+ this.globalData = {};
70
+ // 流程包含的节点类型
71
+ this.nodeModelMap = nodeModelMap;
72
+ // 需要执行的队列
73
+ this.executeList = [];
74
+ // 执行中的任务
75
+ this.executingInstance = null;
76
+ // 外部传入的上下文,最终会传递给每个节点
77
+ this.context = context;
78
+ // 用于存储全局数据,可以在流程中共享。
79
+ this.globalData = globalData;
80
+ // 开始节点类型,在执行流程时,会从这些节点开始执行。
81
+ this.startNodeType = startNodeType;
82
+ this.isRunning = false;
83
+ this.scheduler = new Scheduler_1.default({
84
+ flowModel: this,
85
+ recorder: recorder,
86
+ });
87
+ this.scheduler.on(constant_1.EVENT_INSTANCE_COMPLETE, function (result) {
88
+ _this.onExecuteFinished(result);
89
+ });
90
+ this.scheduler.on(constant_1.EVENT_INSTANCE_INTERRUPTED, function (result) {
91
+ _this.onExecuteFinished(result);
92
+ });
93
+ this.scheduler.on(constant_1.EVENT_INSTANCE_ERROR, function (result) {
94
+ _this.onExecuteFinished(result);
95
+ });
96
+ }
97
+ FlowModel.prototype.setStartNodeType = function (startNodeType) {
98
+ this.startNodeType = startNodeType;
99
+ };
100
+ /**
101
+ * 解析LogicFlow图数据,将nodes和edges转换成节点格式。
102
+ * 例如:
103
+ * graphData: {
104
+ * nodes: [
105
+ * { id: 'node1', type: 'StartNode', properties: {} },
106
+ * { id: 'node2', type: 'TaskNode', properties: {} },
107
+ * ],
108
+ * edges: [
109
+ * { id: 'edge1', sourceNodeId: 'node1', targetNodeId: 'node2', properties: {} },
110
+ * ]
111
+ * }
112
+ * 转换成:
113
+ * nodeConfigMap: {
114
+ * node1: {
115
+ * id: 'node1',
116
+ * type: 'StartNode',
117
+ * properties: {},
118
+ * incoming: [],
119
+ * outgoing: [{ id: 'edge1', properties: {}, target: 'node2' }]
120
+ * },
121
+ * node2: {
122
+ * id: 'node2',
123
+ * type: 'TaskNode',
124
+ * properties: {},
125
+ * incoming: [{ id: 'edge1', properties: {}, source: 'node1' }],
126
+ * outgoing: [],
127
+ * }
128
+ * }
129
+ * 此格式方便后续执行时,根据节点id快速找到节点和执行初始化节点模型。
130
+ * 同时此方法还会找到所有的开始节点,方便后续执行时,从开始节点开始执行。
131
+ * @param graphData 流程图数据
132
+ */
133
+ FlowModel.prototype.load = function (graphData) {
134
+ var _this = this;
135
+ var _a = graphData.nodes, nodes = _a === void 0 ? [] : _a, _b = graphData.edges, edges = _b === void 0 ? [] : _b;
136
+ this.startNodes = [];
137
+ this.nodeConfigMap = new Map();
138
+ nodes.forEach(function (node) {
139
+ if (_this.nodeModelMap.has(node.type)) {
140
+ var nodeConfig = {
141
+ id: node.id,
142
+ type: node.type,
143
+ properties: node.properties,
144
+ incoming: [],
145
+ outgoing: [],
146
+ };
147
+ _this.nodeConfigMap.set(node.id, nodeConfig);
148
+ if (node.type === _this.startNodeType) {
149
+ _this.startNodes.push(nodeConfig);
150
+ }
151
+ }
152
+ else {
153
+ console.warn("\u672A\u8BC6\u522B\u7684\u8282\u70B9\u7C7B\u578B: " + node.type);
154
+ }
155
+ });
156
+ edges.forEach(function (edge) {
157
+ var sourceNode = _this.nodeConfigMap.get(edge.sourceNodeId);
158
+ var targetNode = _this.nodeConfigMap.get(edge.targetNodeId);
159
+ if (sourceNode) {
160
+ sourceNode.outgoing.push({
161
+ id: edge.id,
162
+ properties: edge.properties,
163
+ target: edge.targetNodeId,
164
+ });
165
+ }
166
+ if (targetNode && targetNode.type !== _this.startNodeType) {
167
+ targetNode.incoming.push({
168
+ id: edge.id,
169
+ properties: edge.properties,
170
+ source: edge.sourceNodeId,
171
+ });
172
+ }
173
+ });
174
+ };
175
+ /**
176
+ * 执行流程, 每次执行都会生成一个唯一的executionId,用于区分不同的执行。
177
+ * 同一次执行,这次执行内部的节点执行顺序为并行。内部并行是为了避免异步节点阻塞其他节点的执行。
178
+ * 多次执行,多次执行之间为串行,这里选择串行的原因是避免多次执行之间的数据冲突。
179
+ * example:
180
+ * 一个流程存在着两个开始节点,A和B,A和B的下一个节点都是C,C的下两个节点是D和E。
181
+ * 外部分别触发了A和B的执行,那么A和B的执行是串行的(也就是需要A执行完成后再执行B),但是D和E的执行是并行的。
182
+ * 如果希望A和B的执行是并行的,就不能使用同一个流程模型执行,应该初始化两个。
183
+ * TODO: 去掉此处的对列,直接使用调度器的队列。
184
+ */
185
+ FlowModel.prototype.execute = function (params) {
186
+ return __awaiter(this, void 0, void 0, function () {
187
+ return __generator(this, function (_a) {
188
+ this.createExecution(params);
189
+ return [2 /*return*/];
190
+ });
191
+ });
192
+ };
193
+ FlowModel.prototype.resume = function (params) {
194
+ return __awaiter(this, void 0, void 0, function () {
195
+ return __generator(this, function (_a) {
196
+ this.createExecution(params);
197
+ return [2 /*return*/];
198
+ });
199
+ });
200
+ };
201
+ /**
202
+ * 创建节点实例, 每个节点实例都会有一个唯一的actionId。
203
+ * 通过executionId、nodeId、actionId可以唯一确定一个节点的某一次执行。
204
+ * @param nodeId 节点Id
205
+ * @returns 节点示例
206
+ */
207
+ FlowModel.prototype.createAction = function (nodeId) {
208
+ var nodeConfig = this.nodeConfigMap.get(nodeId);
209
+ var NodeModel = this.nodeModelMap.get(nodeConfig.type);
210
+ var action = new NodeModel({
211
+ nodeConfig: nodeConfig,
212
+ globalData: this.globalData,
213
+ context: this.context,
214
+ });
215
+ return action;
216
+ };
217
+ /**
218
+ * 更新流程全局数据
219
+ */
220
+ FlowModel.prototype.updateGlobalData = function (data) {
221
+ this.globalData = __assign(__assign({}, this.globalData), data);
222
+ };
223
+ /**
224
+ * 在执行完成后,通知外部此次执行完成。
225
+ * 如果还存在待执行的任务,那么继续执行。
226
+ */
227
+ FlowModel.prototype.onExecuteFinished = function (result) {
228
+ var index = this.executeList.findIndex(function (i) { return i.executionId === result.executionId; });
229
+ if (index !== -1) {
230
+ var callback = this.executeList[index].callback;
231
+ this.executeList.splice(index, 1);
232
+ callback && callback(result);
233
+ }
234
+ };
235
+ /**
236
+ * 从待执行队列中取出需要执行的内容。
237
+ * 会依次判断是否有taskId、nodeId、executionId。
238
+ * 若存在taskId,那么表示恢复执行。
239
+ * 若存在nodeId,那么表示从指定节点开始执行。
240
+ * 若都不存在,那么新建一个executionId,从开始节点开始执行。
241
+ */
242
+ FlowModel.prototype.createExecution = function (execParams) {
243
+ var _this = this;
244
+ this.executeList.push(execParams);
245
+ // 如果有taskId,那么表示恢复执行
246
+ if (execParams.actionId && execParams.executionId && execParams.nodeId) {
247
+ this.scheduler.resume({
248
+ executionId: execParams.executionId,
249
+ actionId: execParams.actionId,
250
+ nodeId: execParams.nodeId,
251
+ data: execParams.data,
252
+ });
253
+ return;
254
+ }
255
+ var executionId = ID_1.createExecId();
256
+ execParams.executionId = executionId;
257
+ if (execParams.nodeId) {
258
+ var nodeConfig = this.nodeConfigMap.get(execParams.nodeId);
259
+ if (!nodeConfig) {
260
+ execParams.onError(new Error(LogCode_1.getErrorMsg(LogCode_1.ErrorCode.NONE_NODE_ID) + "(" + execParams.nodeId + ")"));
261
+ return;
262
+ }
263
+ this.startNodes = [nodeConfig];
264
+ }
265
+ this.startNodes.forEach(function (startNode) {
266
+ _this.scheduler.addAction({
267
+ executionId: executionId,
268
+ nodeId: startNode.id,
269
+ });
270
+ });
271
+ this.scheduler.run({
272
+ executionId: executionId,
273
+ });
274
+ };
275
+ return FlowModel;
276
+ }());
277
+ exports.default = FlowModel;
@@ -0,0 +1,252 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ extendStatics(d, b);
11
+ function __() { this.constructor = d; }
12
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13
+ };
14
+ })();
15
+ var __assign = (this && this.__assign) || function () {
16
+ __assign = Object.assign || function(t) {
17
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
18
+ s = arguments[i];
19
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
20
+ t[p] = s[p];
21
+ }
22
+ return t;
23
+ };
24
+ return __assign.apply(this, arguments);
25
+ };
26
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
27
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
28
+ return new (P || (P = Promise))(function (resolve, reject) {
29
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
30
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
31
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
32
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
33
+ });
34
+ };
35
+ var __generator = (this && this.__generator) || function (thisArg, body) {
36
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
37
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
38
+ function verb(n) { return function (v) { return step([n, v]); }; }
39
+ function step(op) {
40
+ if (f) throw new TypeError("Generator is already executing.");
41
+ while (_) try {
42
+ 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;
43
+ if (y = 0, t) op = [op[0] & 2, t.value];
44
+ switch (op[0]) {
45
+ case 0: case 1: t = op; break;
46
+ case 4: _.label++; return { value: op[1], done: false };
47
+ case 5: _.label++; y = op[1]; op = [0]; continue;
48
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
49
+ default:
50
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
51
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
52
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
53
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
54
+ if (t[2]) _.ops.pop();
55
+ _.trys.pop(); continue;
56
+ }
57
+ op = body.call(thisArg, _);
58
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
59
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
60
+ }
61
+ };
62
+ Object.defineProperty(exports, "__esModule", { value: true });
63
+ var EventEmitter_1 = require("./EventEmitter");
64
+ var constant_1 = require("./constant/constant");
65
+ var ID_1 = require("./util/ID");
66
+ /**
67
+ * 调度器
68
+ * 通过一个队列维护需要执行的节点,一个集合维护正在执行的节点
69
+ */
70
+ var Scheduler = /** @class */ (function (_super) {
71
+ __extends(Scheduler, _super);
72
+ function Scheduler(config) {
73
+ var _this = _super.call(this) || this;
74
+ _this.nodeQueueMap = new Map();
75
+ _this.actionRunningMap = new Map();
76
+ _this.flowModel = config.flowModel;
77
+ _this.recorder = config.recorder;
78
+ return _this;
79
+ }
80
+ /**
81
+ * 添加一个任务到队列中。
82
+ * 1. 由流程模型将所有的开始节点添加到队列中。
83
+ * 2. 当一个节点执行完成后,将后续的节点添加到队列中。
84
+ */
85
+ Scheduler.prototype.addAction = function (nodeParam) {
86
+ var executionId = nodeParam.executionId;
87
+ if (!this.nodeQueueMap.has(executionId)) {
88
+ this.nodeQueueMap.set(executionId, []);
89
+ }
90
+ var currentActionQueue = this.nodeQueueMap.get(executionId);
91
+ currentActionQueue.push(nodeParam);
92
+ };
93
+ /**
94
+ * 调度器执行下一个任务
95
+ * 1. 提供给流程模型,用户开始执行第一个任务。
96
+ * 2. 内部任务执行完成后,调用此方法继续执行下一个任务。
97
+ * 3. 当判断没有可以继续执行的任务后,触发流程结束事件。
98
+ */
99
+ Scheduler.prototype.run = function (runParams) {
100
+ var nodeQueue = this.nodeQueueMap.get(runParams.executionId);
101
+ // 将同一个executionId当前待执行的节点一起执行
102
+ // 避免出现某一个节点执行时间过长,导致其他节点等待时间过长。
103
+ while (nodeQueue.length) {
104
+ var currentNode = nodeQueue.pop();
105
+ var actionId = ID_1.createActionId();
106
+ var actionParam = __assign(__assign({}, currentNode), { actionId: actionId });
107
+ this.pushActionToRunningMap(actionParam);
108
+ this.exec(actionParam);
109
+ }
110
+ if (!this.hasRunningAction(runParams.executionId)) {
111
+ // 当一个流程在nodeQueueMap和actionRunningMap中都不存在执行的节点时,说明这个流程已经执行完成。
112
+ this.emit(constant_1.EVENT_INSTANCE_COMPLETE, __assign(__assign({}, runParams), { status: constant_1.FlowStatus.COMPLETED }));
113
+ }
114
+ };
115
+ /**
116
+ * 恢复某个任务的执行。
117
+ * 可以自定义节点手动实现流程中断,然后通过此方法恢复流程的执行。
118
+ */
119
+ Scheduler.prototype.resume = function (resumeParam) {
120
+ return __awaiter(this, void 0, void 0, function () {
121
+ var model;
122
+ return __generator(this, function (_a) {
123
+ switch (_a.label) {
124
+ case 0:
125
+ this.pushActionToRunningMap({
126
+ executionId: resumeParam.executionId,
127
+ nodeId: resumeParam.nodeId,
128
+ actionId: resumeParam.actionId,
129
+ });
130
+ model = this.flowModel.createAction(resumeParam.nodeId);
131
+ return [4 /*yield*/, model.resume(__assign(__assign({}, resumeParam), { next: this.next.bind(this) }))];
132
+ case 1:
133
+ _a.sent();
134
+ return [2 /*return*/];
135
+ }
136
+ });
137
+ });
138
+ };
139
+ Scheduler.prototype.pushActionToRunningMap = function (actionParam) {
140
+ var executionId = actionParam.executionId, actionId = actionParam.actionId;
141
+ if (!this.actionRunningMap.has(executionId)) {
142
+ var runningMap = new Map();
143
+ this.actionRunningMap.set(executionId, runningMap);
144
+ }
145
+ this.actionRunningMap.get(executionId).set(actionId, actionParam);
146
+ };
147
+ Scheduler.prototype.removeActionFromRunningMap = function (actionParam) {
148
+ var executionId = actionParam.executionId, actionId = actionParam.actionId;
149
+ if (!actionId)
150
+ return;
151
+ var runningMap = this.actionRunningMap.get(executionId);
152
+ if (!runningMap)
153
+ return;
154
+ runningMap.delete(actionId);
155
+ };
156
+ Scheduler.prototype.hasRunningAction = function (executionId) {
157
+ var runningMap = this.actionRunningMap.get(executionId);
158
+ if (!runningMap)
159
+ return false;
160
+ if (runningMap.size === 0) {
161
+ this.actionRunningMap.delete(executionId);
162
+ return false;
163
+ }
164
+ return true;
165
+ };
166
+ Scheduler.prototype.exec = function (actionParam) {
167
+ return __awaiter(this, void 0, void 0, function () {
168
+ var model, execResult;
169
+ return __generator(this, function (_a) {
170
+ switch (_a.label) {
171
+ case 0:
172
+ model = this.flowModel.createAction(actionParam.nodeId);
173
+ return [4 /*yield*/, model.execute({
174
+ executionId: actionParam.executionId,
175
+ actionId: actionParam.actionId,
176
+ nodeId: actionParam.nodeId,
177
+ next: this.next.bind(this),
178
+ })];
179
+ case 1:
180
+ execResult = _a.sent();
181
+ if (execResult && execResult.status === constant_1.FlowStatus.INTERRUPTED) {
182
+ this.interrupted(execResult);
183
+ this.saveActionResult({
184
+ executionId: actionParam.executionId,
185
+ nodeId: actionParam.nodeId,
186
+ actionId: actionParam.actionId,
187
+ nodeType: execResult.nodeType,
188
+ properties: execResult.properties,
189
+ outgoing: execResult.outgoing,
190
+ status: execResult.status,
191
+ detail: execResult.detail,
192
+ });
193
+ this.removeActionFromRunningMap(actionParam);
194
+ }
195
+ if (execResult && execResult.status === constant_1.FlowStatus.ERROR) {
196
+ this.error(execResult);
197
+ this.saveActionResult({
198
+ executionId: actionParam.executionId,
199
+ nodeId: actionParam.nodeId,
200
+ actionId: actionParam.actionId,
201
+ nodeType: execResult.nodeType,
202
+ properties: execResult.properties,
203
+ outgoing: execResult.outgoing,
204
+ status: execResult.status,
205
+ detail: execResult.detail,
206
+ });
207
+ this.removeActionFromRunningMap(actionParam);
208
+ }
209
+ return [2 /*return*/];
210
+ }
211
+ });
212
+ });
213
+ };
214
+ Scheduler.prototype.interrupted = function (execResult) {
215
+ this.emit(constant_1.EVENT_INSTANCE_INTERRUPTED, execResult);
216
+ };
217
+ Scheduler.prototype.error = function (execResult) {
218
+ this.emit(constant_1.EVENT_INSTANCE_ERROR, execResult);
219
+ };
220
+ Scheduler.prototype.next = function (data) {
221
+ var _this = this;
222
+ if (data.outgoing && data.outgoing.length > 0) {
223
+ data.outgoing.forEach(function (item) {
224
+ if (item.result) {
225
+ _this.addAction({
226
+ executionId: data.executionId,
227
+ nodeId: item.target,
228
+ });
229
+ }
230
+ });
231
+ }
232
+ this.saveActionResult(data);
233
+ this.removeActionFromRunningMap(data);
234
+ this.run(data);
235
+ };
236
+ Scheduler.prototype.saveActionResult = function (data) {
237
+ var _a;
238
+ (_a = this.recorder) === null || _a === void 0 ? void 0 : _a.addActionRecord({
239
+ executionId: data.executionId,
240
+ actionId: data.actionId,
241
+ nodeId: data.nodeId,
242
+ nodeType: data.nodeType,
243
+ timestamp: Date.now(),
244
+ properties: data.properties,
245
+ outgoing: data.outgoing,
246
+ detail: data.detail,
247
+ status: data.status,
248
+ });
249
+ };
250
+ return Scheduler;
251
+ }(EventEmitter_1.default));
252
+ exports.default = Scheduler;
@@ -1,4 +1,5 @@
1
1
  "use strict";
2
+ var _a, _b;
2
3
  Object.defineProperty(exports, "__esModule", { value: true });
3
4
  exports.getWarningMsg = exports.getErrorMsg = exports.WarningCode = exports.ErrorCode = void 0;
4
5
  var ErrorCode;
@@ -16,19 +17,15 @@ var WarningCode;
16
17
  // 表达式判断异常
17
18
  WarningCode[WarningCode["EXPRESSION_EXEC_ERROR"] = 3000] = "EXPRESSION_EXEC_ERROR";
18
19
  })(WarningCode = exports.WarningCode || (exports.WarningCode = {}));
19
- // TODO: 感觉这块可以用个国际化插件,这样配置不同语言加文件就可以了。
20
- const errorMsgMapCn = {
21
- [ErrorCode.NONE_START_NODE]: '未找到入度为0的节点',
22
- [ErrorCode.NONE_NODE_ID]: '流程数据中存在没有此节点',
23
- [ErrorCode.NO_DOCUMENT_BODY]: '找不到document.body, 请在DOM加载完成后再执行',
24
- };
25
- const warningMsgMapCn = {
26
- [WarningCode.NONE_START_NODE_IN_DATA]: '初始化数据中未找到入度为0的节点',
27
- [WarningCode.START_NODE_INCOMING]: '开始节点不允许被连入',
28
- [WarningCode.EXPRESSION_EXEC_ERROR]: '表达式执行异常',
29
- };
30
- const getErrorMsg = (code) => `error[${code}]: ${errorMsgMapCn[code]}`;
31
- exports.getErrorMsg = getErrorMsg;
32
- const getWarningMsg = (code) => `warning[${code}]: ${warningMsgMapCn[code]}`;
33
- exports.getWarningMsg = getWarningMsg;
34
- //# sourceMappingURL=logCode.js.map
20
+ var errorMsgMapCn = (_a = {},
21
+ _a[ErrorCode.NONE_START_NODE] = '未找到入度为0的节点',
22
+ _a[ErrorCode.NONE_NODE_ID] = '流程数据中存在没有此节点',
23
+ _a[ErrorCode.NO_DOCUMENT_BODY] = '找不到document.body, 请在DOM加载完成后再执行',
24
+ _a);
25
+ var warningMsgMapCn = (_b = {},
26
+ _b[WarningCode.NONE_START_NODE_IN_DATA] = '初始化数据中未找到入度为0的节点',
27
+ _b[WarningCode.START_NODE_INCOMING] = '开始节点不允许被连入',
28
+ _b[WarningCode.EXPRESSION_EXEC_ERROR] = '表达式执行异常',
29
+ _b);
30
+ exports.getErrorMsg = function (code) { return "error[" + code + "]: " + errorMsgMapCn[code]; };
31
+ exports.getWarningMsg = function (code) { return "warning[" + code + "]: " + warningMsgMapCn[code]; };
@@ -3,11 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ActionStatus = exports.FlowStatus = exports.EVENT_INSTANCE_ERROR = exports.EVENT_INSTANCE_INTERRUPTED = exports.EVENT_INSTANCE_COMPLETE = exports.BASE_START_NODE = void 0;
4
4
  // baseType
5
5
  exports.BASE_START_NODE = 'start';
6
- // eventType
6
+ // event name
7
7
  exports.EVENT_INSTANCE_COMPLETE = 'instance:complete';
8
8
  exports.EVENT_INSTANCE_INTERRUPTED = 'instance:interrupted';
9
9
  exports.EVENT_INSTANCE_ERROR = 'instance:error';
10
- // flowStatus
10
+ // flow status
11
11
  var FlowStatus;
12
12
  (function (FlowStatus) {
13
13
  FlowStatus["COMPLETED"] = "completed";
@@ -16,11 +16,10 @@ var FlowStatus;
16
16
  FlowStatus["PENDING"] = "pending";
17
17
  FlowStatus["ERROR"] = "error";
18
18
  })(FlowStatus = exports.FlowStatus || (exports.FlowStatus = {}));
19
- // actionStatus
19
+ // action status
20
20
  var ActionStatus;
21
21
  (function (ActionStatus) {
22
22
  ActionStatus["SUCCESS"] = "success";
23
23
  ActionStatus["ERROR"] = "error";
24
24
  ActionStatus["INTERRUPTED"] = "interrupted";
25
25
  })(ActionStatus = exports.ActionStatus || (exports.ActionStatus = {}));
26
- //# sourceMappingURL=index.js.map