@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
@@ -0,0 +1,168 @@
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
+ var storage_1 = require("../util/storage");
40
+ var LOGICFLOW_ENGINE_INSTANCES = 'LOGICFLOW_ENGINE_INSTANCES';
41
+ var MAX_RECORDER = 100;
42
+ var MAX_INSTANCE = 100;
43
+ var Recorder = /** @class */ (function () {
44
+ function Recorder(_a) {
45
+ var instanceId = _a.instanceId;
46
+ this.maxRecorder = MAX_RECORDER;
47
+ this.instanceId = instanceId;
48
+ var instances = this.getItem(LOGICFLOW_ENGINE_INSTANCES) || [];
49
+ if (instances.indexOf(instanceId) === -1) {
50
+ instances.push(instanceId);
51
+ }
52
+ if (instances.length > MAX_INSTANCE) {
53
+ var clearInstance = instances.shift();
54
+ this.clearInstance(clearInstance);
55
+ }
56
+ this.setItem(LOGICFLOW_ENGINE_INSTANCES, instances);
57
+ }
58
+ Recorder.prototype.setMaxRecorderNumber = function (maxRecorder) {
59
+ this.maxRecorder = maxRecorder;
60
+ };
61
+ // 将存储 storage 的方法收敛到此处,并在此处做异常处理 - setItem
62
+ Recorder.prototype.setItem = function (key, value) {
63
+ try {
64
+ storage_1.default.setItem(key, value);
65
+ }
66
+ catch (error) {
67
+ console.log('Ops, something wrong with storage.setItem');
68
+ storage_1.default.clear();
69
+ storage_1.default.setItem(key, value);
70
+ }
71
+ };
72
+ // getItem 方法
73
+ Recorder.prototype.getItem = function (key) {
74
+ return storage_1.default.getItem(key);
75
+ };
76
+ /*
77
+ * @param {Object} action
78
+ * {
79
+ * actionId: '',
80
+ * nodeId: '',
81
+ * executionId: '',
82
+ * nodeType: '',
83
+ * timestamp: '',
84
+ * properties: {},
85
+ * }
86
+ */
87
+ Recorder.prototype.addActionRecord = function (action) {
88
+ return __awaiter(this, void 0, void 0, function () {
89
+ var executionId, actionId, instanceData;
90
+ return __generator(this, function (_a) {
91
+ switch (_a.label) {
92
+ case 0:
93
+ executionId = action.executionId, actionId = action.actionId;
94
+ return [4 /*yield*/, this.getExecutionActions(executionId)];
95
+ case 1:
96
+ instanceData = _a.sent();
97
+ if (!instanceData) {
98
+ this.pushExecution(executionId);
99
+ }
100
+ this.pushActionToExecution(executionId, actionId);
101
+ this.setItem(actionId, action);
102
+ return [2 /*return*/];
103
+ }
104
+ });
105
+ });
106
+ };
107
+ Recorder.prototype.getActionRecord = function (actionId) {
108
+ return __awaiter(this, void 0, void 0, function () {
109
+ return __generator(this, function (_a) {
110
+ return [2 /*return*/, this.getItem(actionId)];
111
+ });
112
+ });
113
+ };
114
+ Recorder.prototype.getExecutionActions = function (executionId) {
115
+ return __awaiter(this, void 0, void 0, function () {
116
+ return __generator(this, function (_a) {
117
+ return [2 /*return*/, this.getItem(executionId)];
118
+ });
119
+ });
120
+ };
121
+ Recorder.prototype.getExecutionList = function () {
122
+ return __awaiter(this, void 0, void 0, function () {
123
+ var instances;
124
+ return __generator(this, function (_a) {
125
+ instances = this.getItem(this.instanceId) || [];
126
+ return [2 /*return*/, instances];
127
+ });
128
+ });
129
+ };
130
+ Recorder.prototype.clear = function () {
131
+ this.clearInstance(this.instanceId);
132
+ };
133
+ Recorder.prototype.clearInstance = function (instanceId) {
134
+ var _this = this;
135
+ var instanceExecutions = this.getItem(instanceId) || [];
136
+ instanceExecutions.forEach(function (executionId) {
137
+ storage_1.default.removeItem(executionId);
138
+ var instanceData = _this.getItem(executionId) || [];
139
+ instanceData.forEach(function (actionId) {
140
+ storage_1.default.removeItem(actionId);
141
+ });
142
+ });
143
+ storage_1.default.removeItem(instanceId);
144
+ };
145
+ Recorder.prototype.pushExecution = function (executionId) {
146
+ var instanceExecutions = this.getItem(this.instanceId) || [];
147
+ if (instanceExecutions.length >= this.maxRecorder) {
148
+ var removeItem = instanceExecutions.shift();
149
+ this.popExecution(removeItem);
150
+ }
151
+ instanceExecutions.push(executionId);
152
+ this.setItem(this.instanceId, instanceExecutions);
153
+ };
154
+ Recorder.prototype.popExecution = function (executionId) {
155
+ var instanceData = this.getItem(executionId) || [];
156
+ instanceData.forEach(function (actionId) {
157
+ storage_1.default.removeItem(actionId);
158
+ });
159
+ storage_1.default.removeItem(executionId);
160
+ };
161
+ Recorder.prototype.pushActionToExecution = function (executionId, actionId) {
162
+ var actions = this.getItem(executionId) || [];
163
+ actions.push(actionId);
164
+ this.setItem(executionId, actions);
165
+ };
166
+ return Recorder;
167
+ }());
168
+ exports.default = Recorder;
package/cjs/util/ID.js ADDED
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createEngineId = exports.createActionId = exports.createExecId = void 0;
4
+ var uuid_1 = require("uuid");
5
+ exports.createExecId = function () {
6
+ var uuid = uuid_1.v4();
7
+ return "exec-" + uuid;
8
+ };
9
+ exports.createActionId = function () {
10
+ var uuid = uuid_1.v4();
11
+ return "action-" + uuid;
12
+ };
13
+ exports.createEngineId = function () {
14
+ var uuid = uuid_1.v4();
15
+ return "engine-" + uuid;
16
+ };
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ // The one and only way of getting global scope in all environments
3
+ // https://stackoverflow.com/q/3277182/1008999
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.isInNodeJS = exports.isInWebWorker = exports.isInBrowser = exports.globalScope = void 0;
6
+ var isInBrowser = typeof window === 'object' && window.window === window;
7
+ exports.isInBrowser = isInBrowser;
8
+ var isInNodeJS = typeof global === 'object' && global.global === global;
9
+ exports.isInNodeJS = isInNodeJS;
10
+ // eslint-disable-next-line no-restricted-globals
11
+ var isInWebWorker = !isInBrowser && typeof self === 'object' && self.constructor;
12
+ exports.isInWebWorker = isInWebWorker;
13
+ var globalScope = (function () {
14
+ if (isInBrowser) {
15
+ return window;
16
+ }
17
+ // eslint-disable-next-line no-restricted-globals
18
+ if (typeof self === 'object' && self.self === self) { // web workers
19
+ // eslint-disable-next-line no-restricted-globals
20
+ return self;
21
+ }
22
+ if (isInNodeJS) {
23
+ return global;
24
+ }
25
+ if (typeof globalThis === 'object') {
26
+ return globalThis;
27
+ }
28
+ return {
29
+ eval: function () { return undefined; },
30
+ };
31
+ })();
32
+ exports.globalScope = globalScope;
@@ -3,46 +3,45 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  /**
4
4
  * 存储执行记录
5
5
  */
