-
Notifications
You must be signed in to change notification settings - Fork 0
/
persistency.ts
62 lines (57 loc) · 1.79 KB
/
persistency.ts
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
54
55
56
57
58
59
60
61
62
import { settings } from "./bsim.js";
import { Value, signals } from "./jsml/signals.js";
import { LANG } from "./lang.js";
type Settings = {
graphics: {
blur: Value<"off"|"low"|"high">,
gate_symbols: Value<"ansi"|"iec">,
},
advanced: {
debug_display: Value<boolean>,
perf_graph: Value<boolean>,
}
}
export function saveSettings(s: Settings) {
const jsonSettings = JSON.stringify(s, (k: string, v: any) => {
if (v instanceof Value) {
return v.value;
}
return v;
});
try {
localStorage.setItem("settingsVersion", "1");
localStorage.setItem("settings", jsonSettings);
localStorage.setItem("lang", LANG.get());
} catch {
console.warn("Unable to save settings.");
}
}
export function loadSettings(): Settings {
const lang = localStorage.getItem("lang");
if (lang) {
LANG.set(lang as typeof LANG["value"]);
}
const version = localStorage.getItem("settingsVersion");
if (version === null) {
return {
graphics: {
blur: signals.value<"off"|"low"|"high">("low"),
gate_symbols: signals.value<"ansi"|"iec">("ansi"),
},
advanced: {
debug_display: signals.value(false),
perf_graph: signals.value(false),
}
}
}
const s = localStorage.getItem("settings") as string;
if (version === "1") {
return JSON.parse(s, (k: string, v: any) => {
if (typeof v === "string" || typeof v === "boolean" || typeof v === "number") {
return signals.value(v);
}
return v;
});
}
throw new Error(`Trying to load settings... from the future???!?! (settingsVersion: ${version})`);
}