@logicflow/engine
Version:
a process engine for javascript
113 lines • 3.82 kB
JavaScript
import { __awaiter } from "tslib";
import { storage } from '../utils';
export const MAX_RECORDER = 100;
export const MAX_INSTANCE = 100;
export const LOGICFLOW_ENGINE_INSTANCES = 'LOGICFLOW_ENGINE_INSTANCES';
export class Recorder {
constructor({ instanceId }) {
this.instanceId = instanceId;
this.maxRecorder = MAX_RECORDER;
const instances = this.getItem(LOGICFLOW_ENGINE_INSTANCES) || [];
if (instances.indexOf(instanceId) === -1) {
instances.push(instanceId);
}
if (instances.length > MAX_INSTANCE) {
const clearInstance = instances.shift();
this.clearInstance(clearInstance);
}
this.setItem(LOGICFLOW_ENGINE_INSTANCES, instances);
}
setMaxRecorderNumber(max) {
this.maxRecorder = max;
}
// 将存储 storage 的方法收敛到此处,并在此处做异常处理 - setItem
setItem(key, value) {
try {
storage.setItem(key, value);
}
catch (error) {
console.error('Ops, something wrong with storage.setItem !!!');
storage.clear();
storage.setItem(key, value);
}
}
// getItem 方法
getItem(key) {
return storage.getItem(key);
}
getExecutionActions(executionId) {
return __awaiter(this, void 0, void 0, function* () {
return this.getItem(executionId);
});
}
getExecutionList() {
return __awaiter(this, void 0, void 0, function* () {
return this.getItem(this.instanceId) || [];
});
}
addExecution(executionId) {
const instanceExecutions = this.getItem(this.instanceId) || [];
if (instanceExecutions.length >= this.maxRecorder) {
const toBeRemovedItem = instanceExecutions.shift();
this.popExecution(toBeRemovedItem);
}
instanceExecutions.push(executionId);
this.setItem(this.instanceId, instanceExecutions);
}
popExecution(executionId) {
const instanceData = this.getItem(executionId) || [];
instanceData.forEach((actionId) => {
storage.removeItem(actionId);
});
storage.removeItem(executionId);
}
pushActionToExecution(executionId, actionId) {
const actions = this.getItem(executionId) || [];
actions.push(actionId);
this.setItem(executionId, actions);
}
/**
* @param {Object} action
* {
* actionId: '',
* nodeId: '',
* executionId: '',
* nodeType: '',
* timestamp: '',
* properties: {},
* }
*/
addActionRecord(action) {
return __awaiter(this, void 0, void 0, function* () {
const { executionId, actionId } = action;
const instanceData = yield this.getExecutionActions(executionId);
if (!instanceData) {
this.addExecution(executionId);
}
this.pushActionToExecution(executionId, actionId);
this.setItem(actionId, action);
});
}
getActionRecord(actionId) {
return __awaiter(this, void 0, void 0, function* () {
return this.getItem(actionId);
});
}
clear() {
this.clearInstance(this.instanceId);
}
clearInstance(instanceId) {
const instanceExecutions = this.getItem(instanceId) || [];
// TODO: 完善类型定义
instanceExecutions.forEach((executionId) => {
storage.removeItem(executionId);
const instanceData = this.getItem(executionId) || [];
instanceData.forEach((actionId) => {
storage.removeItem(actionId);
});
});
storage.removeItem(instanceId);
}
}
export default Recorder;
//# sourceMappingURL=index.js.map