@logicflow/engine 0.0.11-beta.0 → 0.0.12-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.
- package/README.md +5 -2
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/es/EventEmitter.d.ts +34 -4
- package/es/EventEmitter.js +70 -48
- package/es/EventEmitter.js.map +1 -0
- package/es/FlowModel.d.ts +75 -73
- package/es/FlowModel.js +130 -173
- package/es/FlowModel.js.map +1 -0
- package/es/Scheduler.d.ts +50 -34
- package/es/Scheduler.js +134 -209
- package/es/Scheduler.js.map +1 -0
- package/es/constant/{constant.js → index.js} +8 -7
- package/es/constant/index.js.map +1 -0
- package/es/constant/logCode.js +29 -0
- package/es/constant/logCode.js.map +1 -0
- package/es/index.d.ts +135 -38
- package/es/index.js +92 -144
- package/es/index.js.map +1 -0
- package/es/nodes/base.d.ts +108 -0
- package/es/nodes/base.js +151 -0
- package/es/nodes/base.js.map +1 -0
- package/es/nodes/index.d.ts +3 -0
- package/es/nodes/index.js +4 -0
- package/es/nodes/index.js.map +1 -0
- package/es/nodes/{StartNode.d.ts → start.d.ts} +3 -2
- package/es/nodes/start.js +11 -0
- package/es/nodes/start.js.map +1 -0
- package/es/nodes/{TaskNode.d.ts → task.d.ts} +3 -2
- package/es/nodes/task.js +11 -0
- package/es/nodes/task.js.map +1 -0
- package/es/platform/browser/browserVm.d.ts +1 -0
- package/es/platform/browser/browserVm.js +66 -0
- package/es/platform/browser/browserVm.js.map +1 -0
- package/es/platform/browser/index.d.ts +4 -0
- package/es/platform/browser/index.js +23 -0
- package/es/platform/browser/index.js.map +1 -0
- package/es/platform/index.d.ts +1 -0
- package/es/platform/index.js +2 -0
- package/es/platform/index.js.map +1 -0
- package/es/platform/node/index.d.ts +4 -0
- package/es/platform/node/index.js +23 -0
- package/es/platform/node/index.js.map +1 -0
- package/es/platform/node/nodeVm.d.ts +1 -0
- package/es/platform/node/nodeVm.js +9 -0
- package/es/platform/node/nodeVm.js.map +1 -0
- package/es/recorder/index.d.ts +36 -10
- package/es/recorder/index.js +82 -135
- package/es/recorder/index.js.map +1 -0
- package/es/utils/global.d.ts +5 -0
- package/es/utils/global.js +27 -0
- package/es/utils/global.js.map +1 -0
- package/es/utils/id.js +14 -0
- package/es/utils/id.js.map +1 -0
- package/es/utils/index.d.ts +4 -0
- package/es/utils/index.js +5 -0
- package/es/utils/index.js.map +1 -0
- package/es/{util → utils}/storage.js +17 -16
- package/es/utils/storage.js.map +1 -0
- package/lib/EventEmitter.d.ts +37 -0
- package/lib/EventEmitter.js +94 -0
- package/lib/EventEmitter.js.map +1 -0
- package/lib/FlowModel.d.ts +146 -0
- package/lib/FlowModel.js +236 -0
- package/lib/FlowModel.js.map +1 -0
- package/lib/Scheduler.d.ts +78 -0
- package/lib/Scheduler.js +179 -0
- package/lib/Scheduler.js.map +1 -0
- package/lib/constant/index.d.ts +16 -0
- package/{cjs/constant/constant.js → lib/constant/index.js} +4 -3
- package/lib/constant/index.js.map +1 -0
- package/lib/constant/logCode.d.ts +12 -0
- package/{cjs/constant/LogCode.js → lib/constant/logCode.js} +16 -13
- package/lib/constant/logCode.js.map +1 -0
- package/lib/index.d.ts +157 -0
- package/lib/index.js +159 -0
- package/lib/index.js.map +1 -0
- package/lib/nodes/base.d.ts +108 -0
- package/lib/nodes/base.js +154 -0
- package/lib/nodes/base.js.map +1 -0
- package/lib/nodes/index.d.ts +3 -0
- package/lib/nodes/index.js +7 -0
- package/lib/nodes/index.js.map +1 -0
- package/lib/nodes/start.d.ts +6 -0
- package/lib/nodes/start.js +15 -0
- package/lib/nodes/start.js.map +1 -0
- package/lib/nodes/task.d.ts +6 -0
- package/lib/nodes/task.js +15 -0
- package/lib/nodes/task.js.map +1 -0
- package/lib/platform/browser/browserVm.d.ts +1 -0
- package/lib/platform/browser/browserVm.js +70 -0
- package/lib/platform/browser/browserVm.js.map +1 -0
- package/lib/platform/browser/index.d.ts +4 -0
- package/lib/platform/browser/index.js +28 -0
- package/lib/platform/browser/index.js.map +1 -0
- package/lib/platform/index.d.ts +1 -0
- package/lib/platform/index.js +5 -0
- package/lib/platform/index.js.map +1 -0
- package/lib/platform/node/index.d.ts +4 -0
- package/lib/platform/node/index.js +28 -0
- package/lib/platform/node/index.js.map +1 -0
- package/lib/platform/node/nodeVm.d.ts +1 -0
- package/lib/platform/node/nodeVm.js +13 -0
- package/lib/platform/node/nodeVm.js.map +1 -0
- package/lib/recorder/index.d.ts +46 -0
- package/lib/recorder/index.js +117 -0
- package/lib/recorder/index.js.map +1 -0
- package/lib/utils/global.d.ts +5 -0
- package/lib/utils/global.js +31 -0
- package/lib/utils/global.js.map +1 -0
- package/lib/utils/id.d.ts +3 -0
- package/lib/utils/id.js +20 -0
- package/lib/utils/id.js.map +1 -0
- package/lib/utils/index.d.ts +4 -0
- package/lib/utils/index.js +9 -0
- package/lib/utils/index.js.map +1 -0
- package/lib/utils/storage.d.ts +7 -0
- package/{cjs/util → lib/utils}/storage.js +18 -17
- package/lib/utils/storage.js.map +1 -0
- package/package.json +31 -71
- package/src/EventEmitter.ts +103 -0
- package/src/FlowModel.ts +325 -0
- package/src/Scheduler.ts +244 -0
- package/src/constant/index.ts +23 -0
- package/src/constant/logCode.ts +34 -0
- package/src/index.ts +300 -0
- package/src/nodes/base.ts +236 -0
- package/src/nodes/index.ts +3 -0
- package/src/nodes/start.ts +8 -0
- package/src/nodes/task.ts +8 -0
- package/src/platform/browser/browserVm.ts +68 -0
- package/src/platform/browser/index.ts +28 -0
- package/src/platform/index.ts +1 -0
- package/src/platform/node/index.ts +28 -0
- package/src/platform/node/nodeVm.ts +12 -0
- package/src/recorder/index.ts +137 -0
- package/src/typings.d.ts +0 -0
- package/src/utils/global.ts +41 -0
- package/src/utils/id.ts +16 -0
- package/src/utils/index.ts +5 -0
- package/src/utils/storage.ts +55 -0
- package/cjs/EventEmitter.js +0 -70
- package/cjs/FlowModel.js +0 -277
- package/cjs/Scheduler.js +0 -252
- package/cjs/expression/browserVm.js +0 -81
- package/cjs/expression/index.js +0 -57
- package/cjs/index.js +0 -210
- package/cjs/nodes/BaseNode.js +0 -252
- package/cjs/nodes/StartNode.js +0 -27
- package/cjs/nodes/TaskNode.js +0 -27
- package/cjs/recorder/index.js +0 -168
- package/cjs/util/ID.js +0 -16
- package/cjs/util/global.js +0 -32
- package/es/constant/LogCode.js +0 -28
- package/es/expression/browserVm.d.ts +0 -4
- package/es/expression/browserVm.js +0 -76
- package/es/expression/index.d.ts +0 -2
- package/es/expression/index.js +0 -54
- package/es/nodes/BaseNode.d.ts +0 -110
- package/es/nodes/BaseNode.js +0 -250
- package/es/nodes/StartNode.js +0 -25
- package/es/nodes/TaskNode.js +0 -25
- package/es/util/ID.js +0 -13
- package/es/util/global.d.ts +0 -5
- package/es/util/global.js +0 -26
- package/lib/main.js +0 -1
- /package/es/constant/{constant.d.ts → index.d.ts} +0 -0
- /package/es/constant/{LogCode.d.ts → logCode.d.ts} +0 -0
- /package/es/{util/ID.d.ts → utils/id.d.ts} +0 -0
- /package/es/{util → utils}/storage.d.ts +0 -0
package/src/FlowModel.ts
ADDED
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
import { Engine } from '.'
|
|
2
|
+
import { Recorder } from './recorder'
|
|
3
|
+
import { Scheduler } from './Scheduler'
|
|
4
|
+
import { BaseNode } from './nodes'
|
|
5
|
+
import { createExecId } from './utils'
|
|
6
|
+
import {
|
|
7
|
+
EVENT_INSTANCE_COMPLETE,
|
|
8
|
+
EVENT_INSTANCE_INTERRUPTED,
|
|
9
|
+
EVENT_INSTANCE_ERROR,
|
|
10
|
+
} from './constant'
|
|
11
|
+
import { ErrorCode, getErrorMsg } from './constant/logCode'
|
|
12
|
+
|
|
13
|
+
export class FlowModel {
|
|
14
|
+
/**
|
|
15
|
+
* 流程支持的节点类型.
|
|
16
|
+
*/
|
|
17
|
+
nodeModelMap: Map<string, BaseNode.NodeConstructor>
|
|
18
|
+
/**
|
|
19
|
+
* 调度器,用于调度节点执行
|
|
20
|
+
*/
|
|
21
|
+
scheduler: Scheduler
|
|
22
|
+
/**
|
|
23
|
+
* 待执行的队列,当流程正在执行时,如果再次触发执行。那么会将执行参数放到队列中,等待上一次执行完成后再执行。
|
|
24
|
+
*/
|
|
25
|
+
executeList: Partial<FlowModel.ExecParam>[]
|
|
26
|
+
/**
|
|
27
|
+
* 当前正在执行的任务。当监听到调度器执行完成时,触发执行参数中的回调,告知外部执行完成。
|
|
28
|
+
*/
|
|
29
|
+
executingInstance?: FlowModel.ExecParam | null
|
|
30
|
+
/**
|
|
31
|
+
* 当前流程模型中的所有节点,边会被转换成节点的 incoming 和 outgoing 属性
|
|
32
|
+
*/
|
|
33
|
+
nodeConfigMap: Map<Engine.Key, BaseNode.NodeConfig> = new Map()
|
|
34
|
+
/**
|
|
35
|
+
* 当流程正在执行时,如果再次触发执行。那么会将执行参数放入到队列中,等待上一次执行完成后再执行。
|
|
36
|
+
*/
|
|
37
|
+
isRunning: boolean
|
|
38
|
+
/**
|
|
39
|
+
* 开始接地那类型,在执行流程时,会从这些节点开始执行
|
|
40
|
+
*/
|
|
41
|
+
startNodeType: string
|
|
42
|
+
/**
|
|
43
|
+
* 当前流程中开始节点组成的数组
|
|
44
|
+
*/
|
|
45
|
+
startNodes: BaseNode.NodeConfig[] = []
|
|
46
|
+
/**
|
|
47
|
+
* 用于存储全局数据,最终会传递给每个节点
|
|
48
|
+
*/
|
|
49
|
+
globalData: Record<string, unknown> = {}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* 外部传入的上下文,最终会传递给每个节点
|
|
53
|
+
* 例如:
|
|
54
|
+
* const context = {
|
|
55
|
+
* request: {
|
|
56
|
+
* get: (url) => {
|
|
57
|
+
* return fetch(url);
|
|
58
|
+
* },
|
|
59
|
+
* },
|
|
60
|
+
* }
|
|
61
|
+
* 在节点内可以通过 this.context.request.get(url) 来调用。
|
|
62
|
+
*/
|
|
63
|
+
context: Record<string, unknown>
|
|
64
|
+
|
|
65
|
+
constructor({
|
|
66
|
+
nodeModelMap,
|
|
67
|
+
recorder,
|
|
68
|
+
context = {},
|
|
69
|
+
globalData = {},
|
|
70
|
+
startNodeType = 'StartNode',
|
|
71
|
+
}: FlowModel.IFlowModelProps) {
|
|
72
|
+
// 流程包含的节点类型 ??? 在 load 代码中又初始化了一遍,为什么要传进来
|
|
73
|
+
this.nodeModelMap = nodeModelMap
|
|
74
|
+
// 需要执行的队列
|
|
75
|
+
this.executeList = []
|
|
76
|
+
// 执行中的任务
|
|
77
|
+
this.executingInstance = null
|
|
78
|
+
// 外部传入的上下文,最终会传递给每个节点
|
|
79
|
+
this.context = context
|
|
80
|
+
// 用于存储全局数据,可以在流程中共享
|
|
81
|
+
this.globalData = globalData
|
|
82
|
+
// 开始节点类型,在执行流程时,会从这些节点开始执行
|
|
83
|
+
this.startNodeType = startNodeType
|
|
84
|
+
this.isRunning = false
|
|
85
|
+
this.scheduler = new Scheduler({
|
|
86
|
+
flowModel: this,
|
|
87
|
+
recorder,
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
this.scheduler.on(EVENT_INSTANCE_COMPLETE, (result) => {
|
|
91
|
+
this.onExecuteFinished(result)
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
this.scheduler.on(EVENT_INSTANCE_INTERRUPTED, (result) => {
|
|
95
|
+
this.onExecuteFinished(result)
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
this.scheduler.on(EVENT_INSTANCE_ERROR, (result) => {
|
|
99
|
+
this.onExecuteFinished(result)
|
|
100
|
+
})
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* 解析LogicFlow图数据,将nodes和edges转换成节点格式。
|
|
104
|
+
* 例如:
|
|
105
|
+
* graphData: {
|
|
106
|
+
* nodes: [
|
|
107
|
+
* { id: 'node1', type: 'StartNode', properties: {} },
|
|
108
|
+
* { id: 'node2', type: 'TaskNode', properties: {} },
|
|
109
|
+
* ],
|
|
110
|
+
* edges: [
|
|
111
|
+
* { id: 'edge1', sourceNodeId: 'node1', targetNodeId: 'node2', properties: {} },
|
|
112
|
+
* ]
|
|
113
|
+
* }
|
|
114
|
+
* 转换成:
|
|
115
|
+
* nodeConfigMap: {
|
|
116
|
+
* node1: {
|
|
117
|
+
* id: 'node1',
|
|
118
|
+
* type: 'StartNode',
|
|
119
|
+
* properties: {},
|
|
120
|
+
* incoming: [],
|
|
121
|
+
* outgoing: [{ id: 'edge1', properties: {}, target: 'node2' }]
|
|
122
|
+
* },
|
|
123
|
+
* node2: {
|
|
124
|
+
* id: 'node2',
|
|
125
|
+
* type: 'TaskNode',
|
|
126
|
+
* properties: {},
|
|
127
|
+
* incoming: [{ id: 'edge1', properties: {}, source: 'node1' }],
|
|
128
|
+
* outgoing: [],
|
|
129
|
+
* }
|
|
130
|
+
* }
|
|
131
|
+
* 此格式方便后续执行时,根据节点id快速找到节点和执行初始化节点模型。
|
|
132
|
+
* 同时此方法还会找到所有的开始节点,方便后续执行时,从开始节点开始执行。
|
|
133
|
+
* @param graphData 流程图数据
|
|
134
|
+
*/
|
|
135
|
+
public load(graphData: Engine.GraphConfigData) {
|
|
136
|
+
const { nodes = [], edges = [] } = graphData
|
|
137
|
+
nodes.forEach((node) => {
|
|
138
|
+
if (this.nodeModelMap.has(node.type)) {
|
|
139
|
+
const nodeConfig: BaseNode.NodeConfig = {
|
|
140
|
+
id: node.id,
|
|
141
|
+
type: node.type,
|
|
142
|
+
properties: node.properties,
|
|
143
|
+
incoming: [],
|
|
144
|
+
outgoing: [],
|
|
145
|
+
}
|
|
146
|
+
this.nodeConfigMap.set(node.id, nodeConfig)
|
|
147
|
+
if (node.type === this.startNodeType) {
|
|
148
|
+
this.startNodes.push(nodeConfig)
|
|
149
|
+
}
|
|
150
|
+
} else {
|
|
151
|
+
console.warn(`未识别的节点类型:${node.type}`)
|
|
152
|
+
}
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
edges.forEach((edge) => {
|
|
156
|
+
const sourceNode = this.nodeConfigMap.get(edge.sourceNodeId)
|
|
157
|
+
const targetNode = this.nodeConfigMap.get(edge.targetNodeId)
|
|
158
|
+
if (sourceNode) {
|
|
159
|
+
sourceNode.outgoing.push({
|
|
160
|
+
id: edge.id,
|
|
161
|
+
properties: edge.properties,
|
|
162
|
+
target: edge.targetNodeId,
|
|
163
|
+
})
|
|
164
|
+
}
|
|
165
|
+
if (targetNode && targetNode.type !== this.startNodeType) {
|
|
166
|
+
targetNode.incoming.push({
|
|
167
|
+
id: edge.id,
|
|
168
|
+
properties: edge.properties,
|
|
169
|
+
source: edge.sourceNodeId,
|
|
170
|
+
})
|
|
171
|
+
}
|
|
172
|
+
})
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* 从待执行队列中取出需要执行的内容。
|
|
177
|
+
* 会依次判断是否有 actionId、nodeId、executionId。
|
|
178
|
+
* 若存在 actionId,那么表示恢复执行
|
|
179
|
+
* 若存在 nodeId,那么表示从指定节点开始执行
|
|
180
|
+
* 若都不存在,那么新建一个 executionId,从开始节点开始执行
|
|
181
|
+
* @private
|
|
182
|
+
*/
|
|
183
|
+
private createExecution(execParam: Partial<FlowModel.ExecParam>) {
|
|
184
|
+
this.executeList.push(execParam)
|
|
185
|
+
|
|
186
|
+
// 如果有 actionId,则表示恢复执行
|
|
187
|
+
// TODO: 待测试,确认该流程
|
|
188
|
+
if (execParam.actionId && execParam.nodeId && execParam.executionId) {
|
|
189
|
+
this.scheduler.resume({
|
|
190
|
+
executionId: execParam.executionId,
|
|
191
|
+
actionId: execParam.actionId,
|
|
192
|
+
nodeId: execParam.nodeId,
|
|
193
|
+
data: execParam.data,
|
|
194
|
+
})
|
|
195
|
+
return
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// 否则,判断 executionId 是否存在,使用 executionId 或创建新的 execution,从开始节点开始执行
|
|
199
|
+
// const executionId = execParam?.executionId || createExecId()
|
|
200
|
+
const executionId = createExecId()
|
|
201
|
+
execParam.executionId = executionId
|
|
202
|
+
|
|
203
|
+
// 当指定了具体需要执行的节点时,执行下面方法
|
|
204
|
+
if (execParam?.nodeId) {
|
|
205
|
+
const nodeConfig = this.nodeConfigMap.get(execParam.nodeId)
|
|
206
|
+
if (!nodeConfig) {
|
|
207
|
+
execParam?.onError?.(
|
|
208
|
+
new Error(
|
|
209
|
+
`${getErrorMsg(ErrorCode.NONE_NODE_ID)}(${execParam.nodeId})`,
|
|
210
|
+
),
|
|
211
|
+
)
|
|
212
|
+
return
|
|
213
|
+
}
|
|
214
|
+
// 当指定了开始节点,且该节点存在,则直接以这个节点开始执行
|
|
215
|
+
this.startNodes = [nodeConfig]
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
this.startNodes.forEach((startNode) => {
|
|
219
|
+
this.scheduler.addAction({
|
|
220
|
+
executionId,
|
|
221
|
+
nodeId: startNode.id,
|
|
222
|
+
})
|
|
223
|
+
})
|
|
224
|
+
// 所有的开始节点都执行
|
|
225
|
+
this.scheduler.run({
|
|
226
|
+
executionId,
|
|
227
|
+
})
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* 执行流程,每次执行都会生成一个唯一的 executionId,用于区分不同的执行。
|
|
232
|
+
* 同一次执行,这次执行内部的节点执行顺序为并行。内部并行是为了避免异步节点阻塞其他节点的执行
|
|
233
|
+
* 多次执行,多次执行之间为串行,这里选择串行的原因是避免多次执行之间的数据冲突。
|
|
234
|
+
* 例如:
|
|
235
|
+
* 一个流程存在两个开始节点,A 和 B,A 和 B 的下一个节点都是 C,C 的下两个节点是 D 和 E
|
|
236
|
+
* 外部分别触发了 A 和 B 的执行,那么 A 和 B 的执行是串行(即 A 执行完再执行 B),但是 D 和 E 的执行是并行的。
|
|
237
|
+
* 如果希望 A 和 B 的执行时并行的,就不能使用同一个流程模型执行,应该初始化两个。
|
|
238
|
+
* 下面直接使用调度器的队列
|
|
239
|
+
* @param params
|
|
240
|
+
*/
|
|
241
|
+
public async execute(params: Partial<FlowModel.ExecParam>) {
|
|
242
|
+
this.createExecution(params)
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
public async resume(params: Partial<FlowModel.ExecParam>) {
|
|
246
|
+
this.createExecution(params)
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* 创建节点实例,每个节点实例都会有一个唯一的 actionId
|
|
251
|
+
* 通过 executionId, nodeId, actionId 可以唯一确定一个节点的某一次执行
|
|
252
|
+
* @param nodeId
|
|
253
|
+
* @returns 节点实例
|
|
254
|
+
*/
|
|
255
|
+
// TODO: 确认下面这种场景,类型如何定义
|
|
256
|
+
public createAction(nodeId: Engine.Key) {
|
|
257
|
+
const nodeConfig = this.nodeConfigMap.get(nodeId)
|
|
258
|
+
if (nodeConfig) {
|
|
259
|
+
const NodeModel = this.nodeModelMap.get(nodeConfig.type)
|
|
260
|
+
if (!NodeModel) {
|
|
261
|
+
throw new Error('该 NodeModel 不存在,抛出异常')
|
|
262
|
+
}
|
|
263
|
+
return new NodeModel({
|
|
264
|
+
nodeConfig,
|
|
265
|
+
globalData: this.globalData,
|
|
266
|
+
context: this.context,
|
|
267
|
+
})
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
public setStartNodeType(type: string) {
|
|
272
|
+
this.startNodeType = type
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
public updateGlobalData(data: Record<string, unknown>) {
|
|
276
|
+
// TODO: 数据的合并,是否考虑子项的合并(默认值的替换)
|
|
277
|
+
this.globalData = {
|
|
278
|
+
...this.globalData,
|
|
279
|
+
...data,
|
|
280
|
+
}
|
|
281
|
+
return this.globalData
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* 在执行完成后,通知外部此次之行完成
|
|
286
|
+
* 如果还存在待执行的任务,那么继续执行
|
|
287
|
+
* @param result
|
|
288
|
+
* @private
|
|
289
|
+
*/
|
|
290
|
+
private onExecuteFinished(result) {
|
|
291
|
+
const index = this.executeList.findIndex(
|
|
292
|
+
(i) => i.executionId === result.executionId,
|
|
293
|
+
)
|
|
294
|
+
if (index > -1) {
|
|
295
|
+
const { callback } = this.executeList[index]
|
|
296
|
+
this.executeList.splice(index, 1)
|
|
297
|
+
callback?.(result)
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
export namespace FlowModel {
|
|
303
|
+
export type FlowResult =
|
|
304
|
+
| ({
|
|
305
|
+
result?: Record<string, unknown>
|
|
306
|
+
} & Engine.ActionParam)
|
|
307
|
+
| Engine.NextActionParam
|
|
308
|
+
|
|
309
|
+
export type ActionParam = Scheduler.ActionParam
|
|
310
|
+
|
|
311
|
+
export type ExecParam = {
|
|
312
|
+
callback?: (result: Engine.NextActionParam) => void
|
|
313
|
+
onError?: (error: Error) => void
|
|
314
|
+
} & ActionParam
|
|
315
|
+
|
|
316
|
+
export interface IFlowModelProps {
|
|
317
|
+
nodeModelMap: Map<string, BaseNode.NodeConstructor>
|
|
318
|
+
recorder?: Recorder
|
|
319
|
+
context?: Record<string, unknown>
|
|
320
|
+
globalData?: Record<string, unknown>
|
|
321
|
+
startNodeType?: string
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
export default FlowModel
|
package/src/Scheduler.ts
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import { Engine } from '.'
|
|
2
|
+
import Recorder from './recorder'
|
|
3
|
+
import FlowModel from './FlowModel'
|
|
4
|
+
import EventEmitter from './EventEmitter'
|
|
5
|
+
import { createActionId } from './utils'
|
|
6
|
+
import {
|
|
7
|
+
EVENT_INSTANCE_ERROR,
|
|
8
|
+
EVENT_INSTANCE_COMPLETE,
|
|
9
|
+
EVENT_INSTANCE_INTERRUPTED,
|
|
10
|
+
FlowStatus,
|
|
11
|
+
} from './constant'
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 调度器
|
|
15
|
+
* 通过一个队列维护需要执行的节点,一个集合维护正在执行的节点
|
|
16
|
+
*/
|
|
17
|
+
export class Scheduler extends EventEmitter {
|
|
18
|
+
/**
|
|
19
|
+
* 当前需要执行的节点队列
|
|
20
|
+
*/
|
|
21
|
+
nodeQueueMap: Map<Engine.Key, Engine.NodeParam[]>
|
|
22
|
+
/**
|
|
23
|
+
* 当前正在执行的节点集合
|
|
24
|
+
* 在每个节点执行完成后,会从集合中删除
|
|
25
|
+
* 同时会判断次集合中是否还存在和此节点相同的 executionId,如果不存在,说明该流程已经执行完成
|
|
26
|
+
*/
|
|
27
|
+
actionRunningMap: Map<Engine.Key, Scheduler.ActionParamMap>
|
|
28
|
+
/**
|
|
29
|
+
* 流程模型,用于创建节点模型
|
|
30
|
+
*/
|
|
31
|
+
flowModel: FlowModel
|
|
32
|
+
/**
|
|
33
|
+
* 执行记录存储器
|
|
34
|
+
* 用于存储节点执行的结果
|
|
35
|
+
*/
|
|
36
|
+
recorder?: Recorder
|
|
37
|
+
|
|
38
|
+
constructor(config: Scheduler.ISchedulerProps) {
|
|
39
|
+
super()
|
|
40
|
+
this.nodeQueueMap = new Map()
|
|
41
|
+
this.actionRunningMap = new Map()
|
|
42
|
+
this.flowModel = config.flowModel
|
|
43
|
+
this.recorder = config.recorder
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 添加一个任务到队列中。
|
|
48
|
+
* 1. 由流程模型将所有的开始及诶带你添加到队列中
|
|
49
|
+
* 2. 当一个节点执行完成后,将后续的节点添加到队列中
|
|
50
|
+
* @param nodeParam
|
|
51
|
+
*/
|
|
52
|
+
public addAction(nodeParam: Engine.NodeParam) {
|
|
53
|
+
const { executionId } = nodeParam
|
|
54
|
+
if (!this.nodeQueueMap.has(executionId)) {
|
|
55
|
+
this.nodeQueueMap.set(executionId, [])
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const currentActionQueue: Engine.NodeParam[] | undefined =
|
|
59
|
+
this.nodeQueueMap.get(executionId)
|
|
60
|
+
if (currentActionQueue) {
|
|
61
|
+
currentActionQueue.push(nodeParam)
|
|
62
|
+
}
|
|
63
|
+
console.log('this.nodeQueueMap--->>>', this.nodeQueueMap)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
private pushActionToRunningMap(actionParam: Scheduler.ActionParam) {
|
|
67
|
+
const { executionId, actionId } = actionParam
|
|
68
|
+
if (!this.actionRunningMap.has(executionId)) {
|
|
69
|
+
const runningMap: Scheduler.ActionParamMap = new Map()
|
|
70
|
+
this.actionRunningMap.set(executionId, runningMap)
|
|
71
|
+
}
|
|
72
|
+
if (actionId) {
|
|
73
|
+
this.actionRunningMap.get(executionId)?.set(actionId, actionParam)
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
private removeActionFromRunningMap(actionParam: Engine.ActionParam) {
|
|
78
|
+
const { executionId, actionId } = actionParam
|
|
79
|
+
if (!actionId) return
|
|
80
|
+
|
|
81
|
+
const runningMap = this.actionRunningMap.get(executionId)
|
|
82
|
+
if (!runningMap) return
|
|
83
|
+
|
|
84
|
+
runningMap.delete(actionId)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* 为了防止多次添加导致
|
|
89
|
+
* @param actionParam
|
|
90
|
+
*/
|
|
91
|
+
private saveActionResult(actionParam: Engine.NextActionParam) {
|
|
92
|
+
this.recorder?.addActionRecord({
|
|
93
|
+
timestamp: Date.now(),
|
|
94
|
+
...actionParam,
|
|
95
|
+
})
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
private hasRunningAction(executionId: Engine.Key): boolean {
|
|
99
|
+
const runningMap = this.actionRunningMap.get(executionId)
|
|
100
|
+
if (!runningMap) return false
|
|
101
|
+
if (runningMap.size === 0) {
|
|
102
|
+
this.actionRunningMap.delete(executionId)
|
|
103
|
+
return false
|
|
104
|
+
}
|
|
105
|
+
return true
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* 调度器执行下一个任务
|
|
110
|
+
* 1. 提供给流程模型,用户开始执行第一个任务
|
|
111
|
+
* 2. 内部任务执行完成后,调用此方法继续执行下一个任务
|
|
112
|
+
* 3. 当判断没有可以继续执行的任务后,触发流程结束事件
|
|
113
|
+
* @param runParam
|
|
114
|
+
*/
|
|
115
|
+
public run(runParam: Scheduler.ActionParam) {
|
|
116
|
+
const nodeQueue: Engine.NodeParam[] | undefined = this.nodeQueueMap.get(
|
|
117
|
+
runParam.executionId,
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
// 将同一个 executionId 当前待执行的节点一起执行
|
|
121
|
+
// 避免出现某一个节点执行时间过长,导致其他节点等待时间过长
|
|
122
|
+
while (nodeQueue?.length) {
|
|
123
|
+
const currentNode = nodeQueue.pop()
|
|
124
|
+
const actionId = createActionId()
|
|
125
|
+
const actionParam: Engine.ActionParam = {
|
|
126
|
+
...(currentNode as Engine.NodeParam),
|
|
127
|
+
actionId,
|
|
128
|
+
}
|
|
129
|
+
this.pushActionToRunningMap(actionParam)
|
|
130
|
+
this.exec(actionParam)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (!this.hasRunningAction(runParam.executionId)) {
|
|
134
|
+
// 当一个流程在 nodeQueueMap 和 actionRunningMap 中都不存在执行的节点时,说明这个流程已经执行完成。
|
|
135
|
+
this.emit(EVENT_INSTANCE_COMPLETE, {
|
|
136
|
+
...runParam,
|
|
137
|
+
status: FlowStatus.COMPLETED,
|
|
138
|
+
})
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
private next(data: Engine.NextActionParam) {
|
|
143
|
+
if (data.outgoing && data.outgoing.length > 0) {
|
|
144
|
+
data.outgoing.forEach((item) => {
|
|
145
|
+
if (item.result) {
|
|
146
|
+
this.addAction({
|
|
147
|
+
executionId: data.executionId,
|
|
148
|
+
nodeId: item.target,
|
|
149
|
+
})
|
|
150
|
+
}
|
|
151
|
+
})
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
this.saveActionResult(data)
|
|
155
|
+
this.removeActionFromRunningMap(data)
|
|
156
|
+
this.run(data)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* 恢复某个任务的执行
|
|
161
|
+
* 可以自定义节点手动实现流程中断,然后通过此方法恢复流程的执行
|
|
162
|
+
* @param resumeParam
|
|
163
|
+
*/
|
|
164
|
+
public async resume(resumeParam: Engine.ResumeParam) {
|
|
165
|
+
const { executionId, actionId, nodeId } = resumeParam
|
|
166
|
+
this.pushActionToRunningMap({
|
|
167
|
+
executionId,
|
|
168
|
+
actionId,
|
|
169
|
+
nodeId,
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
const model = this.flowModel.createAction(nodeId)
|
|
173
|
+
await model?.resume({
|
|
174
|
+
...resumeParam,
|
|
175
|
+
next: this.next.bind(this),
|
|
176
|
+
})
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// 中断时,触发事件
|
|
180
|
+
private interrupted(execResult: Engine.NextActionParam) {
|
|
181
|
+
this.emit(EVENT_INSTANCE_INTERRUPTED, execResult)
|
|
182
|
+
}
|
|
183
|
+
// 报错时,触发事件
|
|
184
|
+
private error(execResult: Engine.NextActionParam) {
|
|
185
|
+
this.emit(EVENT_INSTANCE_ERROR, execResult)
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
private async exec(actionParam: Engine.ActionParam) {
|
|
189
|
+
const { executionId, actionId, nodeId } = actionParam
|
|
190
|
+
const model = this.flowModel.createAction(nodeId)
|
|
191
|
+
const execResult = await model?.execute({
|
|
192
|
+
executionId,
|
|
193
|
+
actionId,
|
|
194
|
+
nodeId,
|
|
195
|
+
next: this.next.bind(this),
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
if (execResult) {
|
|
199
|
+
const { nodeType, properties, outgoing, status, detail } = execResult
|
|
200
|
+
const actionResult: Engine.NextActionParam = {
|
|
201
|
+
// actionParam
|
|
202
|
+
executionId,
|
|
203
|
+
actionId,
|
|
204
|
+
nodeId,
|
|
205
|
+
// execResult
|
|
206
|
+
nodeType,
|
|
207
|
+
properties,
|
|
208
|
+
outgoing,
|
|
209
|
+
status,
|
|
210
|
+
detail,
|
|
211
|
+
}
|
|
212
|
+
if (execResult?.status === FlowStatus.INTERRUPTED) {
|
|
213
|
+
this.interrupted(execResult)
|
|
214
|
+
this.saveActionResult(actionResult)
|
|
215
|
+
this.removeActionFromRunningMap(actionParam)
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (execResult?.status === FlowStatus.ERROR) {
|
|
219
|
+
this.error(execResult)
|
|
220
|
+
this.saveActionResult(actionResult)
|
|
221
|
+
this.removeActionFromRunningMap(actionParam)
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
// TODO: 考虑停下所有的任务
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export namespace Scheduler {
|
|
229
|
+
export type ActionParam = {
|
|
230
|
+
executionId: Engine.Key
|
|
231
|
+
actionId?: Engine.Key
|
|
232
|
+
nodeId?: Engine.Key
|
|
233
|
+
data?: Record<string, unknown>
|
|
234
|
+
[key: string]: unknown
|
|
235
|
+
}
|
|
236
|
+
export type ActionParamMap = Map<Engine.Key, ActionParam>
|
|
237
|
+
|
|
238
|
+
export interface ISchedulerProps {
|
|
239
|
+
flowModel: FlowModel
|
|
240
|
+
recorder?: Recorder
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export default Scheduler
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// baseType
|
|
2
|
+
export const BASE_START_NODE = 'start'
|
|
3
|
+
|
|
4
|
+
// eventType
|
|
5
|
+
export const EVENT_INSTANCE_COMPLETE = 'instance:complete'
|
|
6
|
+
export const EVENT_INSTANCE_INTERRUPTED = 'instance:interrupted'
|
|
7
|
+
export const EVENT_INSTANCE_ERROR = 'instance:error'
|
|
8
|
+
|
|
9
|
+
// flowStatus
|
|
10
|
+
export enum FlowStatus {
|
|
11
|
+
COMPLETED = 'completed',
|
|
12
|
+
INTERRUPTED = 'interrupted',
|
|
13
|
+
RUNNING = 'running',
|
|
14
|
+
PENDING = 'pending',
|
|
15
|
+
ERROR = 'error',
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// actionStatus
|
|
19
|
+
export enum ActionStatus {
|
|
20
|
+
SUCCESS = 'success',
|
|
21
|
+
ERROR = 'error',
|
|
22
|
+
INTERRUPTED = 'interrupted',
|
|
23
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export enum ErrorCode {
|
|
2
|
+
// 模型数据错误
|
|
3
|
+
NONE_START_NODE = 1000,
|
|
4
|
+
NONE_NODE_ID = 1001,
|
|
5
|
+
|
|
6
|
+
// 表达式错误
|
|
7
|
+
NO_DOCUMENT_BODY = 2001,
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export enum WarningCode {
|
|
11
|
+
NONE_START_NODE_IN_DATA = 2000,
|
|
12
|
+
START_NODE_INCOMING = 2001,
|
|
13
|
+
|
|
14
|
+
// 表达式判断异常
|
|
15
|
+
EXPRESSION_EXEC_ERROR = 3000,
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// TODO: 感觉这块可以用个国际化插件,这样配置不同语言加文件就可以了。
|
|
19
|
+
const errorMsgMapCn = {
|
|
20
|
+
[ErrorCode.NONE_START_NODE]: '未找到入度为0的节点',
|
|
21
|
+
[ErrorCode.NONE_NODE_ID]: '流程数据中存在没有此节点',
|
|
22
|
+
[ErrorCode.NO_DOCUMENT_BODY]: '找不到document.body, 请在DOM加载完成后再执行',
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const warningMsgMapCn = {
|
|
26
|
+
[WarningCode.NONE_START_NODE_IN_DATA]: '初始化数据中未找到入度为0的节点',
|
|
27
|
+
[WarningCode.START_NODE_INCOMING]: '开始节点不允许被连入',
|
|
28
|
+
[WarningCode.EXPRESSION_EXEC_ERROR]: '表达式执行异常',
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const getErrorMsg = (code: ErrorCode) =>
|
|
32
|
+
`error[${code}]: ${errorMsgMapCn[code]}`
|
|
33
|
+
export const getWarningMsg = (code: WarningCode) =>
|
|
34
|
+
`warning[${code}]: ${warningMsgMapCn[code]}`
|