@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/Scheduler.js CHANGED
@@ -1,66 +1,158 @@
1
- import { __awaiter } from "tslib";
1
+ var __extends = (this && this.__extends) || (function () {
2
+ var extendStatics = function (d, b) {
3
+ extendStatics = Object.setPrototypeOf ||
4
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5
+ function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
6
+ return extendStatics(d, b);
7
+ };
8
+ return function (d, b) {
9
+ extendStatics(d, b);
10
+ function __() { this.constructor = d; }
11
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
12
+ };
13
+ })();
14
+ var __assign = (this && this.__assign) || function () {
15
+ __assign = Object.assign || function(t) {
16
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
17
+ s = arguments[i];
18
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
19
+ t[p] = s[p];
20
+ }
21
+ return t;
22
+ };
23
+ return __assign.apply(this, arguments);
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ var __generator = (this && this.__generator) || function (thisArg, body) {
35
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
36
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
37
+ function verb(n) { return function (v) { return step([n, v]); }; }
38
+ function step(op) {
39
+ if (f) throw new TypeError("Generator is already executing.");
40
+ while (_) try {
41
+ 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;
42
+ if (y = 0, t) op = [op[0] & 2, t.value];
43
+ switch (op[0]) {
44
+ case 0: case 1: t = op; break;
45
+ case 4: _.label++; return { value: op[1], done: false };
46
+ case 5: _.label++; y = op[1]; op = [0]; continue;
47
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
48
+ default:
49
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
50
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
51
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
52
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
53
+ if (t[2]) _.ops.pop();
54
+ _.trys.pop(); continue;
55
+ }
56
+ op = body.call(thisArg, _);
57
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
58
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
59
+ }
60
+ };
2
61
  import EventEmitter from './EventEmitter';
3
- import { createActionId } from './utils';
4
- import { EVENT_INSTANCE_ERROR, EVENT_INSTANCE_COMPLETE, EVENT_INSTANCE_INTERRUPTED, FlowStatus, } from './constant';
62
+ import { EVENT_INSTANCE_COMPLETE, EVENT_INSTANCE_INTERRUPTED, EVENT_INSTANCE_ERROR, FlowStatus, } from './constant/constant';
63
+ import { createActionId } from './util/ID';
5
64
  /**
6
65
  * 调度器
7
66
  * 通过一个队列维护需要执行的节点,一个集合维护正在执行的节点
8
67
  */
9
- export class Scheduler extends EventEmitter {
10
- constructor(config) {
11
- super();
12
- this.nodeQueueMap = new Map();
13
- this.actionRunningMap = new Map();
14
- this.flowModel = config.flowModel;
15
- this.recorder = config.recorder;
68
+ var Scheduler = /** @class */ (function (_super) {
69
+ __extends(Scheduler, _super);
70
+ function Scheduler(config) {
71
+ var _this = _super.call(this) || this;
72
+ _this.nodeQueueMap = new Map();
73
+ _this.actionRunningMap = new Map();
74
+ _this.flowModel = config.flowModel;
75
+ _this.recorder = config.recorder;
76
+ return _this;
16
77
  }
17
78
  /**
18
79
  * 添加一个任务到队列中。
19
- * 1. 由流程模型将所有的开始及诶带你添加到队列中
20
- * 2. 当一个节点执行完成后,将后续的节点添加到队列中
21
- * @param nodeParam
80
+ * 1. 由流程模型将所有的开始节点添加到队列中。
81
+ * 2. 当一个节点执行完成后,将后续的节点添加到队列中。
22
82
  */
23
- addAction(nodeParam) {
24
- const { executionId } = nodeParam;
83
+ Scheduler.prototype.addAction = function (nodeParam) {
84
+ var executionId = nodeParam.executionId;
25
85
  if (!this.nodeQueueMap.has(executionId)) {
26
86
  this.nodeQueueMap.set(executionId, []);
27
87
  }
28
- const currentActionQueue = this.nodeQueueMap.get(executionId);
29
- if (currentActionQueue) {
30
- currentActionQueue.push(nodeParam);
88
+ var currentActionQueue = this.nodeQueueMap.get(executionId);
89
+ currentActionQueue.push(nodeParam);
90
+ };
91
+ /**
92
+ * 调度器执行下一个任务
93
+ * 1. 提供给流程模型,用户开始执行第一个任务。
94
+ * 2. 内部任务执行完成后,调用此方法继续执行下一个任务。
95
+ * 3. 当判断没有可以继续执行的任务后,触发流程结束事件。
96
+ */
97
+ Scheduler.prototype.run = function (runParams) {
98
+ var nodeQueue = this.nodeQueueMap.get(runParams.executionId);
99
+ // 将同一个executionId当前待执行的节点一起执行
100
+ // 避免出现某一个节点执行时间过长,导致其他节点等待时间过长。
101
+ while (nodeQueue.length) {
102
+ var currentNode = nodeQueue.pop();
103
+ var actionId = createActionId();
104
+ var actionParam = __assign(__assign({}, currentNode), { actionId: actionId });
105
+ this.pushActionToRunningMap(actionParam);
106
+ this.exec(actionParam);
31
107
  }
32
- console.log('this.nodeQueueMap--->>>', this.nodeQueueMap);
33
- }
34
- pushActionToRunningMap(actionParam) {
35
- var _a;
36
- const { executionId, actionId } = actionParam;
108
+ if (!this.hasRunningAction(runParams.executionId)) {
109
+ // 当一个流程在nodeQueueMap和actionRunningMap中都不存在执行的节点时,说明这个流程已经执行完成。
110
+ this.emit(EVENT_INSTANCE_COMPLETE, __assign(__assign({}, runParams), { status: FlowStatus.COMPLETED }));
111
+ }
112
+ };
113
+ /**
114
+ * 恢复某个任务的执行。
115
+ * 可以自定义节点手动实现流程中断,然后通过此方法恢复流程的执行。
116
+ */
117
+ Scheduler.prototype.resume = function (resumeParam) {
118
+ return __awaiter(this, void 0, void 0, function () {
119
+ var model;
120
+ return __generator(this, function (_a) {
121
+ switch (_a.label) {
122
+ case 0:
123
+ this.pushActionToRunningMap({
124
+ executionId: resumeParam.executionId,
125
+ nodeId: resumeParam.nodeId,
126
+ actionId: resumeParam.actionId,
127
+ });
128
+ model = this.flowModel.createAction(resumeParam.nodeId);
129
+ return [4 /*yield*/, model.resume(__assign(__assign({}, resumeParam), { next: this.next.bind(this) }))];
130
+ case 1:
131
+ _a.sent();
132
+ return [2 /*return*/];
133
+ }
134
+ });
135
+ });
136
+ };
137
+ Scheduler.prototype.pushActionToRunningMap = function (actionParam) {
138
+ var executionId = actionParam.executionId, actionId = actionParam.actionId;
37
139
  if (!this.actionRunningMap.has(executionId)) {
38
- const runningMap = new Map();
140
+ var runningMap = new Map();
39
141
  this.actionRunningMap.set(executionId, runningMap);
40
142
  }
41
- if (actionId) {
42
- (_a = this.actionRunningMap.get(executionId)) === null || _a === void 0 ? void 0 : _a.set(actionId, actionParam);
43
- }
44
- }
45
- removeActionFromRunningMap(actionParam) {
46
- const { executionId, actionId } = actionParam;
143
+ this.actionRunningMap.get(executionId).set(actionId, actionParam);
144
+ };
145
+ Scheduler.prototype.removeActionFromRunningMap = function (actionParam) {
146
+ var executionId = actionParam.executionId, actionId = actionParam.actionId;
47
147
  if (!actionId)
48
148
  return;
49
- const runningMap = this.actionRunningMap.get(executionId);
149
+ var runningMap = this.actionRunningMap.get(executionId);
50
150
  if (!runningMap)
51
151
  return;
52
152
  runningMap.delete(actionId);
53
- }
54
- /**
55
- * 为了防止多次添加导致
56
- * @param actionParam
57
- */
58
- saveActionResult(actionParam) {
59
- var _a;
60
- (_a = this.recorder) === null || _a === void 0 ? void 0 : _a.addActionRecord(Object.assign({ timestamp: Date.now() }, actionParam));
61
- }
62
- hasRunningAction(executionId) {
63
- const runningMap = this.actionRunningMap.get(executionId);
153
+ };
154
+ Scheduler.prototype.hasRunningAction = function (executionId) {
155
+ var runningMap = this.actionRunningMap.get(executionId);
64
156
  if (!runningMap)
65
157
  return false;
66
158
  if (runningMap.size === 0) {
@@ -68,35 +160,67 @@ export class Scheduler extends EventEmitter {
68
160
  return false;
69
161
  }
70
162
  return true;
71
- }
72
- /**
73
- * 调度器执行下一个任务
74
- * 1. 提供给流程模型,用户开始执行第一个任务
75
- * 2. 内部任务执行完成后,调用此方法继续执行下一个任务
76
- * 3. 当判断没有可以继续执行的任务后,触发流程结束事件
77
- * @param runParam
78
- */
79
- run(runParam) {
80
- const nodeQueue = this.nodeQueueMap.get(runParam.executionId);
81
- // 将同一个 executionId 当前待执行的节点一起执行
82
- // 避免出现某一个节点执行时间过长,导致其他节点等待时间过长
83
- while (nodeQueue === null || nodeQueue === void 0 ? void 0 : nodeQueue.length) {
84
- const currentNode = nodeQueue.pop();
85
- const actionId = createActionId();
86
- const actionParam = Object.assign(Object.assign({}, currentNode), { actionId });
87
- this.pushActionToRunningMap(actionParam);
88
- this.exec(actionParam);
89
- }
90
- if (!this.hasRunningAction(runParam.executionId)) {
91
- // 当一个流程在 nodeQueueMap 和 actionRunningMap 中都不存在执行的节点时,说明这个流程已经执行完成。
92
- this.emit(EVENT_INSTANCE_COMPLETE, Object.assign(Object.assign({}, runParam), { status: FlowStatus.COMPLETED }));
93
- }
94
- }
95
- next(data) {
163
+ };
164
+ Scheduler.prototype.exec = function (actionParam) {
165
+ return __awaiter(this, void 0, void 0, function () {
166
+ var model, execResult;
167
+ return __generator(this, function (_a) {
168
+ switch (_a.label) {
169
+ case 0:
170
+ model = this.flowModel.createAction(actionParam.nodeId);
171
+ return [4 /*yield*/, model.execute({
172
+ executionId: actionParam.executionId,
173
+ actionId: actionParam.actionId,
174
+ nodeId: actionParam.nodeId,
175
+ next: this.next.bind(this),
176
+ })];
177
+ case 1:
178
+ execResult = _a.sent();
179
+ if (execResult && execResult.status === FlowStatus.INTERRUPTED) {
180
+ this.interrupted(execResult);
181
+ this.saveActionResult({
182
+ executionId: actionParam.executionId,
183
+ nodeId: actionParam.nodeId,
184
+ actionId: actionParam.actionId,
185
+ nodeType: execResult.nodeType,
186
+ properties: execResult.properties,
187
+ outgoing: execResult.outgoing,
188
+ status: execResult.status,
189
+ detail: execResult.detail,
190
+ });
191
+ this.removeActionFromRunningMap(actionParam);
192
+ }
193
+ if (execResult && execResult.status === FlowStatus.ERROR) {
194
+ this.error(execResult);
195
+ this.saveActionResult({
196
+ executionId: actionParam.executionId,
197
+ nodeId: actionParam.nodeId,
198
+ actionId: actionParam.actionId,
199
+ nodeType: execResult.nodeType,
200
+ properties: execResult.properties,
201
+ outgoing: execResult.outgoing,
202
+ status: execResult.status,
203
+ detail: execResult.detail,
204
+ });
205
+ this.removeActionFromRunningMap(actionParam);
206
+ }
207
+ return [2 /*return*/];
208
+ }
209
+ });
210
+ });
211
+ };
212
+ Scheduler.prototype.interrupted = function (execResult) {
213
+ this.emit(EVENT_INSTANCE_INTERRUPTED, execResult);
214
+ };
215
+ Scheduler.prototype.error = function (execResult) {
216
+ this.emit(EVENT_INSTANCE_ERROR, execResult);
217
+ };
218
+ Scheduler.prototype.next = function (data) {
219
+ var _this = this;
96
220
  if (data.outgoing && data.outgoing.length > 0) {
97
- data.outgoing.forEach((item) => {
221
+ data.outgoing.forEach(function (item) {
98
222
  if (item.result) {
99
- this.addAction({
223
+ _this.addAction({
100
224
  executionId: data.executionId,
101
225
  nodeId: item.target,
102
226
  });
@@ -106,70 +230,21 @@ export class Scheduler extends EventEmitter {
106
230
  this.saveActionResult(data);
107
231
  this.removeActionFromRunningMap(data);
108
232
  this.run(data);
109
- }
110
- /**
111
- * 恢复某个任务的执行
112
- * 可以自定义节点手动实现流程中断,然后通过此方法恢复流程的执行
113
- * @param resumeParam
114
- */
115
- resume(resumeParam) {
116
- return __awaiter(this, void 0, void 0, function* () {
117
- const { executionId, actionId, nodeId } = resumeParam;
118
- this.pushActionToRunningMap({
119
- executionId,
120
- actionId,
121
- nodeId,
122
- });
123
- const model = this.flowModel.createAction(nodeId);
124
- yield (model === null || model === void 0 ? void 0 : model.resume(Object.assign(Object.assign({}, resumeParam), { next: this.next.bind(this) })));
125
- });
126
- }
127
- // 中断时,触发事件
128
- interrupted(execResult) {
129
- this.emit(EVENT_INSTANCE_INTERRUPTED, execResult);
130
- }
131
- // 报错时,触发事件
132
- error(execResult) {
133
- this.emit(EVENT_INSTANCE_ERROR, execResult);
134
- }
135
- exec(actionParam) {
136
- return __awaiter(this, void 0, void 0, function* () {
137
- const { executionId, actionId, nodeId } = actionParam;
138
- const model = this.flowModel.createAction(nodeId);
139
- const execResult = yield (model === null || model === void 0 ? void 0 : model.execute({
140
- executionId,
141
- actionId,
142
- nodeId,
143
- next: this.next.bind(this),
144
- }));
145
- if (execResult) {
146
- const { nodeType, properties, outgoing, status, detail } = execResult;
147
- const actionResult = {
148
- // actionParam
149
- executionId,
150
- actionId,
151
- nodeId,
152
- // execResult
153
- nodeType,
154
- properties,
155
- outgoing,
156
- status,
157
- detail,
158
- };
159
- if ((execResult === null || execResult === void 0 ? void 0 : execResult.status) === FlowStatus.INTERRUPTED) {
160
- this.interrupted(execResult);
161
- this.saveActionResult(actionResult);
162
- this.removeActionFromRunningMap(actionParam);
163
- }
164
- if ((execResult === null || execResult === void 0 ? void 0 : execResult.status) === FlowStatus.ERROR) {
165
- this.error(execResult);
166
- this.saveActionResult(actionResult);
167
- this.removeActionFromRunningMap(actionParam);
168
- }
169
- }
170
- // TODO: 考虑停下所有的任务
233
+ };
234
+ Scheduler.prototype.saveActionResult = function (data) {
235
+ var _a;
236
+ (_a = this.recorder) === null || _a === void 0 ? void 0 : _a.addActionRecord({
237
+ executionId: data.executionId,
238
+ actionId: data.actionId,
239
+ nodeId: data.nodeId,
240
+ nodeType: data.nodeType,
241
+ timestamp: Date.now(),
242
+ properties: data.properties,
243
+ outgoing: data.outgoing,
244
+ detail: data.detail,
245
+ status: data.status,
171
246
  });
172
- }
173
- }
247
+ };
248
+ return Scheduler;
249
+ }(EventEmitter));
174
250
  export default Scheduler;
175
- //# sourceMappingURL=Scheduler.js.map
@@ -0,0 +1,28 @@
1
+ var _a, _b;
2
+ export var ErrorCode;
3
+ (function (ErrorCode) {
4
+ // 模型数据错误
5
+ ErrorCode[ErrorCode["NONE_START_NODE"] = 1000] = "NONE_START_NODE";
6
+ ErrorCode[ErrorCode["NONE_NODE_ID"] = 1001] = "NONE_NODE_ID";
7
+ // 表达式错误
8
+ ErrorCode[ErrorCode["NO_DOCUMENT_BODY"] = 2001] = "NO_DOCUMENT_BODY";
9
+ })(ErrorCode || (ErrorCode = {}));
10
+ export var WarningCode;
11
+ (function (WarningCode) {
12
+ WarningCode[WarningCode["NONE_START_NODE_IN_DATA"] = 2000] = "NONE_START_NODE_IN_DATA";
13
+ WarningCode[WarningCode["START_NODE_INCOMING"] = 2001] = "START_NODE_INCOMING";
14
+ // 表达式判断异常
15
+ WarningCode[WarningCode["EXPRESSION_EXEC_ERROR"] = 3000] = "EXPRESSION_EXEC_ERROR";
16
+ })(WarningCode || (WarningCode = {}));
17
+ var errorMsgMapCn = (_a = {},
18
+ _a[ErrorCode.NONE_START_NODE] = '未找到入度为0的节点',
19
+ _a[ErrorCode.NONE_NODE_ID] = '流程数据中存在没有此节点',
20
+ _a[ErrorCode.NO_DOCUMENT_BODY] = '找不到document.body, 请在DOM加载完成后再执行',
21
+ _a);
22
+ var warningMsgMapCn = (_b = {},
23
+ _b[WarningCode.NONE_START_NODE_IN_DATA] = '初始化数据中未找到入度为0的节点',
24
+ _b[WarningCode.START_NODE_INCOMING] = '开始节点不允许被连入',
25
+ _b[WarningCode.EXPRESSION_EXEC_ERROR] = '表达式执行异常',
26
+ _b);
27
+ export var getErrorMsg = function (code) { return "error[" + code + "]: " + errorMsgMapCn[code]; };
28
+ export var getWarningMsg = function (code) { return "warning[" + code + "]: " + warningMsgMapCn[code]; };
@@ -1,10 +1,10 @@
1
1
  // baseType
2
- export const BASE_START_NODE = 'start';
3
- // eventType
4
- export const EVENT_INSTANCE_COMPLETE = 'instance:complete';
5
- export const EVENT_INSTANCE_INTERRUPTED = 'instance:interrupted';
6
- export const EVENT_INSTANCE_ERROR = 'instance:error';
7
- // flowStatus
2
+ export var BASE_START_NODE = 'start';
3
+ // event name
4
+ export var EVENT_INSTANCE_COMPLETE = 'instance:complete';
5
+ export var EVENT_INSTANCE_INTERRUPTED = 'instance:interrupted';
6
+ export var EVENT_INSTANCE_ERROR = 'instance:error';
7
+ // flow status
8
8
  export var FlowStatus;
9
9
  (function (FlowStatus) {
10
10
  FlowStatus["COMPLETED"] = "completed";
@@ -13,11 +13,10 @@ export var FlowStatus;
13
13
  FlowStatus["PENDING"] = "pending";
14
14
  FlowStatus["ERROR"] = "error";
15
15
  })(FlowStatus || (FlowStatus = {}));
16
- // actionStatus
16
+ // action status
17
17
  export var ActionStatus;
18
18
  (function (ActionStatus) {
19
19
  ActionStatus["SUCCESS"] = "success";
20
20
  ActionStatus["ERROR"] = "error";
21
21
  ActionStatus["INTERRUPTED"] = "interrupted";
22
22
  })(ActionStatus || (ActionStatus = {}));
23
- //# sourceMappingURL=index.js.map
@@ -0,0 +1,4 @@
1
+ declare const createContext: (globalData: any) => any;
2
+ declare const runInContext: (code: any, context: any) => any;
3
+ declare const runInBrowserContext: (code: string, globalData?: any) => Promise<any>;
4
+ export { runInBrowserContext, createContext, runInContext, };
@@ -0,0 +1,76 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __generator = (this && this.__generator) || function (thisArg, body) {
11
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
12
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
+ function verb(n) { return function (v) { return step([n, v]); }; }
14
+ function step(op) {
15
+ if (f) throw new TypeError("Generator is already executing.");
16
+ while (_) try {
17
+ 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;
18
+ if (y = 0, t) op = [op[0] & 2, t.value];
19
+ switch (op[0]) {
20
+ case 0: case 1: t = op; break;
21
+ case 4: _.label++; return { value: op[1], done: false };
22
+ case 5: _.label++; y = op[1]; op = [0]; continue;
23
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
+ default:
25
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
+ if (t[2]) _.ops.pop();
30
+ _.trys.pop(); continue;
31
+ }
32
+ op = body.call(thisArg, _);
33
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
+ }
36
+ };
37
+ import { ErrorCode, WarningCode, getErrorMsg, getWarningMsg, } from '../constant/LogCode';
38
+ var createContext = function (globalData) {
39
+ var iframe = document.createElement('iframe');
40
+ iframe.style.display = 'none';
41
+ if (!document || !document.body) {
42
+ console.error(getErrorMsg(ErrorCode.NO_DOCUMENT_BODY));
43
+ }
44
+ document.body.appendChild(iframe);
45
+ var iframeWindow = iframe.contentWindow;
46
+ iframeWindow.parent = null;
47
+ Object.keys(globalData).forEach(function (key) {
48
+ iframeWindow[key] = globalData[key];
49
+ });
50
+ return iframeWindow;
51
+ };
52
+ var runInContext = function (code, context) {
53
+ try {
54
+ var iframeEval = context.eval;
55
+ iframeEval.call(context, code);
56
+ if (context.frameElement) {
57
+ document.body.removeChild(context.frameElement);
58
+ }
59
+ }
60
+ catch (e) {
61
+ console.warn(getWarningMsg(WarningCode.EXPRESSION_EXEC_ERROR), { code: code, context: context, e: e });
62
+ }
63
+ return context;
64
+ };
65
+ var runInBrowserContext = function (code, globalData) {
66
+ if (globalData === void 0) { globalData = {}; }
67
+ return __awaiter(void 0, void 0, void 0, function () {
68
+ var context;
69
+ return __generator(this, function (_a) {
70
+ context = createContext(globalData);
71
+ runInContext(code, context);
72
+ return [2 /*return*/, context];
73
+ });
74
+ });
75
+ };
76
+ export { runInBrowserContext, createContext, runInContext, };
@@ -1,2 +1,2 @@
1
1
  declare const getExpressionResult: (code: string, context: any) => Promise<any>;
2
- export { getExpressionResult };
2
+ export { getExpressionResult, };
@@ -1,17 +1,54 @@
1
- import { __awaiter } from "tslib";
2
- import { runInNodeContext } from './nodeVm';
3
- import { runInBrowserContext } from './brewserVm';
4
- import { isInNodeJS, isInBrowser, globalScope } from '../utils';
5
- const getExpressionResult = (code, context) => __awaiter(void 0, void 0, void 0, function* () {
6
- if (isInNodeJS) {
7
- const r = yield runInNodeContext(code, context);
8
- return r;
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __generator = (this && this.__generator) || function (thisArg, body) {
11
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
12
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
+ function verb(n) { return function (v) { return step([n, v]); }; }
14
+ function step(op) {
15
+ if (f) throw new TypeError("Generator is already executing.");
16
+ while (_) try {
17
+ 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;
18
+ if (y = 0, t) op = [op[0] & 2, t.value];
19
+ switch (op[0]) {
20
+ case 0: case 1: t = op; break;
21
+ case 4: _.label++; return { value: op[1], done: false };
22
+ case 5: _.label++; y = op[1]; op = [0]; continue;
23
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
+ default:
25
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
+ if (t[2]) _.ops.pop();
30
+ _.trys.pop(); continue;
31
+ }
32
+ op = body.call(thisArg, _);
33
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
9
35
  }
10
- if (isInBrowser) {
11
- const r = yield runInBrowserContext(code, context);
12
- return r;
13
- }
14
- return globalScope.eval(code);
15
- });
16
- export { getExpressionResult };
17
- //# sourceMappingURL=index.js.map
36
+ };
37
+ // import { runInNewContext } from './nodeVm';
38
+ import { runInBrowserContext } from './browserVm';
39
+ import { isInBrowser, globalScope } from '../util/global';
40
+ var getExpressionResult = function (code, context) { return __awaiter(void 0, void 0, void 0, function () {
41
+ var r;
42
+ return __generator(this, function (_a) {
43
+ switch (_a.label) {
44
+ case 0:
45
+ if (!isInBrowser) return [3 /*break*/, 2];
46
+ return [4 /*yield*/, runInBrowserContext(code, context)];
47
+ case 1:
48
+ r = _a.sent();
49
+ return [2 /*return*/, r];
50
+ case 2: return [2 /*return*/, globalScope.eval(code)]; // eslint-disable-line no-eval
51
+ }
52
+ });
53
+ }); };
54
+ export { getExpressionResult, };