@logicflow/engine 0.0.4 → 0.0.6

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.
@@ -1,11 +1,11 @@
1
- import type { ActionResult, NodeExecResult, ExecResumeParams, ExecParams } from '../types.d';
1
+ import type { NextActionParam, ActionResult, ExecResumeParams, ExecParams, OutgoingConfig } from '../types.d';
2
2
  export interface BaseNodeInterface {
3
3
  outgoing: Record<string, any>[];
4
4
  incoming: Record<string, any>[];
5
5
  nodeId: string;
6
6
  type: string;
7
7
  readonly baseType: string;
8
- execute(actionParam: any): Promise<NodeExecResult>;
8
+ execute(actionParam: any): Promise<NextActionParam>;
9
9
  }
10
10
  export declare type NodeConstructor = {
11
11
  new (config: {
@@ -17,7 +17,7 @@ export declare type NodeConstructor = {
17
17
  executionId: string;
18
18
  actionId: string;
19
19
  nodeId: string;
20
- }): Promise<ActionResult>;
20
+ }): Promise<NextActionParam>;
21
21
  onResume(params: {
22
22
  executionId: string;
23
23
  actionId: string;
@@ -30,11 +30,6 @@ export declare type IncomingConfig = {
30
30
  properties?: Record<string, any>;
31
31
  source: string;
32
32
  };
33
- export declare type OutgoingConfig = {
34
- id: string;
35
- target: string;
36
- properties?: Record<string, any>;
37
- };
38
33
  export declare type NodeConfig = {
39
34
  id: string;
40
35
  type: string;
@@ -42,14 +37,6 @@ export declare type NodeConfig = {
42
37
  incoming: IncomingConfig[];
43
38
  outgoing: OutgoingConfig[];
44
39
  };
45
- export declare type NextActionParam = {
46
- executionId: string;
47
- nodeId: string;
48
- actionId: string;
49
- nodeType: string;
50
- outgoing: OutgoingConfig[];
51
- properties?: Record<string, any>;
52
- };
53
40
  export default class BaseNode implements BaseNodeInterface {
54
41
  static nodeTypeName: string;
55
42
  /**
@@ -84,7 +71,7 @@ export default class BaseNode implements BaseNodeInterface {
84
71
  /**
85
72
  * 节点的每一次执行都会生成一个唯一的actionId
86
73
  */
87
- execute(params: ExecParams): Promise<NodeExecResult>;
74
+ execute(params: ExecParams): Promise<NextActionParam>;
88
75
  /**
89
76
  * 节点在执行中断后,可以通过resume方法恢复执行。
90
77
  * 自定义节点时不建议重写此方法
@@ -98,6 +85,9 @@ export default class BaseNode implements BaseNodeInterface {
98
85
  * @param params.executionId 流程执行记录ID
99
86
  * @param params.actionId 此节点执行记录ID
100
87
  * @param params.nodeId 节点ID
88
+ * @returns 返回下一步的执行参数
89
+ * 当不返回时,表示此节点执行成功,流程会继续执行下一步。
90
+ * 当返回时,返回格式
101
91
  */
102
92
  action(params: {
103
93
  executionId: string;
@@ -75,7 +75,7 @@ var BaseNode = /** @class */ (function () {
75
75
  */
76
76
  BaseNode.prototype.execute = function (params) {
77
77
  return __awaiter(this, void 0, void 0, function () {
78
- var r, outgoing;
78
+ var r, status, outgoing, detail;
79
79
  return __generator(this, function (_a) {
80
80
  switch (_a.label) {
81
81
  case 0: return [4 /*yield*/, this.action({
@@ -85,11 +85,15 @@ var BaseNode = /** @class */ (function () {
85
85
  })];
86
86
  case 1:
87
87
  r = _a.sent();
88
- if (!(!r || r.status === ActionStatus.SUCCESS)) return [3 /*break*/, 3];
88
+ status = r ? r.status : 'success';
89
+ if (!(status === ActionStatus.SUCCESS)) return [3 /*break*/, 3];
89
90
  return [4 /*yield*/, this.getOutgoing()];
90
91
  case 2:
91
92
  outgoing = _a.sent();
93
+ detail = r ? r.detail : {};
92
94
  params.next({
95
+ status: ActionStatus.SUCCESS,
96
+ detail: detail,
93
97
  executionId: params.executionId,
94
98
  actionId: params.actionId,
95
99
  nodeId: this.nodeId,
@@ -99,13 +103,14 @@ var BaseNode = /** @class */ (function () {
99
103
  });
100
104
  _a.label = 3;
101
105
  case 3: return [2 /*return*/, {
102
- status: r && r.status,
106
+ status: status,
103
107
  detail: r && r.detail,
104
108
  executionId: params.executionId,
105
109
  actionId: params.actionId,
106
110
  nodeId: this.nodeId,
107
111
  nodeType: this.type,
108
112
  properties: this.properties,
113
+ outgoing: [],
109
114
  }];
110
115
  }
111
116
  });
@@ -138,6 +143,7 @@ var BaseNode = /** @class */ (function () {
138
143
  nodeType: this.type,
139
144
  properties: this.properties,
140
145
  outgoing: outgoing,
146
+ status: ActionStatus.SUCCESS,
141
147
  });
142
148
  return [2 /*return*/, undefined];
143
149
  }
@@ -172,9 +178,9 @@ var BaseNode = /** @class */ (function () {
172
178
  case 1:
173
179
  result = _d.sent();
174
180
  result.forEach(function (item, index) {
175
- if (item) {
176
- outgoing.push(_this.outgoing[index]);
177
- }
181
+ var out = _this.outgoing[index];
182
+ out.result = item;
183
+ outgoing.push(out);
178
184
  });
179
185
  return [2 /*return*/, outgoing];
180
186
  }
@@ -213,11 +219,14 @@ var BaseNode = /** @class */ (function () {
213
219
  * @param params.executionId 流程执行记录ID
214
220
  * @param params.actionId 此节点执行记录ID
215
221
  * @param params.nodeId 节点ID
222
+ * @returns 返回下一步的执行参数
223
+ * 当不返回时,表示此节点执行成功,流程会继续执行下一步。
224
+ * 当返回时,返回格式
216
225
  */
217
226
  BaseNode.prototype.action = function (params) {
218
227
  return __awaiter(this, void 0, void 0, function () {
219
228
  return __generator(this, function (_a) {
220
- return [2 /*return*/, undefined];
229
+ return [2 /*return*/, null];
221
230
  });
222
231
  });
223
232
  };
@@ -1,13 +1,17 @@
1
1
  import type { RecorderData, RecorderInterface } from '../types.d';
2
2
  export default class Recorder implements RecorderInterface {
3
3
  maxRecorder: number;
4
- constructor();
4
+ instanceId: number;
5
+ constructor({ instanceId }: {
6
+ instanceId: any;
7
+ });
5
8
  setMaxRecorderNumber(maxRecorder: number): void;
6
9
  addActionRecord(action: RecorderData): Promise<void>;
7
10
  getActionRecord(actionId: string): Promise<RecorderData>;
8
11
  getExecutionActions(executionId: any): Promise<any>;
9
12
  getExecutionList(): Promise<any>;
10
13
  clear(): void;
14
+ clearInstance(instanceId: any): void;
11
15
  private pushExecution;
12
16
  private popExecution;
13
17
  private pushActionToExecution;
@@ -37,9 +37,21 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
37
37
  import storage from '../util/storage';
38
38
  var LOGICFLOW_ENGINE_INSTANCES = 'LOGICFLOW_ENGINE_INSTANCES';
39
39
  var MAX_RECORDER = 100;
40
+ var MAX_INSTANCE = 100;
40
41
  var Recorder = /** @class */ (function () {
41
- function Recorder() {
42
+ function Recorder(_a) {
43
+ var instanceId = _a.instanceId;
42
44
  this.maxRecorder = MAX_RECORDER;
45
+ this.instanceId = instanceId;
46
+ var instances = storage.getItem(LOGICFLOW_ENGINE_INSTANCES) || [];
47
+ if (instances.indexOf(instanceId) === -1) {
48
+ instances.push(instanceId);
49
+ }
50
+ if (instances.length > MAX_INSTANCE) {
51
+ var clearInstance = instances.shift();
52
+ this.clearInstance(clearInstance);
53
+ }
54
+ storage.setItem(LOGICFLOW_ENGINE_INSTANCES, instances);
43
55
  }
44
56
  Recorder.prototype.setMaxRecorderNumber = function (maxRecorder) {
45
57
  this.maxRecorder = maxRecorder;
@@ -93,30 +105,33 @@ var Recorder = /** @class */ (function () {
93
105
  return __awaiter(this, void 0, void 0, function () {
94
106
  var instances;
95
107
  return __generator(this, function (_a) {
96
- instances = storage.getItem(LOGICFLOW_ENGINE_INSTANCES) || [];
108
+ instances = storage.getItem(this.instanceId) || [];
97
109
  return [2 /*return*/, instances];
98
110
  });
99
111
  });
100
112
  };
101
113
  Recorder.prototype.clear = function () {
102
- var instances = storage.getItem(LOGICFLOW_ENGINE_INSTANCES) || [];
103
- instances.forEach(function (executionId) {
114
+ this.clearInstance(this.instanceId);
115
+ };
116
+ Recorder.prototype.clearInstance = function (instanceId) {
117
+ var instanceExecutions = storage.getItem(instanceId) || [];
118
+ instanceExecutions.forEach(function (executionId) {
104
119
  storage.removeItem(executionId);
105
120
  var instanceData = storage.getItem(executionId) || [];
106
121
  instanceData.forEach(function (actionId) {
107
122
  storage.removeItem(actionId);
108
123
  });
109
124
  });
110
- storage.removeItem(LOGICFLOW_ENGINE_INSTANCES);
125
+ storage.removeItem(instanceId);
111
126
  };
112
127
  Recorder.prototype.pushExecution = function (executionId) {
113
- var instance = storage.getItem(LOGICFLOW_ENGINE_INSTANCES) || [];
114
- if (instance.length >= this.maxRecorder) {
115
- var removeItem = instance.shift();
128
+ var instanceExecutions = storage.getItem(this.instanceId) || [];
129
+ if (instanceExecutions.length >= this.maxRecorder) {
130
+ var removeItem = instanceExecutions.shift();
116
131
  this.popExecution(removeItem);
117
132
  }
118
- instance.push(executionId);
119
- storage.setItem(LOGICFLOW_ENGINE_INSTANCES, instance);
133
+ instanceExecutions.push(executionId);
134
+ storage.setItem(this.instanceId, instanceExecutions);
120
135
  };
121
136
  Recorder.prototype.popExecution = function (executionId) {
122
137
  var instanceData = storage.getItem(executionId) || [];