Move panel temporary state to worker OPFS

This commit is contained in:
2026-09-03 14:03:32 +08:00
parent 0f6691a9fc
commit 2b1e249baf
22 changed files with 688 additions and 42 deletions

View File

@@ -1,32 +1,11 @@
export const CONTEXT_ID = "linuxcnc-2.10.0-pre0-wasm-m1-py-c1";
export const PROTOCOL_SCHEMA = 1;
const POWER_OFF_CHECKPOINT_KEY = "linuxcnc-web-wasm:xyzac-power-off-checkpoint";
let CLIENT_INSTANCE = 0;
function isObject(value) {
return value !== null && typeof value === "object" && !Array.isArray(value);
}
function persistPowerOffCheckpoint(message) {
const payload = isObject(message?.payload) ? message.payload : {};
if (message.kind !== "COMMAND_RESULT" || payload.status !== "verified" || payload.command !== "POWER" || payload.active !== false || !isObject(payload.recovery_checkpoint)) return;
try { globalThis.localStorage?.setItem(POWER_OFF_CHECKPOINT_KEY, JSON.stringify(payload.recovery_checkpoint)); } catch { /* checkpoint remains visible in the response */ }
}
function attachPowerOffCheckpoint(payload) {
if (!isObject(payload)) return payload;
try {
const raw = globalThis.localStorage?.getItem(POWER_OFF_CHECKPOINT_KEY);
return raw ? { ...payload, recovery_checkpoint: JSON.parse(raw) } : payload;
} catch { return payload; }
}
function clearPowerOffCheckpoint(message) {
const payload = isObject(message?.payload) ? message.payload : {};
if (message.kind !== "COMMAND_RESULT" || payload.status !== "verified" || payload.command !== "POWER" || payload.active !== true) return;
try { globalThis.localStorage?.removeItem(POWER_OFF_CHECKPOINT_KEY); } catch { /* cleanup is best effort */ }
}
function commandError(message) {
const payload = isObject(message?.payload) ? message.payload : {};
const reason = payload.reason ?? payload.gate_reason ?? payload.message_key ?? payload.code ?? "worker_error";
@@ -64,9 +43,6 @@ export class WorkerClient {
}
#receive(message) {
const persistence = persistPowerOffCheckpoint(message);
if (persistence && isObject(message?.payload)) message.payload.recovery_persistence = persistence;
clearPowerOffCheckpoint(message);
this.#emit(message);
if (!isObject(message)) return;
const operationId = message.operation_id ?? message.payload?.operation_id;
@@ -101,7 +77,7 @@ export class WorkerClient {
seq,
kind,
...(operationId ? { operation_id: operationId } : {}),
payload: kind === "LOAD_WORKSPACE" ? attachPowerOffCheckpoint(payload) : payload,
payload,
};
let resolvePromise;
let rejectPromise;