Files
37duAutomation/desktop/electron-shell/electron/state.js
T

56 lines
1.5 KiB
JavaScript

// 运行时共享状态:可变全局唯一真源(对齐 Python webhost state.py)。
"use strict";
const PLATFORMS = {
hr: { id: "hr", label: "HR 系统" },
oa: { id: "oa", label: "OA 系统" },
};
// 运行会话:run_id -> {status, account, plugin_id, name, version, trigger, schedule_id, started, result, error, logs}
const RUNS = new Map();
const MAX_RUNS = 50;
// 单次运行内存中累积的日志上限(结束后由 run-history 落盘,此处只服务"正在跑"的实时展示与回放)
const MAX_RUN_LOGS = 800;
// 插件权限白名单(null = 全量;空集 = 零权限)。由登录/启动/同步决定。
let ALLOWED = null;
function setAllowed(v) {
ALLOWED = v === undefined ? null : v;
}
function setRun(runId, fields) {
const run = RUNS.get(runId);
if (run) Object.assign(run, fields);
}
function registerRun(runId, record) {
if (RUNS.size >= MAX_RUNS) {
const oldest = RUNS.keys().next().value;
RUNS.delete(oldest);
}
RUNS.set(runId, record);
}
// 累积"这一次运行"的日志:日志是运行的属性,跟着运行走(不再是全局单例)
function appendRunLog(runId, entry) {
const run = RUNS.get(runId);
if (!run) return;
if (!Array.isArray(run.logs)) run.logs = [];
run.logs.push(entry);
if (run.logs.length > MAX_RUN_LOGS) run.logs.splice(0, run.logs.length - MAX_RUN_LOGS);
}
module.exports = {
PLATFORMS,
RUNS,
MAX_RUNS,
MAX_RUN_LOGS,
ALLOWED,
setAllowed,
setRun,
registerRun,
appendRunLog,
};