Spaces:
Running
Running
File size: 1,224 Bytes
009fd18 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | import type { PersistedSession, PersistenceService } from "../app/types";
import { PERSISTENCE_VERSION } from "../app/types";
const STORAGE_KEY = "private-voice-agent/session";
interface StorageLike {
getItem(key: string): string | null;
setItem(key: string, value: string): void;
removeItem(key: string): void;
}
export class LocalPersistenceService implements PersistenceService {
#storage: StorageLike | null;
constructor(storage: StorageLike | null = typeof localStorage !== "undefined" ? localStorage : null) {
this.#storage = storage;
}
load(): PersistedSession | null {
if (!this.#storage) {
return null;
}
const raw = this.#storage.getItem(STORAGE_KEY);
if (!raw) {
return null;
}
try {
const parsed = JSON.parse(raw) as PersistedSession;
if (parsed.version !== PERSISTENCE_VERSION) {
this.clear();
return null;
}
return parsed;
} catch {
this.clear();
return null;
}
}
save(value: PersistedSession): void {
if (!this.#storage) {
return;
}
this.#storage.setItem(STORAGE_KEY, JSON.stringify(value));
}
clear(): void {
this.#storage?.removeItem(STORAGE_KEY);
}
}
|