@logicflow/engine 0.0.2 → 0.0.4

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.
@@ -67,13 +67,13 @@ var BaseNode = /** @class */ (function () {
67
67
  this.incoming = nodeConfig.incoming;
68
68
  this.nodeId = nodeConfig.id;
69
69
  this.type = nodeConfig.type;
70
- this.properties = nodeConfig.properties;
70
+ this.properties = nodeConfig.properties || {};
71
71
  this.context = context;
72
72
  this.globalData = globalData;
73
73
  this.baseType = 'base';
74
74
  }
75
75
  /**
76
- * 节点的每一次执行都会生成一个唯一的taskId
76
+ * 节点的每一次执行都会生成一个唯一的actionId
77
77
  */
78
78
  BaseNode.prototype.execute = function (params) {
79
79
  return __awaiter(this, void 0, void 0, function () {
@@ -82,18 +82,18 @@ var BaseNode = /** @class */ (function () {
82
82
  switch (_a.label) {
83
83
  case 0: return [4 /*yield*/, this.action({
84
84
  executionId: params.executionId,
85
- taskId: params.taskId,
85
+ actionId: params.actionId,
86
86
  nodeId: this.nodeId,
87
87
  })];
88
88
  case 1:
89
89
  r = _a.sent();
90
- if (!(!r || r.status === constant_1.TaskStatus.SUCCESS)) return [3 /*break*/, 3];
90
+ if (!(!r || r.status === constant_1.ActionStatus.SUCCESS)) return [3 /*break*/, 3];
91
91
  return [4 /*yield*/, this.getOutgoing()];
92
92
  case 2:
93
93
  outgoing = _a.sent();
94
94
  params.next({
95
95
  executionId: params.executionId,
96
- taskId: params.taskId,
96
+ actionId: params.actionId,
97
97
  nodeId: this.nodeId,
98
98
  nodeType: this.type,
99
99
  properties: this.properties,
@@ -104,7 +104,7 @@ var BaseNode = /** @class */ (function () {
104
104
  status: r && r.status,
105
105
  detail: r && r.detail,
106
106
  executionId: params.executionId,
107
- taskId: params.taskId,
107
+ actionId: params.actionId,
108
108
  nodeId: this.nodeId,
109
109
  nodeType: this.type,
110
110
  properties: this.properties,
@@ -128,14 +128,14 @@ var BaseNode = /** @class */ (function () {
128
128
  return [4 /*yield*/, this.onResume({
129
129
  executionId: params.executionId,
130
130
  nodeId: params.nodeId,
131
- taskId: params.taskId,
131
+ actionId: params.actionId,
132
132
  data: params.data,
133
133
  })];
134
134
  case 2:
135
135
  _a.sent();
136
136
  params.next({
137
137
  executionId: params.executionId,
138
- taskId: params.taskId,
138
+ actionId: params.actionId,
139
139
  nodeId: this.nodeId,
140
140
  nodeType: this.type,
141
141
  properties: this.properties,
@@ -213,7 +213,7 @@ var BaseNode = /** @class */ (function () {
213
213
  * 节点的执行逻辑
214
214
  * @overridable 可以自定义节点重写此方法。
215
215
  * @param params.executionId 流程执行记录ID
216
- * @param params.taskId 此节点执行记录ID
216
+ * @param params.actionId 此节点执行记录ID
217
217
  * @param params.nodeId 节点ID
218
218
  */
219
219
  BaseNode.prototype.action = function (params) {
@@ -227,7 +227,7 @@ var BaseNode = /** @class */ (function () {
227
227
  * 节点的重新恢复执行逻辑
228
228
  * @overridable 可以自定义节点重写此方法。
229
229
  * @param params.executionId 流程执行记录ID
230
- * @param params.taskId 此节点执行记录ID
230
+ * @param params.actionId 此节点执行记录ID
231
231
  * @param params.nodeId 节点ID
232
232
  */
233
233
  BaseNode.prototype.onResume = function (params) {
@@ -38,13 +38,18 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
39
  var storage_1 = require("../util/storage");
40
40
  var LOGICFLOW_ENGINE_INSTANCES = 'LOGICFLOW_ENGINE_INSTANCES';
41
+ var MAX_RECORDER = 100;
41
42
  var Recorder = /** @class */ (function () {
42
43
  function Recorder() {
44
+ this.maxRecorder = MAX_RECORDER;
43
45
  }
46
+ Recorder.prototype.setMaxRecorderNumber = function (maxRecorder) {
47
+ this.maxRecorder = maxRecorder;
48
+ };
44
49
  /*
45
- * @param {Object} task
50
+ * @param {Object} action
46
51
  * {
47
- * taskId: '',
52
+ * actionId: '',
48
53
  * nodeId: '',
49
54
  * executionId: '',
50
55
  * nodeType: '',
@@ -52,55 +57,80 @@ var Recorder = /** @class */ (function () {
52
57
  * properties: {},
53
58
  * }
54
59
  */
55
- Recorder.prototype.addTask = function (task) {
60
+ Recorder.prototype.addActionRecord = function (action) {
56
61
  return __awaiter(this, void 0, void 0, function () {
57
- var executionId, taskId, instanceData;
62
+ var executionId, actionId, instanceData;
58
63
  return __generator(this, function (_a) {
59
- executionId = task.executionId, taskId = task.taskId;
60
- instanceData = this.getExecutionTasks(executionId);
61
- if (!instanceData) {
62
- this.pushExecution(executionId);
64
+ switch (_a.label) {
65
+ case 0:
66
+ executionId = action.executionId, actionId = action.actionId;
67
+ return [4 /*yield*/, this.getExecutionActions(executionId)];
68
+ case 1:
69
+ instanceData = _a.sent();
70
+ if (!instanceData) {
71
+ this.pushExecution(executionId);
72
+ }
73
+ this.pushActionToExecution(executionId, actionId);
74
+ storage_1.default.setItem(actionId, action);
75
+ return [2 /*return*/];
63
76
  }
64
- this.pushTaskToExecution(executionId, taskId);
65
- storage_1.default.setItem(taskId, task);
66
- return [2 /*return*/];
67
77
  });
68
78
  });
69
79
  };
70
- Recorder.prototype.getTask = function (taskId) {
80
+ Recorder.prototype.getActionRecord = function (actionId) {
71
81
  return __awaiter(this, void 0, void 0, function () {
72
82
  return __generator(this, function (_a) {
73
- return [2 /*return*/, storage_1.default.getItem(taskId)];
83
+ return [2 /*return*/, storage_1.default.getItem(actionId)];
74
84
  });
75
85
  });
76
86
  };
77
- Recorder.prototype.getExecutionTasks = function (executionId) {
87
+ Recorder.prototype.getExecutionActions = function (executionId) {
78
88
  return __awaiter(this, void 0, void 0, function () {
79
89
  return __generator(this, function (_a) {
80
90
  return [2 /*return*/, storage_1.default.getItem(executionId)];
81
91
  });
82
92
  });
83
93
  };
94
+ Recorder.prototype.getExecutionList = function () {
95
+ return __awaiter(this, void 0, void 0, function () {
96
+ var instances;
97
+ return __generator(this, function (_a) {
98
+ instances = storage_1.default.getItem(LOGICFLOW_ENGINE_INSTANCES) || [];
99
+ return [2 /*return*/, instances];
100
+ });
101
+ });
102
+ };
84
103
  Recorder.prototype.clear = function () {
85
- var instance = storage_1.default.getItem(LOGICFLOW_ENGINE_INSTANCES) || [];
86
- instance.forEach(function (executionId) {
104
+ var instances = storage_1.default.getItem(LOGICFLOW_ENGINE_INSTANCES) || [];
105
+ instances.forEach(function (executionId) {
87
106
  storage_1.default.removeItem(executionId);
88
107
  var instanceData = storage_1.default.getItem(executionId) || [];
89
- instanceData.forEach(function (taskId) {
90
- storage_1.default.removeItem(taskId);
108
+ instanceData.forEach(function (actionId) {
109
+ storage_1.default.removeItem(actionId);
91
110
  });
92
111
  });
93
112
  storage_1.default.removeItem(LOGICFLOW_ENGINE_INSTANCES);
94
113
  };
95
114
  Recorder.prototype.pushExecution = function (executionId) {
96
115
  var instance = storage_1.default.getItem(LOGICFLOW_ENGINE_INSTANCES) || [];
116
+ if (instance.length >= this.maxRecorder) {
117
+ var removeItem = instance.shift();
118
+ this.popExecution(removeItem);
119
+ }
97
120
  instance.push(executionId);
98
121
  storage_1.default.setItem(LOGICFLOW_ENGINE_INSTANCES, instance);
99
122
  };
100
- Recorder.prototype.pushTaskToExecution = function (executionId, taskId) {
101
- var tasks = storage_1.default.getItem(executionId) || [];
102
- tasks.push(taskId);
103
- storage_1.default.setItem(executionId, tasks);
123
+ Recorder.prototype.popExecution = function (executionId) {
124
+ var instanceData = storage_1.default.getItem(executionId) || [];
125
+ instanceData.forEach(function (actionId) {
126
+ storage_1.default.removeItem(actionId);
127
+ });
128
+ storage_1.default.removeItem(executionId);
129
+ };
130
+ Recorder.prototype.pushActionToExecution = function (executionId, actionId) {
131
+ var actions = storage_1.default.getItem(executionId) || [];
132
+ actions.push(actionId);
133
+ storage_1.default.setItem(executionId, actions);
104
134
  };
105
135
  return Recorder;
106
136
  }());
package/cjs/util/ID.js CHANGED
@@ -1,12 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createTaskId = exports.createExecId = void 0;
3
+ exports.createEngineId = exports.createActionId = exports.createExecId = void 0;
4
4
  var uuid_1 = require("uuid");
5
5
  exports.createExecId = function () {
6
6
  var uuid = uuid_1.v4();
7
7
  return "exec-" + uuid;
8
8
  };
9
- exports.createTaskId = function () {
9
+ exports.createActionId = function () {
10
10
  var uuid = uuid_1.v4();
11
- return "task-" + uuid;
11
+ return "action-" + uuid;
12
+ };
13
+ exports.createEngineId = function () {
14
+ var uuid = uuid_1.v4();
15
+ return "engine-" + uuid;
12
16
  };
package/es/FlowModel.d.ts CHANGED
@@ -1,20 +1,19 @@
1
1
  import type { NodeConfig, NodeConstructor } from './nodes/BaseNode';
2
2
  import type Recorder from './recorder';
3
3
  import Scheduler from './Scheduler';
4
- import type { TaskParam } from './types.d';
4
+ import type { ActionParam } from './types.d';
5
5
  export declare type FlowResult = {
6
6
  result?: Record<string, any>;
7
- } & TaskParam;
8
- export declare type TaskParams = {
7
+ } & ActionParam;
8
+ export declare type ActionParams = {
9
9
  executionId?: string;
10
- taskId?: string;
10
+ actionId?: string;
11
11
  nodeId?: string;
12
- data?: Record<string, any>;
13
12
  };
14
13
  export declare type ExecParams = {
15
14
  callback?: (result: FlowResult) => void;
16
15
  onError?: (error: Error) => void;
17
- } & TaskParams;
16
+ } & ActionParams;
18
17
  export default class FlowModel {
19
18
  /**
20
19
  * 流程支持的节点类型
@@ -27,7 +26,7 @@ export default class FlowModel {
27
26
  /**
28
27
  * 待执行的队列,当流程正在执行时,如果再次触发执行。那么会将执行参数放入到队列中,等待上一次执行完成后再执行。
29
28
  */
30
- executeQueue: ExecParams[];
29
+ executeList: ExecParams[];
31
30
  /**
32
31
  * 当前正在执行。当监听到调度器执行完成时,出触发执行参数中的回调,告知外部执行完成。
33
32
  */
@@ -114,16 +113,17 @@ export default class FlowModel {
114
113
  * 一个流程存在着两个开始节点,A和B,A和B的下一个节点都是C,C的下两个节点是D和E。
115
114
  * 外部分别触发了A和B的执行,那么A和B的执行是串行的(也就是需要A执行完成后再执行B),但是D和E的执行是并行的。
116
115
  * 如果希望A和B的执行是并行的,就不能使用同一个流程模型执行,应该初始化两个。
116
+ * TODO: 去掉此处的对列,直接使用调度器的队列。
117
117
  */
118
118
  execute(params: ExecParams): Promise<void>;
119
119
  resume(params: ExecParams): Promise<void>;
120
120
  /**
121
- * 创建节点实例, 每个节点实例都会有一个唯一的taskId
122
- * 通过executionId、nodeId、taskId可以唯一确定一个节点的某一次执行。
121
+ * 创建节点实例, 每个节点实例都会有一个唯一的actionId
122
+ * 通过executionId、nodeId、actionId可以唯一确定一个节点的某一次执行。
123
123
  * @param nodeId 节点Id
124
124
  * @returns 节点示例
125
125
  */
126
- createTask(nodeId: string): import("./nodes/BaseNode").default;
126
+ createAction(nodeId: string): import("./nodes/BaseNode").default;
127
127
  /**
128
128
  * 更新流程全局数据
129
129
  */
@@ -132,7 +132,7 @@ export default class FlowModel {
132
132
  * 在执行完成后,通知外部此次执行完成。
133
133
  * 如果还存在待执行的任务,那么继续执行。
134
134
  */
135
- private onTaskFinished;
135
+ private onExecuteFinished;
136
136
  /**
137
137
  * 从待执行队列中取出需要执行的内容。
138
138
  * 会依次判断是否有taskId、nodeId、executionId。
package/es/FlowModel.js CHANGED
@@ -68,7 +68,7 @@ var FlowModel = /** @class */ (function () {
68
68
  // 流程包含的节点类型
69
69
  this.nodeModelMap = nodeModelMap;
70
70
  // 需要执行的队列
71
- this.executeQueue = [];
71
+ this.executeList = [];
72
72
  // 执行中的任务
73
73
  this.executingInstance = null;
74
74
  // 外部传入的上下文,最终会传递给每个节点
@@ -83,10 +83,10 @@ var FlowModel = /** @class */ (function () {
83
83
  recorder: recorder,
84
84
  });
85
85
  this.scheduler.on(EVENT_INSTANCE_COMPLETE, function (result) {
86
- _this.onTaskFinished(result);
86
+ _this.onExecuteFinished(result);
87
87
  });
88
88
  this.scheduler.on(EVENT_INSTANCE_INTERRUPTED, function (result) {
89
- _this.onTaskFinished(result);
89
+ _this.onExecuteFinished(result);
90
90
  });
91
91
  }
92
92
  FlowModel.prototype.setStartNodeType = function (startNodeType) {
@@ -175,16 +175,12 @@ var FlowModel = /** @class */ (function () {
175
175
  * 一个流程存在着两个开始节点,A和B,A和B的下一个节点都是C,C的下两个节点是D和E。
176
176
  * 外部分别触发了A和B的执行,那么A和B的执行是串行的(也就是需要A执行完成后再执行B),但是D和E的执行是并行的。
177
177
  * 如果希望A和B的执行是并行的,就不能使用同一个流程模型执行,应该初始化两个。
178
+ * TODO: 去掉此处的对列,直接使用调度器的队列。
178
179
  */
179
180
  FlowModel.prototype.execute = function (params) {
180
181
  return __awaiter(this, void 0, void 0, function () {
181
182
  return __generator(this, function (_a) {
182
- this.executeQueue.push(params);
183
- if (this.isRunning) {
184
- return [2 /*return*/];
185
- }
186
- this.isRunning = true;
187
- this.createExecution();
183
+ this.createExecution(params);
188
184
  return [2 /*return*/];
189
185
  });
190
186
  });
@@ -192,31 +188,26 @@ var FlowModel = /** @class */ (function () {
192
188
  FlowModel.prototype.resume = function (params) {
193
189
  return __awaiter(this, void 0, void 0, function () {
194
190
  return __generator(this, function (_a) {
195
- this.executeQueue.push(params);
196
- if (this.isRunning) {
197
- return [2 /*return*/];
198
- }
199
- this.isRunning = true;
200
- this.createExecution();
191
+ this.createExecution(params);
201
192
  return [2 /*return*/];
202
193
  });
203
194
  });
204
195
  };
205
196
  /**
206
- * 创建节点实例, 每个节点实例都会有一个唯一的taskId
207
- * 通过executionId、nodeId、taskId可以唯一确定一个节点的某一次执行。
197
+ * 创建节点实例, 每个节点实例都会有一个唯一的actionId
198
+ * 通过executionId、nodeId、actionId可以唯一确定一个节点的某一次执行。
208
199
  * @param nodeId 节点Id
209
200
  * @returns 节点示例
210
201
  */
211
- FlowModel.prototype.createTask = function (nodeId) {
202
+ FlowModel.prototype.createAction = function (nodeId) {
212
203
  var nodeConfig = this.nodeConfigMap.get(nodeId);
213
204
  var NodeModel = this.nodeModelMap.get(nodeConfig.type);
214
- var task = new NodeModel({
205
+ var action = new NodeModel({
215
206
  nodeConfig: nodeConfig,
216
207
  globalData: this.globalData,
217
208
  context: this.context,
218
209
  });
219
- return task;
210
+ return action;
220
211
  };
221
212
  /**
222
213
  * 更新流程全局数据
@@ -228,17 +219,12 @@ var FlowModel = /** @class */ (function () {
228
219
  * 在执行完成后,通知外部此次执行完成。
229
220
  * 如果还存在待执行的任务,那么继续执行。
230
221
  */
231
- FlowModel.prototype.onTaskFinished = function (result) {
232
- var callback = this.executingInstance.callback;
233
- if (callback) {
234
- callback(result);
235
- }
236
- this.executingInstance = null;
237
- if (this.executeQueue.length > 0) {
238
- this.createExecution();
239
- }
240
- else {
241
- this.isRunning = false;
222
+ FlowModel.prototype.onExecuteFinished = function (result) {
223
+ var index = this.executeList.findIndex(function (i) { return i.executionId === result.executionId; });
224
+ if (index !== -1) {
225
+ var callback = this.executeList[index].callback;
226
+ this.executeList.splice(index, 1);
227
+ callback && callback(result);
242
228
  }
243
229
  };
244
230
  /**
@@ -248,21 +234,21 @@ var FlowModel = /** @class */ (function () {
248
234
  * 若存在nodeId,那么表示从指定节点开始执行。
249
235
  * 若都不存在,那么新建一个executionId,从开始节点开始执行。
250
236
  */
251
- FlowModel.prototype.createExecution = function () {
237
+ FlowModel.prototype.createExecution = function (execParams) {
252
238
  var _this = this;
253
- var execParams = this.executeQueue.shift();
254
- this.executingInstance = execParams;
239
+ this.executeList.push(execParams);
255
240
  // 如果有taskId,那么表示恢复执行
256
- if (execParams.taskId && execParams.executionId && execParams.nodeId) {
241
+ if (execParams.actionId && execParams.executionId && execParams.nodeId) {
257
242
  this.scheduler.resume({
258
243
  executionId: execParams.executionId,
259
- taskId: execParams.taskId,
244
+ actionId: execParams.actionId,
260
245
  nodeId: execParams.nodeId,
261
246
  data: execParams.data,
262
247
  });
263
248
  return;
264
249
  }
265
- var executionId = execParams.executionId || createExecId();
250
+ var executionId = createExecId();
251
+ execParams.executionId = executionId;
266
252
  if (execParams.nodeId) {
267
253
  var nodeConfig = this.nodeConfigMap.get(execParams.nodeId);
268
254
  if (!nodeConfig) {
@@ -272,7 +258,7 @@ var FlowModel = /** @class */ (function () {
272
258
  this.startNodes = [nodeConfig];
273
259
  }
274
260
  this.startNodes.forEach(function (startNode) {
275
- _this.scheduler.addTask({
261
+ _this.scheduler.addAction({
276
262
  executionId: executionId,
277
263
  nodeId: startNode.id,
278
264
  });
package/es/Scheduler.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import EventEmitter from './EventEmitter';
2
- import type { TaskParam, NodeParam, ResumeParam } from './types.d';
2
+ import type { ActionParam, NodeParam, ResumeParam } from './types.d';
3
3
  import type FlowModel from './FlowModel';
4
4
  import type Recorder from './recorder';
5
- declare type TaskParamMap = Map<string, TaskParam>;
5
+ declare type ActionParamMap = Map<string, ActionParam>;
6
6
  declare type ExecutionId = string;
7
7
  /**
8
8
  * 调度器
@@ -18,7 +18,7 @@ export default class Scheduler extends EventEmitter {
18
18
  * 在每个节点执行完成后,会从集合中删除。
19
19
  * 同时会判断此集合中是否还存在和此节点相同的executionId,如果不存在,说明此流程已经执行完成。
20
20
  */
21
- taskRunningMap: Map<ExecutionId, TaskParamMap>;
21
+ actionRunningMap: Map<ExecutionId, ActionParamMap>;
22
22
  /**
23
23
  * 流程模型,用于创建节点模型。
24
24
  */
@@ -34,7 +34,7 @@ export default class Scheduler extends EventEmitter {
34
34
  * 1. 由流程模型将所有的开始节点添加到队列中。
35
35
  * 2. 当一个节点执行完成后,将后续的节点添加到队列中。
36
36
  */
37
- addTask(nodeParam: NodeParam): void;
37
+ addAction(nodeParam: NodeParam): void;
38
38
  /**
39
39
  * 调度器执行下一个任务
40
40
  * 1. 提供给流程模型,用户开始执行第一个任务。
@@ -44,19 +44,19 @@ export default class Scheduler extends EventEmitter {
44
44
  run(runParams: {
45
45
  executionId: string;
46
46
  nodeId?: string;
47
- taskId?: string;
47
+ actionId?: string;
48
48
  }): void;
49
49
  /**
50
50
  * 恢复某个任务的执行。
51
51
  * 可以自定义节点手动实现流程中断,然后通过此方法恢复流程的执行。
52
52
  */
53
53
  resume(resumeParam: ResumeParam): Promise<void>;
54
- private pushTaskToRunningMap;
55
- private removeTaskFromRunningMap;
56
- private hasRunningTask;
54
+ private pushActionToRunningMap;
55
+ private removeActionFromRunningMap;
56
+ private hasRunningAction;
57
57
  private exec;
58
58
  private interrupted;
59
59
  private next;
60
- private saveTaskResult;
60
+ private saveActionResult;
61
61
  }
62
62
  export {};