@logicflow/engine 0.0.1

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 (67) hide show
  1. package/README.md +22 -0
  2. package/cjs/EventEmitter.js +70 -0
  3. package/cjs/FlowModel.js +252 -0
  4. package/cjs/Scheduler.js +278 -0
  5. package/cjs/constant/LogCode.js +31 -0
  6. package/cjs/constant/constant.js +23 -0
  7. package/cjs/expression/browserVm.js +69 -0
  8. package/cjs/expression/index.js +63 -0
  9. package/cjs/expression/nodeVm.js +53 -0
  10. package/cjs/index.js +161 -0
  11. package/cjs/nodes/BaseNode.js +243 -0
  12. package/cjs/nodes/StartNode.js +27 -0
  13. package/cjs/nodes/TaskNode.js +27 -0
  14. package/cjs/recorder/index.js +112 -0
  15. package/cjs/util/ID.js +12 -0
  16. package/cjs/util/global.js +32 -0
  17. package/cjs/util/storage.js +41 -0
  18. package/es/EventEmitter.d.ts +7 -0
  19. package/es/EventEmitter.js +68 -0
  20. package/es/FlowModel.d.ts +104 -0
  21. package/es/FlowModel.js +250 -0
  22. package/es/Scheduler.d.ts +51 -0
  23. package/es/Scheduler.js +276 -0
  24. package/es/constant/LogCode.d.ts +12 -0
  25. package/es/constant/LogCode.js +28 -0
  26. package/es/constant/constant.d.ts +14 -0
  27. package/es/constant/constant.js +20 -0
  28. package/es/expression/browserVm.d.ts +2 -0
  29. package/es/expression/browserVm.js +66 -0
  30. package/es/expression/index.d.ts +2 -0
  31. package/es/expression/index.js +60 -0
  32. package/es/expression/nodeVm.d.ts +2 -0
  33. package/es/expression/nodeVm.js +50 -0
  34. package/es/index.d.ts +47 -0
  35. package/es/index.js +156 -0
  36. package/es/nodes/BaseNode.d.ts +109 -0
  37. package/es/nodes/BaseNode.js +241 -0
  38. package/es/nodes/StartNode.d.ts +5 -0
  39. package/es/nodes/StartNode.js +25 -0
  40. package/es/nodes/TaskNode.d.ts +5 -0
  41. package/es/nodes/TaskNode.js +25 -0
  42. package/es/recorder/index.d.ts +9 -0
  43. package/es/recorder/index.js +110 -0
  44. package/es/util/ID.d.ts +2 -0
  45. package/es/util/ID.js +9 -0
  46. package/es/util/global.d.ts +5 -0
  47. package/es/util/global.js +26 -0
  48. package/es/util/storage.d.ts +6 -0
  49. package/es/util/storage.js +39 -0
  50. package/lib/main.js +1 -0
  51. package/package.json +87 -0
  52. package/types/EventEmitter.d.ts +7 -0
  53. package/types/FlowModel.d.ts +104 -0
  54. package/types/Scheduler.d.ts +51 -0
  55. package/types/constant/LogCode.d.ts +12 -0
  56. package/types/constant/constant.d.ts +14 -0
  57. package/types/expression/browserVm.d.ts +2 -0
  58. package/types/expression/index.d.ts +2 -0
  59. package/types/expression/nodeVm.d.ts +2 -0
  60. package/types/index.d.ts +47 -0
  61. package/types/nodes/BaseNode.d.ts +109 -0
  62. package/types/nodes/StartNode.d.ts +5 -0
  63. package/types/nodes/TaskNode.d.ts +5 -0
  64. package/types/recorder/index.d.ts +9 -0
  65. package/types/util/ID.d.ts +2 -0
  66. package/types/util/global.d.ts +5 -0
  67. package/types/util/storage.d.ts +6 -0
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ 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;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.runInBrowserContext = void 0;
40
+ var LogCode_1 = require("../constant/LogCode");
41
+ var runInBrowserContext = function (code, globalData) {
42
+ if (globalData === void 0) { globalData = {}; }
43
+ return __awaiter(void 0, void 0, void 0, function () {
44
+ var iframe, iframeWindow, iframeEval, res;
45
+ return __generator(this, function (_a) {
46
+ iframe = document.createElement('iframe');
47
+ iframe.style.display = 'none';
48
+ if (!document || !document.body) {
49
+ console.error(LogCode_1.getErrorMsg(LogCode_1.ErrorCode.NO_DOCUMENT_BODY));
50
+ }
51
+ document.body.appendChild(iframe);
52
+ iframeWindow = iframe.contentWindow;
53
+ iframeEval = iframeWindow.eval;
54
+ Object.keys(globalData).forEach(function (key) {
55
+ iframeWindow[key] = globalData[key];
56
+ });
57
+ res = null;
58
+ try {
59
+ res = iframeEval.call(iframeWindow, code);
60
+ }
61
+ catch (e) {
62
+ console.warn(LogCode_1.getWarningMsg(LogCode_1.WarningCode.EXPRESSION_EXEC_ERROR), { code: code, globalData: globalData, e: e });
63
+ }
64
+ document.body.removeChild(iframe);
65
+ return [2 /*return*/, res];
66
+ });
67
+ });
68
+ };
69
+ exports.runInBrowserContext = runInBrowserContext;
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ 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;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.getExpressionResult = void 0;
40
+ var nodeVm_1 = require("./nodeVm");
41
+ var browserVm_1 = require("./browserVm");
42
+ var global_1 = require("../util/global");
43
+ var getExpressionResult = function (code, context) { return __awaiter(void 0, void 0, void 0, function () {
44
+ var r, r;
45
+ return __generator(this, function (_a) {
46
+ switch (_a.label) {
47
+ case 0:
48
+ if (!global_1.isInNodeJS) return [3 /*break*/, 2];
49
+ return [4 /*yield*/, nodeVm_1.runInNewContext(code, context)];
50
+ case 1:
51
+ r = _a.sent();
52
+ return [2 /*return*/, r];
53
+ case 2:
54
+ if (!global_1.isInBrowser) return [3 /*break*/, 4];
55
+ return [4 /*yield*/, browserVm_1.runInBrowserContext(code, context)];
56
+ case 3:
57
+ r = _a.sent();
58
+ return [2 /*return*/, r];
59
+ case 4: return [2 /*return*/, global_1.globalScope.eval(code)]; // eslint-disable-line no-eval
60
+ }
61
+ });
62
+ }); };
63
+ exports.getExpressionResult = getExpressionResult;
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ 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;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.runInNewContext = void 0;
40
+ // import vm from 'node:vm';
41
+ var vm = require('vm');
42
+ var runInNewContext = function (code, globalData) {
43
+ if (globalData === void 0) { globalData = {}; }
44
+ return __awaiter(void 0, void 0, void 0, function () {
45
+ var context;
46
+ return __generator(this, function (_a) {
47
+ context = vm.createContext(globalData);
48
+ vm.runInContext(code, context);
49
+ return [2 /*return*/, context];
50
+ });
51
+ });
52
+ };
53
+ exports.runInNewContext = runInNewContext;
package/cjs/index.js ADDED
@@ -0,0 +1,161 @@
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
+ exports.StartNode = exports.TaskNode = exports.Engine = void 0;
51
+ var FlowModel_1 = require("./FlowModel");
52
+ var StartNode_1 = require("./nodes/StartNode");
53
+ exports.StartNode = StartNode_1.default;
54
+ var TaskNode_1 = require("./nodes/TaskNode");
55
+ exports.TaskNode = TaskNode_1.default;
56
+ var recorder_1 = require("./recorder");
57
+ var Engine = /** @class */ (function () {
58
+ function Engine() {
59
+ this.nodeModelMap = new Map();
60
+ this.recorder = new recorder_1.default();
61
+ // register node
62
+ this.register({
63
+ type: StartNode_1.default.nodeTypeName,
64
+ model: StartNode_1.default,
65
+ });
66
+ this.register({
67
+ type: TaskNode_1.default.nodeTypeName,
68
+ model: TaskNode_1.default,
69
+ });
70
+ }
71
+ /**
72
+ * 注册节点
73
+ * @param nodeConfig { type: 'custom-node', model: Class }
74
+ */
75
+ Engine.prototype.register = function (nodeConfig) {
76
+ this.nodeModelMap.set(nodeConfig.type, nodeConfig.model);
77
+ };
78
+ /**
79
+ * 自定义执行记录的存储,默认浏览器使用 sessionStorage,nodejs 使用内存存储。
80
+ * 注意:由于执行记录不会主动删除,所以需要自行清理。
81
+ * nodejs环境建议自定义为持久化存储。
82
+ * engine.setCustomRecorder({
83
+ * async addTask(task) {}
84
+ * async getTask(taskId) {}
85
+ * async getExecutionTasks(executionId) {}
86
+ * clear() {}
87
+ * });
88
+ */
89
+ Engine.prototype.setCustomRecorder = function (recorder) {
90
+ this.recorder = recorder;
91
+ };
92
+ /**
93
+ * 加载流程图数据
94
+ */
95
+ Engine.prototype.load = function (_a) {
96
+ var graphData = _a.graphData, _b = _a.startNodeType, startNodeType = _b === void 0 ? 'StartNode' : _b, _c = _a.globalData, globalData = _c === void 0 ? {} : _c, _d = _a.context, context = _d === void 0 ? {} : _d;
97
+ this.flowModel = new FlowModel_1.default({
98
+ nodeModelMap: this.nodeModelMap,
99
+ recorder: this.recorder,
100
+ context: context,
101
+ globalData: globalData,
102
+ startNodeType: startNodeType,
103
+ });
104
+ this.flowModel.load(graphData);
105
+ return this.flowModel;
106
+ };
107
+ /**
108
+ * 执行流程,允许多次调用。
109
+ */
110
+ Engine.prototype.execute = function (execParam) {
111
+ return __awaiter(this, void 0, void 0, function () {
112
+ var _this = this;
113
+ return __generator(this, function (_a) {
114
+ return [2 /*return*/, new Promise(function (resolve, reject) {
115
+ if (!execParam) {
116
+ execParam = {};
117
+ }
118
+ _this.flowModel.execute(__assign(__assign({}, execParam), { callback: function (result) {
119
+ resolve(result);
120
+ }, onError: function (error) {
121
+ reject(error);
122
+ } }));
123
+ })];
124
+ });
125
+ });
126
+ };
127
+ Engine.prototype.resume = function (resumeParam) {
128
+ return __awaiter(this, void 0, void 0, function () {
129
+ var _this = this;
130
+ return __generator(this, function (_a) {
131
+ return [2 /*return*/, new Promise(function (resolve, reject) {
132
+ _this.flowModel.resume(__assign(__assign({}, resumeParam), { callback: function (result) {
133
+ resolve(result);
134
+ }, onError: function (error) {
135
+ reject(error);
136
+ } }));
137
+ })];
138
+ });
139
+ });
140
+ };
141
+ Engine.prototype.getExecutionRecord = function (executionId) {
142
+ return __awaiter(this, void 0, void 0, function () {
143
+ var tasks, records, i;
144
+ return __generator(this, function (_a) {
145
+ switch (_a.label) {
146
+ case 0: return [4 /*yield*/, this.recorder.getExecutionTasks(executionId)];
147
+ case 1:
148
+ tasks = _a.sent();
149
+ records = [];
150
+ for (i = 0; i < tasks.length; i++) {
151
+ records.push(this.recorder.getTask(tasks[i]));
152
+ }
153
+ return [2 /*return*/, Promise.all(records)];
154
+ }
155
+ });
156
+ });
157
+ };
158
+ return Engine;
159
+ }());
160
+ exports.Engine = Engine;
161
+ exports.default = Engine;
@@ -0,0 +1,243 @@
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
+ var __values = (this && this.__values) || function(o) {
50
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
51
+ if (m) return m.call(o);
52
+ if (o && typeof o.length === "number") return {
53
+ next: function () {
54
+ if (o && i >= o.length) o = void 0;
55
+ return { value: o && o[i++], done: !o };
56
+ }
57
+ };
58
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
59
+ };
60
+ Object.defineProperty(exports, "__esModule", { value: true });
61
+ var constant_1 = require("../constant/constant");
62
+ var expression_1 = require("../expression");
63
+ var BaseNode = /** @class */ (function () {
64
+ function BaseNode(_a) {
65
+ var nodeConfig = _a.nodeConfig, context = _a.context, globalData = _a.globalData;
66
+ this.outgoing = nodeConfig.outgoing;
67
+ this.incoming = nodeConfig.incoming;
68
+ this.nodeId = nodeConfig.id;
69
+ this.type = nodeConfig.type;
70
+ this.properties = nodeConfig.properties;
71
+ this.context = context;
72
+ this.globalData = globalData;
73
+ this.baseType = 'base';
74
+ }
75
+ /**
76
+ * 节点的每一次执行都会生成一个唯一的taskId
77
+ */
78
+ BaseNode.prototype.execute = function (params) {
79
+ return __awaiter(this, void 0, void 0, function () {
80
+ var r, outgoing;
81
+ return __generator(this, function (_a) {
82
+ switch (_a.label) {
83
+ case 0: return [4 /*yield*/, this.action({
84
+ executionId: params.executionId,
85
+ taskId: params.taskId,
86
+ nodeId: this.nodeId,
87
+ })];
88
+ case 1:
89
+ r = _a.sent();
90
+ if (!(!r || r.status === constant_1.TaskStatus.SUCCESS)) return [3 /*break*/, 3];
91
+ return [4 /*yield*/, this.getOutgoing()];
92
+ case 2:
93
+ outgoing = _a.sent();
94
+ params.next({
95
+ executionId: params.executionId,
96
+ taskId: params.taskId,
97
+ nodeId: this.nodeId,
98
+ nodeType: this.type,
99
+ properties: this.properties,
100
+ outgoing: outgoing,
101
+ });
102
+ _a.label = 3;
103
+ case 3: return [2 /*return*/, {
104
+ status: r && r.status,
105
+ detail: r && r.detail,
106
+ executionId: params.executionId,
107
+ taskId: params.taskId,
108
+ nodeId: this.nodeId,
109
+ nodeType: this.type,
110
+ properties: this.properties,
111
+ }];
112
+ }
113
+ });
114
+ });
115
+ };
116
+ /**
117
+ * 节点在执行中断后,可以通过resume方法恢复执行。
118
+ * 自定义节点时不建议重写此方法
119
+ */
120
+ BaseNode.prototype.resume = function (params) {
121
+ return __awaiter(this, void 0, void 0, function () {
122
+ var outgoing;
123
+ return __generator(this, function (_a) {
124
+ switch (_a.label) {
125
+ case 0: return [4 /*yield*/, this.getOutgoing()];
126
+ case 1:
127
+ outgoing = _a.sent();
128
+ return [4 /*yield*/, this.onResume({
129
+ executionId: params.executionId,
130
+ nodeId: params.nodeId,
131
+ taskId: params.taskId,
132
+ data: params.data,
133
+ })];
134
+ case 2:
135
+ _a.sent();
136
+ params.next({
137
+ executionId: params.executionId,
138
+ taskId: params.taskId,
139
+ nodeId: this.nodeId,
140
+ nodeType: this.type,
141
+ properties: this.properties,
142
+ outgoing: outgoing,
143
+ });
144
+ return [2 /*return*/, undefined];
145
+ }
146
+ });
147
+ });
148
+ };
149
+ BaseNode.prototype.getOutgoing = function () {
150
+ return __awaiter(this, void 0, void 0, function () {
151
+ var outgoing, expressions, _a, _b, item, properties, result;
152
+ var e_1, _c;
153
+ var _this = this;
154
+ return __generator(this, function (_d) {
155
+ switch (_d.label) {
156
+ case 0:
157
+ outgoing = [];
158
+ expressions = [];
159
+ try {
160
+ for (_a = __values(this.outgoing), _b = _a.next(); !_b.done; _b = _a.next()) {
161
+ item = _b.value;
162
+ properties = item.properties;
163
+ expressions.push(this.isPass(properties));
164
+ }
165
+ }
166
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
167
+ finally {
168
+ try {
169
+ if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
170
+ }
171
+ finally { if (e_1) throw e_1.error; }
172
+ }
173
+ return [4 /*yield*/, Promise.all(expressions)];
174
+ case 1:
175
+ result = _d.sent();
176
+ result.forEach(function (item, index) {
177
+ if (item) {
178
+ outgoing.push(_this.outgoing[index]);
179
+ }
180
+ });
181
+ return [2 /*return*/, outgoing];
182
+ }
183
+ });
184
+ });
185
+ };
186
+ BaseNode.prototype.isPass = function (properties) {
187
+ return __awaiter(this, void 0, void 0, function () {
188
+ var conditionExpression, result, e_2;
189
+ return __generator(this, function (_a) {
190
+ switch (_a.label) {
191
+ case 0:
192
+ if (!properties)
193
+ return [2 /*return*/, true];
194
+ conditionExpression = properties.conditionExpression;
195
+ if (!conditionExpression)
196
+ return [2 /*return*/, true];
197
+ _a.label = 1;
198
+ case 1:
199
+ _a.trys.push([1, 3, , 4]);
200
+ return [4 /*yield*/, expression_1.getExpressionResult("result" + this.nodeId + " = (" + conditionExpression + ")", __assign({}, this.globalData))];
201
+ case 2:
202
+ result = _a.sent();
203
+ return [2 /*return*/, result["result" + this.nodeId]];
204
+ case 3:
205
+ e_2 = _a.sent();
206
+ return [2 /*return*/, false];
207
+ case 4: return [2 /*return*/];
208
+ }
209
+ });
210
+ });
211
+ };
212
+ /**
213
+ * 节点的执行逻辑
214
+ * @overridable 可以自定义节点重写此方法。
215
+ * @param params.executionId 流程执行记录ID
216
+ * @param params.taskId 此节点执行记录ID
217
+ * @param params.nodeId 节点ID
218
+ */
219
+ BaseNode.prototype.action = function (params) {
220
+ return __awaiter(this, void 0, void 0, function () {
221
+ return __generator(this, function (_a) {
222
+ return [2 /*return*/, undefined];
223
+ });
224
+ });
225
+ };
226
+ /**
227
+ * 节点的重新恢复执行逻辑
228
+ * @overridable 可以自定义节点重写此方法。
229
+ * @param params.executionId 流程执行记录ID
230
+ * @param params.taskId 此节点执行记录ID
231
+ * @param params.nodeId 节点ID
232
+ */
233
+ BaseNode.prototype.onResume = function (params) {
234
+ return __awaiter(this, void 0, void 0, function () {
235
+ return __generator(this, function (_a) {
236
+ return [2 /*return*/, undefined];
237
+ });
238
+ });
239
+ };
240
+ BaseNode.nodeTypeName = 'BaseNode';
241
+ return BaseNode;
242
+ }());
243
+ exports.default = BaseNode;
@@ -0,0 +1,27 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ var BaseNode_1 = require("./BaseNode");
17
+ var StartNode = /** @class */ (function (_super) {
18
+ __extends(StartNode, _super);
19
+ function StartNode() {
20
+ var _this = _super !== null && _super.apply(this, arguments) || this;
21
+ _this.baseType = 'start';
22
+ return _this;
23
+ }
24
+ StartNode.nodeTypeName = 'StartNode';
25
+ return StartNode;
26
+ }(BaseNode_1.default));
27
+ exports.default = StartNode;
@@ -0,0 +1,27 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ var BaseNode_1 = require("./BaseNode");
17
+ var TaskNode = /** @class */ (function (_super) {
18
+ __extends(TaskNode, _super);
19
+ function TaskNode() {
20
+ var _this = _super !== null && _super.apply(this, arguments) || this;
21
+ _this.baseType = 'task';
22
+ return _this;
23
+ }
24
+ TaskNode.nodeTypeName = 'TaskNode';
25
+ return TaskNode;
26
+ }(BaseNode_1.default));
27
+ exports.default = TaskNode;