@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.
- package/README.md +2 -5
- package/cjs/EventEmitter.js +70 -0
- package/cjs/FlowModel.js +277 -0
- package/cjs/Scheduler.js +252 -0
- package/{lib/constant/logCode.js → cjs/constant/LogCode.js} +13 -16
- package/{lib/constant/index.js → cjs/constant/constant.js} +3 -4
- package/cjs/expression/browserVm.js +81 -0
- package/cjs/expression/index.js +57 -0
- package/cjs/index.js +210 -0
- package/cjs/nodes/BaseNode.js +252 -0
- package/cjs/nodes/StartNode.js +27 -0
- package/cjs/nodes/TaskNode.js +27 -0
- package/cjs/recorder/index.js +168 -0
- package/cjs/util/ID.js +16 -0
- package/cjs/util/global.js +32 -0
- package/{lib/utils → cjs/util}/storage.js +17 -18
- package/es/EventEmitter.d.ts +4 -34
- package/es/EventEmitter.js +48 -70
- package/es/FlowModel.d.ts +73 -75
- package/es/FlowModel.js +173 -130
- package/es/Scheduler.d.ts +34 -50
- package/es/Scheduler.js +209 -134
- package/es/constant/LogCode.js +28 -0
- package/es/constant/{index.js → constant.js} +7 -8
- package/es/expression/browserVm.d.ts +4 -0
- package/es/expression/browserVm.js +76 -0
- package/es/expression/index.d.ts +1 -1
- package/es/expression/index.js +53 -16
- package/es/index.d.ts +38 -135
- package/es/index.js +144 -92
- package/es/nodes/BaseNode.d.ts +110 -0
- package/es/nodes/BaseNode.js +250 -0
- package/es/nodes/{start.d.ts → StartNode.d.ts} +2 -3
- package/es/nodes/StartNode.js +25 -0
- package/es/nodes/{task.d.ts → TaskNode.d.ts} +2 -3
- package/es/nodes/TaskNode.js +25 -0
- package/es/recorder/index.d.ts +10 -36
- package/es/recorder/index.js +135 -82
- package/es/util/ID.js +13 -0
- package/es/util/global.d.ts +5 -0
- package/es/util/global.js +26 -0
- package/es/{utils → util}/storage.js +16 -17
- package/lib/main.js +1 -0
- package/package.json +71 -30
- package/dist/index.js +0 -2
- package/dist/index.js.map +0 -1
- package/es/EventEmitter.js.map +0 -1
- package/es/FlowModel.js.map +0 -1
- package/es/Scheduler.js.map +0 -1
- package/es/constant/index.js.map +0 -1
- package/es/constant/logCode.js +0 -29
- package/es/constant/logCode.js.map +0 -1
- package/es/expression/brewserVm.d.ts +0 -2
- package/es/expression/brewserVm.js +0 -30
- package/es/expression/brewserVm.js.map +0 -1
- package/es/expression/index.js.map +0 -1
- package/es/expression/nodeVm.d.ts +0 -4
- package/es/expression/nodeVm.js +0 -10
- package/es/expression/nodeVm.js.map +0 -1
- package/es/index.js.map +0 -1
- package/es/nodes/base.d.ts +0 -108
- package/es/nodes/base.js +0 -149
- package/es/nodes/base.js.map +0 -1
- package/es/nodes/index.d.ts +0 -3
- package/es/nodes/index.js +0 -4
- package/es/nodes/index.js.map +0 -1
- package/es/nodes/start.js +0 -11
- package/es/nodes/start.js.map +0 -1
- package/es/nodes/task.js +0 -11
- package/es/nodes/task.js.map +0 -1
- package/es/platform/browser/browserVm.d.ts +0 -4
- package/es/platform/browser/browserVm.js +0 -44
- package/es/platform/browser/browserVm.js.map +0 -1
- package/es/platform/browser/index.d.ts +0 -4
- package/es/platform/browser/index.js +0 -23
- package/es/platform/browser/index.js.map +0 -1
- package/es/platform/index.d.ts +0 -1
- package/es/platform/index.js +0 -2
- package/es/platform/index.js.map +0 -1
- package/es/platform/node/index.d.ts +0 -4
- package/es/platform/node/index.js +0 -23
- package/es/platform/node/index.js.map +0 -1
- package/es/platform/node/nodeVm.d.ts +0 -1
- package/es/platform/node/nodeVm.js +0 -9
- package/es/platform/node/nodeVm.js.map +0 -1
- package/es/recorder/index.js.map +0 -1
- package/es/utils/global.d.ts +0 -5
- package/es/utils/global.js +0 -27
- package/es/utils/global.js.map +0 -1
- package/es/utils/id.js +0 -14
- package/es/utils/id.js.map +0 -1
- package/es/utils/index.d.ts +0 -4
- package/es/utils/index.js +0 -5
- package/es/utils/index.js.map +0 -1
- package/es/utils/storage.js.map +0 -1
- package/lib/EventEmitter.d.ts +0 -37
- package/lib/EventEmitter.js +0 -94
- package/lib/EventEmitter.js.map +0 -1
- package/lib/FlowModel.d.ts +0 -146
- package/lib/FlowModel.js +0 -236
- package/lib/FlowModel.js.map +0 -1
- package/lib/Scheduler.d.ts +0 -78
- package/lib/Scheduler.js +0 -179
- package/lib/Scheduler.js.map +0 -1
- package/lib/constant/index.d.ts +0 -16
- package/lib/constant/index.js.map +0 -1
- package/lib/constant/logCode.d.ts +0 -12
- package/lib/constant/logCode.js.map +0 -1
- package/lib/expression/brewserVm.d.ts +0 -2
- package/lib/expression/brewserVm.js +0 -33
- package/lib/expression/brewserVm.js.map +0 -1
- package/lib/expression/index.d.ts +0 -2
- package/lib/expression/index.js +0 -20
- package/lib/expression/index.js.map +0 -1
- package/lib/expression/nodeVm.d.ts +0 -4
- package/lib/expression/nodeVm.js +0 -13
- package/lib/expression/nodeVm.js.map +0 -1
- package/lib/index.d.ts +0 -157
- package/lib/index.js +0 -159
- package/lib/index.js.map +0 -1
- package/lib/nodes/base.d.ts +0 -108
- package/lib/nodes/base.js +0 -152
- package/lib/nodes/base.js.map +0 -1
- package/lib/nodes/index.d.ts +0 -3
- package/lib/nodes/index.js +0 -7
- package/lib/nodes/index.js.map +0 -1
- package/lib/nodes/start.d.ts +0 -6
- package/lib/nodes/start.js +0 -15
- package/lib/nodes/start.js.map +0 -1
- package/lib/nodes/task.d.ts +0 -6
- package/lib/nodes/task.js +0 -15
- package/lib/nodes/task.js.map +0 -1
- package/lib/platform/browser/browserVm.d.ts +0 -4
- package/lib/platform/browser/browserVm.js +0 -49
- package/lib/platform/browser/browserVm.js.map +0 -1
- package/lib/platform/browser/index.d.ts +0 -4
- package/lib/platform/browser/index.js +0 -28
- package/lib/platform/browser/index.js.map +0 -1
- package/lib/platform/index.d.ts +0 -1
- package/lib/platform/index.js +0 -5
- package/lib/platform/index.js.map +0 -1
- package/lib/platform/node/index.d.ts +0 -4
- package/lib/platform/node/index.js +0 -28
- package/lib/platform/node/index.js.map +0 -1
- package/lib/platform/node/nodeVm.d.ts +0 -1
- package/lib/platform/node/nodeVm.js +0 -13
- package/lib/platform/node/nodeVm.js.map +0 -1
- package/lib/recorder/index.d.ts +0 -46
- package/lib/recorder/index.js +0 -117
- package/lib/recorder/index.js.map +0 -1
- package/lib/utils/global.d.ts +0 -5
- package/lib/utils/global.js +0 -31
- package/lib/utils/global.js.map +0 -1
- package/lib/utils/id.d.ts +0 -3
- package/lib/utils/id.js +0 -20
- package/lib/utils/id.js.map +0 -1
- package/lib/utils/index.d.ts +0 -4
- package/lib/utils/index.js +0 -9
- package/lib/utils/index.js.map +0 -1
- package/lib/utils/storage.d.ts +0 -7
- package/lib/utils/storage.js.map +0 -1
- package/src/EventEmitter.ts +0 -103
- package/src/FlowModel.ts +0 -325
- package/src/Scheduler.ts +0 -244
- package/src/constant/index.ts +0 -23
- package/src/constant/logCode.ts +0 -34
- package/src/expression/brewserVm.ts +0 -36
- package/src/expression/index.ts +0 -17
- package/src/expression/nodeVm.ts +0 -14
- package/src/index.ts +0 -300
- package/src/nodes/base.ts +0 -234
- package/src/nodes/index.ts +0 -3
- package/src/nodes/start.ts +0 -8
- package/src/nodes/task.ts +0 -8
- package/src/platform/browser/browserVm.ts +0 -52
- package/src/platform/browser/index.ts +0 -28
- package/src/platform/index.ts +0 -1
- package/src/platform/node/index.ts +0 -28
- package/src/platform/node/nodeVm.ts +0 -12
- package/src/recorder/index.ts +0 -137
- package/src/typings.d.ts +0 -0
- package/src/utils/global.ts +0 -41
- package/src/utils/id.ts +0 -16
- package/src/utils/index.ts +0 -5
- package/src/utils/storage.ts +0 -55
- /package/es/constant/{logCode.d.ts → LogCode.d.ts} +0 -0
- /package/es/constant/{index.d.ts → constant.d.ts} +0 -0
- /package/es/{utils/id.d.ts → util/ID.d.ts} +0 -0
- /package/es/{utils → util}/storage.d.ts +0 -0
package/es/FlowModel.js
CHANGED
|
@@ -1,23 +1,71 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
-
* 当前流程模型中的所有节点,边会被转换成节点的
|
|
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
|
-
// 流程包含的节点类型
|
|
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
|
-
|
|
85
|
+
this.scheduler.on(EVENT_INSTANCE_COMPLETE, function (result) {
|
|
86
|
+
_this.onExecuteFinished(result);
|
|
39
87
|
});
|
|
40
|
-
this.scheduler.on(EVENT_INSTANCE_INTERRUPTED, (result)
|
|
41
|
-
|
|
88
|
+
this.scheduler.on(EVENT_INSTANCE_INTERRUPTED, function (result) {
|
|
89
|
+
_this.onExecuteFinished(result);
|
|
42
90
|
});
|
|
43
|
-
this.scheduler.on(EVENT_INSTANCE_ERROR, (result)
|
|
44
|
-
|
|
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
|
-
|
|
82
|
-
nodes.
|
|
83
|
-
|
|
84
|
-
|
|
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
|
-
|
|
92
|
-
if (node.type ===
|
|
93
|
-
|
|
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(
|
|
151
|
+
console.warn("\u672A\u8BC6\u522B\u7684\u8282\u70B9\u7C7B\u578B: " + node.type);
|
|
98
152
|
}
|
|
99
153
|
});
|
|
100
|
-
edges.forEach((edge)
|
|
101
|
-
|
|
102
|
-
|
|
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 !==
|
|
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
|
-
* 会依次判断是否有
|
|
122
|
-
* 若存在
|
|
123
|
-
* 若存在
|
|
124
|
-
* 若都不存在,那么新建一个
|
|
125
|
-
* @private
|
|
235
|
+
* 会依次判断是否有taskId、nodeId、executionId。
|
|
236
|
+
* 若存在taskId,那么表示恢复执行。
|
|
237
|
+
* 若存在nodeId,那么表示从指定节点开始执行。
|
|
238
|
+
* 若都不存在,那么新建一个executionId,从开始节点开始执行。
|
|
126
239
|
*/
|
|
127
|
-
createExecution(
|
|
128
|
-
var
|
|
129
|
-
this.executeList.push(
|
|
130
|
-
// 如果有
|
|
131
|
-
|
|
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:
|
|
135
|
-
actionId:
|
|
136
|
-
nodeId:
|
|
137
|
-
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
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
11
|
+
export default class Scheduler extends EventEmitter {
|
|
10
12
|
/**
|
|
11
13
|
* 当前需要执行的节点队列
|
|
12
14
|
*/
|
|
13
|
-
nodeQueueMap: Map<
|
|
15
|
+
nodeQueueMap: Map<ExecutionId, NodeParam[]>;
|
|
14
16
|
/**
|
|
15
17
|
* 当前正在执行的节点集合
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
+
* 在每个节点执行完成后,会从集合中删除。
|
|
19
|
+
* 同时会判断此集合中是否还存在和此节点相同的executionId,如果不存在,说明此流程已经执行完成。
|
|
18
20
|
*/
|
|
19
|
-
actionRunningMap: Map<
|
|
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
|
|
29
|
-
constructor(config:
|
|
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:
|
|
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(
|
|
53
|
-
|
|
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:
|
|
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
|
|
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
|
|
62
|
+
export {};
|