Move panel temporary state to worker OPFS
This commit is contained in:
64
web/worker/opfs-worker-bridge.mjs
Normal file
64
web/worker/opfs-worker-bridge.mjs
Normal file
@@ -0,0 +1,64 @@
|
||||
import {
|
||||
clearPowerOffCheckpoint,
|
||||
clearUiState,
|
||||
loadPowerOffCheckpoint,
|
||||
loadUiState,
|
||||
savePowerOffCheckpoint,
|
||||
saveUiState,
|
||||
} from "./opfs-ui-state.mjs";
|
||||
|
||||
const UI_COMMANDS = new Set(["UI_STATE_SAVE", "UI_STATE_LOAD", "UI_STATE_CLEAR"]);
|
||||
|
||||
function object(value) {
|
||||
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : {};
|
||||
}
|
||||
|
||||
function resultEnvelope(input, payload) {
|
||||
return {
|
||||
schema_version: 1,
|
||||
kind: "UI_STATE_RESULT",
|
||||
context_id: input.context_id,
|
||||
session_id: input.session_id,
|
||||
seq: input.seq,
|
||||
operation_id: input.operation_id ?? null,
|
||||
payload: { ...payload, command: input.kind, operation_id: input.operation_id ?? null },
|
||||
};
|
||||
}
|
||||
|
||||
export function isUiStateCommand(input) {
|
||||
return UI_COMMANDS.has(input?.kind);
|
||||
}
|
||||
|
||||
export async function handleWorkerMessage(session, input) {
|
||||
if (isUiStateCommand(input)) {
|
||||
// UI cache is a Worker storage service, not a LinuxCNC command. It never
|
||||
// consumes the WASM protocol sequence or changes task/motion authority.
|
||||
const payload = object(input.payload);
|
||||
const options = { contextId: input.context_id, key: payload.key ?? "xyzac-trt" };
|
||||
const result = input.kind === "UI_STATE_SAVE"
|
||||
? await saveUiState({ ...options, state: payload.state })
|
||||
: input.kind === "UI_STATE_LOAD"
|
||||
? await loadUiState(options)
|
||||
: await clearUiState(options);
|
||||
return resultEnvelope(input, result);
|
||||
}
|
||||
|
||||
let request = input;
|
||||
if (input?.kind === "LOAD_WORKSPACE" && !object(input.payload).recovery_checkpoint) {
|
||||
const persisted = await loadPowerOffCheckpoint({ contextId: input.context_id, key: "xyzac-trt" });
|
||||
if (persisted.status === "verified" && persisted.checkpoint) request = { ...input, payload: { ...object(input.payload), recovery_checkpoint: persisted.checkpoint } };
|
||||
}
|
||||
const response = session.accept(request);
|
||||
const payload = object(response?.payload);
|
||||
if (input?.kind === "POWER" && response?.kind === "COMMAND_RESULT" && payload.status === "verified") {
|
||||
if (payload.active === false && payload.recovery_checkpoint) {
|
||||
const persisted = await savePowerOffCheckpoint({ contextId: input.context_id, checkpoint: payload.recovery_checkpoint, key: "xyzac-trt" });
|
||||
payload.recovery_persistence = persisted;
|
||||
} else if (payload.active === true) {
|
||||
payload.recovery_persistence = await clearPowerOffCheckpoint({ contextId: input.context_id, key: "xyzac-trt" });
|
||||
}
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
export { UI_COMMANDS };
|
||||
Reference in New Issue
Block a user