6
- const global_1 = require("./global");
6
+ var global_1 = require("./global");
7
7
  if (!global_1.globalScope.sessionStorage) {
8
- const storage = {
8
+ var storage_1 = {
9
9
  data: {},
10
- setItem(key, value) {
11
- storage.data[key] = value;
10
+ setItem: function (key, value) {
11
+ storage_1.data[key] = value;
12
12
  },
13
- getItem(key) {
14
- return storage.data[key];
13
+ getItem: function (key) {
14
+ return storage_1.data[key];
15
15
  },
16
- removeItem(key) {
17
- delete storage.data[key];
16
+ removeItem: function (key) {
17
+ delete storage_1.data[key];
18
18
  },
19
- clear() {
20
- storage.data = {};
19
+ clear: function () {
20
+ storage_1.data = {};
21
21
  },
22
22
  };
23
- global_1.globalScope.sessionStorage = storage;
23
+ global_1.globalScope.sessionStorage = storage_1;
24
24
  }
25
25
  exports.default = {
26
- setItem(key, value) {
26
+ setItem: function (key, value) {
27
27
  if (typeof value === 'object') {
28
28
  value = JSON.stringify(value);
29
29
  }
30
30
  global_1.globalScope.sessionStorage.setItem(key, value);
31
31
  },
32
- getItem(key) {
33
- const value = global_1.globalScope.sessionStorage.getItem(key);
32
+ getItem: function (key) {
33
+ var value = global_1.globalScope.sessionStorage.getItem(key);
34
34
  try {
35
35
  return JSON.parse(value);
36
36
  }
37
- catch (error) {
37
+ catch (e) {
38
38
  return value;
39
39
  }
40
40
  },
41
- removeItem(key) {
41
+ removeItem: function (key) {
42
42
  global_1.globalScope.sessionStorage.removeItem(key);
43
43
  },
44
- clear() {
44
+ clear: function () {
45
45
  global_1.globalScope.sessionStorage.clear();
46
46
  },
47
47
  };
48
- //# sourceMappingURL=storage.js.map
@@ -1,37 +1,7 @@
1
- export interface EventType {
2
- readonly callback: (params?: any) => void;
3
- readonly once: boolean;
4
- }
5
- export type EventArgs = Record<string, unknown>;
6
- export type EventsType = Record<string, EventType[]>;
7
- export type CallbackType = (...args: unknown[]) => void;
8
1
  export default class EventEmitter {
9
- private _events;
2
+ _events: Record<string, any>;
10
3
  constructor();
11
- /**
12
- * 添加一个监听事件
13
- * @param evtKey 事件名称
14
- * @param callback 回调方法
15
- * @param once 是否触发一次
16
- * @returns 当前 EventEmitter 实例
17
- */
18
- on(evtKey: string, callback: CallbackType, once?: boolean): void;
19
- /**
20
- * 取消监听一个事件,或者一个 Channel
21
- * @param evtKey
22
- * @param callback
23
- */
24
- off(evtKey: string, callback?: CallbackType): void;
25
- /**
26
- * 主动触发事件
27
- * @param evtKey 触发事件名称
28
- * @param eventArgs 事件参数
29
- */
30
- emit(evtKey: string, eventArgs: EventArgs): void;
31
- /**
32
- * 获取当前所有事件
33
- * @returns _events
34
- */
35
- getEvents(): EventsType;
4
+ on(evKey: any, callback: any, once?: boolean): void;
5
+ emit(evt: any, eventArgs: any): void;
6
+ off(evt: any, callback: any): void;
36
7
  }
37
- export { EventEmitter };
@@ -1,90 +1,68 @@
1
- export default class EventEmitter {
2
- constructor() {
1
+ var EventEmitter = /** @class */ (function () {
2
+ function EventEmitter() {
3
3
  this._events = {};
4
4
  }
5
- /**
6
- * 添加一个监听事件
7
- * @param evtKey 事件名称
8
- * @param callback 回调方法
9
- * @param once 是否触发一次
10
- * @returns 当前 EventEmitter 实例
11
- */
12
- on(evtKey, callback, once) {
13
- evtKey = evtKey.trim();
14
- if (!this._events[evtKey]) {
15
- this._events[evtKey] = [];
5
+ EventEmitter.prototype.on = function (evKey, callback, once) {
6
+ if (once === void 0) { once = false; }
7
+ evKey = evKey.trim();
8
+ if (!this._events[evKey]) {
9
+ this._events[evKey] = [];
16
10
  }
17
- this._events[evtKey].push({
18
- callback,
11
+ this._events[evKey].push({
12
+ callback: callback,
19
13
  once: !!once,
20
14
  });
21
- }
22
- /**
23
- * 取消监听一个事件,或者一个 Channel
24
- * @param evtKey
25
- * @param callback
26
- */
27
- off(evtKey, callback) {
28
- if (!evtKey) {
29
- // evtKey 为空全部清除
30
- this._events = {};
31
- }
32
- if (!callback) {
33
- // evtKey 存在,callback 为空,清除事件所有方法
34
- delete this._events[evtKey];
35
- }
36
- else {
37
- // evtKey 存在,callback 存在,清除匹配的
38
- const events = this._events[evtKey] || [];
39
- let { length } = events;
40
- for (let i = 0; i < length; i++) {
41
- if (events[i].callback === callback) {
42
- events.splice(i, 1);
43
- length--;
44
- i--;
45
- }
46
- }
47
- if (events.length === 0) {
48
- delete this._events[evtKey];
49
- }
50
- }
51
- }
52
- /**
53
- * 主动触发事件
54
- * @param evtKey 触发事件名称
55
- * @param eventArgs 事件参数
56
- */
57
- emit(evtKey, eventArgs) {
58
- const events = this._events[evtKey] || [];
15
+ };
16
+ EventEmitter.prototype.emit = function (evt, eventArgs) {
17
+ var _this = this;
18
+ var events = this._events[evt] || [];
59
19
  // 实际的处理 emit 方法
60
- const doEmit = (es) => {
61
- let { length } = es;
62
- for (let i = 0; i < length; i++) {
20
+ var doEmit = function (es) {
21
+ var length = es.length;
22
+ for (var i = 0; i < length; i++) {
63
23
  if (!es[i]) {
64
24
  // eslint-disable-next-line no-continue
65
25
  continue;
66
26
  }
67
- const { callback, once } = es[i];
27
+ var _a = es[i], callback = _a.callback, once = _a.once;
68
28
  if (once) {
69
29
  es.splice(i, 1);
70
30
  if (es.length === 0) {
71
- delete this._events[evtKey];
31
+ delete _this._events[evt];
72
32
  }
73
33
  length--;
74
34
  i--;
75
35
  }
76
- callback.apply(this, [eventArgs]);
36
+ callback.apply(_this, [eventArgs]);
77
37
  }
78
38
  };
79
39
  doEmit(events);
80
- }
81
- /**
82
- * 获取当前所有事件
83
- * @returns _events
84
- */
85
- getEvents() {
86
- return this._events;
87
- }
88
- }
89
- export { EventEmitter };
90
- //# sourceMappingURL=EventEmitter.js.map
40
+ };
41
+ EventEmitter.prototype.off = function (evt, callback) {
42
+ if (!evt) {
43
+ // evt 为空全部清除
44
+ this._events = {};
45
+ }
46
+ if (!callback) {
47
+ // evt 存在,callback 为空,清除事件所有方法
48
+ delete this._events[evt];
49
+ }
50
+ else {
51
+ // evt 存在,callback 存在,清除匹配的
52
+ var events = this._events[evt] || [];
53
+ var length_1 = events.length;
54
+ for (var i = 0; i < length_1; i++) {
55
+ if (events[i].callback === callback) {
56
+ events.splice(i, 1);
57
+ length_1--;
58
+ i--;
59
+ }
60
+ }
61
+ if (events.length === 0) {
62
+ delete this._events[evt];
63
+ }
64
+ }
65
+ };
66
+ return EventEmitter;
67
+ }());
68
+ export default EventEmitter;
package/es/FlowModel.d.ts CHANGED
@@ -1,58 +1,76 @@
1
- import { Engine } from '.';
2
- import { Recorder } from './recorder';
3
- import { Scheduler } from './Scheduler';
4
- import { BaseNode } from './nodes';
5
- export declare class FlowModel {
1
+ import type { NodeConfig, NodeConstructor } from './nodes/BaseNode';
2
+ import type Recorder from './recorder';
3
+ import Scheduler from './Scheduler';
4
+ import type { ActionParam, NextActionParam } from './types.d';
5
+ export declare type FlowResult = ({
6
+ result?: Record<string, any>;
7
+ } & ActionParam) | NextActionParam;
8
+ export declare type ActionParams = {
9
+ executionId?: string;
10
+ actionId?: string;
11
+ nodeId?: string;
12
+ };
13
+ export declare type ExecParams = {
14
+ callback?: (result: NextActionParam) => void;
15
+ onError?: (error: Error) => void;
16
+ } & ActionParams;
17
+ export default class FlowModel {
6
18
  /**
7
- * 流程支持的节点类型.
19
+ * 流程支持的节点类型
8
20
  */
9
- nodeModelMap: Map<string, BaseNode.NodeConstructor>;
21
+ nodeModelMap: Map<string, NodeConstructor>;
10
22
  /**
11
- * 调度器,用于调度节点执行
23
+ * 调度器,用于调度节点的执行。
12
24
  */
13
25
  scheduler: Scheduler;
14
26
  /**
15
- * 待执行的队列,当流程正在执行时,如果再次触发执行。那么会将执行参数放到队列中,等待上一次执行完成后再执行。
27
+ * 待执行的队列,当流程正在执行时,如果再次触发执行。那么会将执行参数放入到队列中,等待上一次执行完成后再执行。
16
28
  */
17
- executeList: Partial<FlowModel.ExecParam>[];
29
+ executeList: ExecParams[];
18
30
  /**
19
- * 当前正在执行的任务。当监听到调度器执行完成时,触发执行参数中的回调,告知外部执行完成。
31
+ * 当前正在执行。当监听到调度器执行完成时,出触发执行参数中的回调,告知外部执行完成。
20
32
  */
21
- executingInstance?: FlowModel.ExecParam | null;
33
+ executingInstance: ExecParams;
22
34
  /**
23
- * 当前流程模型中的所有节点,边会被转换成节点的 incoming outgoing 属性
35
+ * 当前流程模型中的所有节点,边会被转换成节点的incoming和outgoing属性。
24
36
  */
25
- nodeConfigMap: Map<Engine.Key, BaseNode.NodeConfig>;
37
+ nodeConfigMap: Map<string, NodeConfig>;
26
38
  /**
27
39
  * 当流程正在执行时,如果再次触发执行。那么会将执行参数放入到队列中,等待上一次执行完成后再执行。
28
40
  */
29
41
  isRunning: boolean;
30
42
  /**
31
- * 开始接地那类型,在执行流程时,会从这些节点开始执行
43
+ * 开始节点类型,在执行流程时,会从这些节点开始执行。
32
44
  */
33
45
  startNodeType: string;
34
46
  /**
35
- * 当前流程中开始节点组成的数组
47
+ * 当前流程中开始节点组成的数组。
36
48
  */
37
- startNodes: BaseNode.NodeConfig[];
49
+ startNodes: NodeConfig[];
38
50
  /**
39
- * 用于存储全局数据,最终会传递给每个节点
51
+ * 用于存储全局数据,可以在流程中共享。
40
52
  */
41
- globalData: Record<string, unknown>;
53
+ globalData: Record<string, any>;
42
54
  /**
43
55
  * 外部传入的上下文,最终会传递给每个节点
44
56
  * 例如:
45
57
  * const context = {
46
- * request: {
47
- * get: (url) => {
48
- * return fetch(url);
49
- * },
50
- * },
58
+ * request: {
59
+ * get: (url) => {
60
+ * return fetch(url);
61
+ * }
51
62
  * }
52
- * 在节点内可以通过 this.context.request.get(url) 来调用。
63
+ * 在节点内部可以通过 this.context.request.get(url) 来调用。
53
64
  */
54
- context: Record<string, unknown>;
55
- constructor({ nodeModelMap, recorder, context, globalData, startNodeType, }: FlowModel.IFlowModelProps);
65
+ context: Record<string, any>;
66
+ constructor({ nodeModelMap, recorder, context, globalData, startNodeType, }: {
67
+ nodeModelMap: Map<string, NodeConstructor>;
68
+ recorder: Recorder;
69
+ context?: Record<string, any>;
70
+ globalData?: Record<string, any>;
71
+ startNodeType?: string;
72
+ });
73
+ setStartNodeType(startNodeType: any): void;
56
74
  /**
57
75
  * 解析LogicFlow图数据,将nodes和edges转换成节点格式。
58
76
  * 例如:
@@ -86,61 +104,41 @@ export declare class FlowModel {
86
104
  * 同时此方法还会找到所有的开始节点,方便后续执行时,从开始节点开始执行。
87
105
  * @param graphData 流程图数据
88
106
  */
89
- load(graphData: Engine.GraphConfigData): void;
107
+ load(graphData: any): void;
90
108
  /**
91
- * 从待执行队列中取出需要执行的内容。
92
- * 会依次判断是否有 actionId、nodeId、executionId。
93
- * 若存在 actionId,那么表示恢复执行
94
- * 若存在 nodeId,那么表示从指定节点开始执行
95
- * 若都不存在,那么新建一个 executionId,从开始节点开始执行
96
- * @private
109
+ * 执行流程, 每次执行都会生成一个唯一的executionId,用于区分不同的执行。
110
+ * 同一次执行,这次执行内部的节点执行顺序为并行。内部并行是为了避免异步节点阻塞其他节点的执行。
111
+ * 多次执行,多次执行之间为串行,这里选择串行的原因是避免多次执行之间的数据冲突。
112
+ * example:
113
+ * 一个流程存在着两个开始节点,A和B,A和B的下一个节点都是C,C的下两个节点是D和E。
114
+ * 外部分别触发了A和B的执行,那么A和B的执行是串行的(也就是需要A执行完成后再执行B),但是D和E的执行是并行的。
115
+ * 如果希望A和B的执行是并行的,就不能使用同一个流程模型执行,应该初始化两个。
116
+ * TODO: 去掉此处的对列,直接使用调度器的队列。
97
117
  */
98
- private createExecution;
118
+ execute(params: ExecParams): Promise<void>;
119
+ resume(params: ExecParams): Promise<void>;
99
120
  /**
100
- * 执行流程,每次执行都会生成一个唯一的 executionId,用于区分不同的执行。
101
- * 同一次执行,这次执行内部的节点执行顺序为并行。内部并行是为了避免异步节点阻塞其他节点的执行
102
- * 多次执行,多次执行之间为串行,这里选择串行的原因是避免多次执行之间的数据冲突。
103
- * 例如:
104
- * 一个流程存在两个开始节点,A 和 B,A 和 B 的下一个节点都是 C,C 的下两个节点是 D 和 E
105
- * 外部分别触发了 A 和 B 的执行,那么 A 和 B 的执行是串行(即 A 执行完再执行 B),但是 D 和 E 的执行是并行的。
106
- * 如果希望 A 和 B 的执行时并行的,就不能使用同一个流程模型执行,应该初始化两个。
107
- * 下面直接使用调度器的队列
108
- * @param params
121
+ * 创建节点实例, 每个节点实例都会有一个唯一的actionId。
122
+ * 通过executionId、nodeId、actionId可以唯一确定一个节点的某一次执行。
123
+ * @param nodeId 节点Id
124
+ * @returns 节点示例
109
125
  */
110
- execute(params: Partial<FlowModel.ExecParam>): Promise<void>;
111
- resume(params: Partial<FlowModel.ExecParam>): Promise<void>;
126
+ createAction(nodeId: string): import("./nodes/BaseNode").default;
112
127
  /**
113
- * 创建节点实例,每个节点实例都会有一个唯一的 actionId
114
- * 通过 executionId, nodeId, actionId 可以唯一确定一个节点的某一次执行
115
- * @param nodeId
116
- * @returns 节点实例
128
+ * 更新流程全局数据
117
129
  */
118
- createAction(nodeId: Engine.Key): BaseNode | undefined;
119
- setStartNodeType(type: string): void;
120
- updateGlobalData(data: Record<string, unknown>): Record<string, unknown>;
130
+ updateGlobalData(data: any): void;
121
131
  /**
122
- * 在执行完成后,通知外部此次之行完成
123
- * 如果还存在待执行的任务,那么继续执行
124
- * @param result
125
- * @private
132
+ * 在执行完成后,通知外部此次执行完成。
133
+ * 如果还存在待执行的任务,那么继续执行。
126
134
  */
127
135
  private onExecuteFinished;
136
+ /**
137
+ * 从待执行队列中取出需要执行的内容。
138
+ * 会依次判断是否有taskId、nodeId、executionId。
139
+ * 若存在taskId,那么表示恢复执行。
140
+ * 若存在nodeId,那么表示从指定节点开始执行。
141
+ * 若都不存在,那么新建一个executionId,从开始节点开始执行。
142
+ */
143
+ private createExecution;
128
144
  }
129
- export declare namespace FlowModel {
130
- type FlowResult = ({
131
- result?: Record<string, unknown>;
132
- } & Engine.ActionParam) | Engine.NextActionParam;
133
- type ActionParam = Scheduler.ActionParam;
134
- type ExecParam = {
135
- callback?: (result: Engine.NextActionParam) => void;
136
- onError?: (error: Error) => void;
137
- } & ActionParam;
138
- interface IFlowModelProps {
139
- nodeModelMap: Map<string, BaseNode.NodeConstructor>;
140
- recorder?: Recorder;
141
- context?: Record<string, unknown>;
142
- globalData?: Record<string, unknown>;
143
- startNodeType?: string;
144
- }
145
- }
146
- export default FlowModel;