@logicflow/engine
Version:
a process engine for javascript
152 lines • 5.24 kB
JavaScript
import { __awaiter } from "tslib";
// import { LogicFlow } from '@logicflow/core';
import { BaseNode, StartNode, TaskNode } from './nodes';
import { FlowModel } from './FlowModel';
import { Recorder } from './recorder';
import { createEngineId } from './utils';
export class Engine {
constructor(options) {
this.nodeModelMap = new Map();
this.instanceId = createEngineId();
if (options === null || options === void 0 ? void 0 : options.debug) {
this.recorder = new Recorder({
instanceId: this.instanceId,
});
}
// 默认注册节点 register default nodes
this.register({
type: StartNode.nodeTypeName,
model: StartNode,
});
this.register({
type: TaskNode.nodeTypeName,
model: TaskNode,
});
this.context = (options === null || options === void 0 ? void 0 : options.context) || {};
}
/**
* 注册节点
* @param nodeConfig { type: 'custom-node', model: NodeClass }
*/
register(nodeConfig) {
this.nodeModelMap.set(nodeConfig.type, nodeConfig.model);
}
/**
* 自定义执行记录的存储,默认浏览器使用 sessionStorage, nodejs 使用内存存储
* 注意:由于执行记录不全会主动删除,所以需要自行清理。
* nodejs 环境建议自定义为持久化存储。
* engine.setCustomRecorder({{
* async addActionRecord(task) {}
* async getTask(actionId) {}
* async getExecutionTasks(executionId) {}
* clear(instanceId) {}
* }}
* @param recorder
*/
setCustomRecorder(recorder) {
this.recorder = recorder;
}
/**
* 加载流程图数据
*/
load({ graphData, startNodeType = 'StartNode', globalData = {}, }) {
this.graphData = graphData;
const flowModel = new FlowModel({
nodeModelMap: this.nodeModelMap,
recorder: this.recorder,
context: this.context,
globalData,
startNodeType,
});
flowModel.load(graphData);
this.flowModel = flowModel;
return flowModel;
}
/**
* 执行流程,允许多次调用
*/
execute(param) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
var _a;
let execParam = param;
if (!param) {
execParam = {};
}
(_a = this.flowModel) === null || _a === void 0 ? void 0 : _a.execute(Object.assign(Object.assign({}, execParam), { callback: (result) => {
resolve(result);
}, onError: (error) => {
reject(error);
} }));
});
});
}
/**
* 中断流程恢复
* @param resumeParam
* @returns
*/
resume(resumeParam) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
var _a;
(_a = this.flowModel) === null || _a === void 0 ? void 0 : _a.resume(Object.assign(Object.assign({}, resumeParam), { callback: (result) => {
resolve(result);
}, onError: (error) => {
reject(error);
} }));
});
});
}
getExecutionList() {
return __awaiter(this, void 0, void 0, function* () {
var _a;
return yield ((_a = this.recorder) === null || _a === void 0 ? void 0 : _a.getExecutionList());
});
}
/**
* 获取执行任务记录
* @param executionId
* @returns
*/
getExecutionRecord(executionId) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
const actions = yield ((_a = this.recorder) === null || _a === void 0 ? void 0 : _a.getExecutionActions(executionId));
if (!actions) {
return null;
}
// DONE: 确认 records 的类型
const records = [];
for (let i = 0; i < (actions === null || actions === void 0 ? void 0 : actions.length); i++) {
const action = actions[i];
if (this.recorder) {
records.push((_b = this.recorder) === null || _b === void 0 ? void 0 : _b.getActionRecord(action));
}
}
return Promise.all(records);
});
}
destroy() {
var _a;
(_a = this.recorder) === null || _a === void 0 ? void 0 : _a.clear();
}
getGlobalData() {
var _a;
return (_a = this.flowModel) === null || _a === void 0 ? void 0 : _a.globalData;
}
setGlobalData(data) {
if (this.flowModel) {
this.flowModel.globalData = data;
}
}
updateGlobalData(data) {
if (this.flowModel) {
Object.assign(this.flowModel.globalData, data);
}
}
}
export * from './constant';
export { BaseNode, StartNode, TaskNode, Recorder };
export default Engine;
//# sourceMappingURL=index.js.map