UNPKG

@logicflow/engine

Version:

a process engine for javascript

159 lines 5.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Recorder = exports.TaskNode = exports.StartNode = exports.BaseNode = exports.Engine = void 0; const tslib_1 = require("tslib"); // import { LogicFlow } from '@logicflow/core'; const nodes_1 = require("./nodes"); Object.defineProperty(exports, "BaseNode", { enumerable: true, get: function () { return nodes_1.BaseNode; } }); Object.defineProperty(exports, "StartNode", { enumerable: true, get: function () { return nodes_1.StartNode; } }); Object.defineProperty(exports, "TaskNode", { enumerable: true, get: function () { return nodes_1.TaskNode; } }); const FlowModel_1 = require("./FlowModel"); const recorder_1 = require("./recorder"); Object.defineProperty(exports, "Recorder", { enumerable: true, get: function () { return recorder_1.Recorder; } }); const utils_1 = require("./utils"); class Engine { constructor(options) { this.nodeModelMap = new Map(); this.instanceId = (0, utils_1.createEngineId)(); if (options === null || options === void 0 ? void 0 : options.debug) { this.recorder = new recorder_1.Recorder({ instanceId: this.instanceId, }); } // 默认注册节点 register default nodes this.register({ type: nodes_1.StartNode.nodeTypeName, model: nodes_1.StartNode, }); this.register({ type: nodes_1.TaskNode.nodeTypeName, model: nodes_1.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_1.FlowModel({ nodeModelMap: this.nodeModelMap, recorder: this.recorder, context: this.context, globalData, startNodeType, }); flowModel.load(graphData); this.flowModel = flowModel; return flowModel; } /** * 执行流程,允许多次调用 */ execute(param) { return tslib_1.__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 tslib_1.__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 tslib_1.__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 tslib_1.__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); } } } exports.Engine = Engine; tslib_1.__exportStar(require("./constant"), exports); exports.default = Engine; //# sourceMappingURL=index.js.map