41 lines
1.6 KiB
TypeScript
41 lines
1.6 KiB
TypeScript
// SPEC: PJ2026-0106050514 Workbench实时运行面 draft-2026-06-30-p0-1297-spec-first.
|
|
// Responsibility: Workbench local storage namespace wrapper backed by SafeStorageRuntime.
|
|
|
|
import { createSafeStorageRuntime, readJsonStorage, removeStorageKey, writeJsonStorage, type SafeStorageResult, type StorageLike } from "@/utils/safe-storage";
|
|
|
|
const runtime = createSafeStorageRuntime();
|
|
|
|
export function readWorkbenchString(key: string, fallback: string): string {
|
|
return workbenchStorage().getItem(key) ?? fallback;
|
|
}
|
|
|
|
export function writeWorkbenchString(key: string, value: string): void {
|
|
workbenchStorage().setItem(key, value);
|
|
}
|
|
|
|
export function removeWorkbenchStorageKey(key: string): SafeStorageResult<null> {
|
|
return removeStorageKey(workbenchStorage(), key);
|
|
}
|
|
|
|
export function readWorkbenchNumber(key: string, fallback: number): number {
|
|
const value = Number(readWorkbenchString(key, ""));
|
|
return Number.isFinite(value) && value > 0 ? value : fallback;
|
|
}
|
|
|
|
export function readWorkbenchJson<T>(key: string, fallback: T): SafeStorageResult<T> {
|
|
return readJsonStorage(workbenchStorage(), key, fallback);
|
|
}
|
|
|
|
export function writeWorkbenchJson<T>(key: string, value: T): SafeStorageResult<T> {
|
|
return writeJsonStorage(workbenchStorage(), key, value);
|
|
}
|
|
|
|
function workbenchStorage(): StorageLike {
|
|
return runtime.localStorageDirect(globalLocalStorage(), "workbench");
|
|
}
|
|
|
|
function globalLocalStorage(): StorageLike | null {
|
|
if (typeof globalThis === "undefined") return null;
|
|
return (globalThis as typeof globalThis & { localStorage?: StorageLike }).localStorage ?? null;
|
|
}
|