Repository navigation
Expand file tree
/
Copy pathstore.ts
More file actions
151 lines (144 loc) · 4.12 KB
/
Copy pathstore.ts
File metadata and controls
151 lines (144 loc) · 4.12 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import {
parseAttempts,
parseProgressGeneration,
type AttemptRecord,
} from "./model.ts";
export interface ProgressSnapshot {
attempts: AttemptRecord[];
hydrated: boolean;
storageAvailable: boolean;
}
interface StorageLike {
getItem(key: string): string | null;
setItem(key: string, value: string): void;
}
/** One store per learner. Guest progress never silently moves between accounts. */
export function createProgressStore(
scope: string,
storage?: () => StorageLike | undefined,
) {
const key = `procoderji:practice:v1:${scope}`;
const initial: ProgressSnapshot = {
attempts: [],
hydrated: false,
storageAvailable: true,
};
let snapshot = initial;
let loaded = false;
let generation: string | null = null;
let pending = Promise.resolve();
const createdHere = new Set<string>();
const listeners = new Set<() => void>();
const resolveStorage =
storage ??
(() => (typeof window === "undefined" ? undefined : window.localStorage));
const emit = () => listeners.forEach((listener) => listener());
const onStorage = (event: StorageEvent) => {
if (event.key === key || event.key === null) {
read();
emit();
}
};
const read = () => {
try {
const target = resolveStorage();
const raw = target?.getItem(key);
generation = null;
let attempts: AttemptRecord[] = [];
if (raw) {
try {
const value: unknown = JSON.parse(raw);
if (Array.isArray(value) && scope === "guest")
attempts = parseAttempts(value);
else if (
value &&
typeof value === "object" &&
"version" in value &&
value.version === 1 &&
"attempts" in value &&
"generation" in value
) {
attempts = parseAttempts(value.attempts);
generation = parseProgressGeneration(value.generation);
}
} catch {
/* Ignore corrupted local data. */
}
}
snapshot = {
attempts,
hydrated: true,
storageAvailable: Boolean(target),
};
} catch {
snapshot = { ...snapshot, hydrated: true, storageAvailable: false };
}
loaded = true;
};
const getSnapshot = () => {
if (!loaded) read();
return snapshot;
};
const replace = (
attempts: readonly AttemptRecord[],
nextGeneration?: string | null,
) => {
if (!loaded) read();
const storedGeneration =
nextGeneration === undefined ? generation : nextGeneration;
const validated = parseAttempts(attempts);
let storageAvailable = true;
try {
const target = resolveStorage();
storageAvailable = Boolean(target);
target?.setItem(
key,
JSON.stringify({
version: 1,
generation: storedGeneration,
attempts: validated,
}),
);
} catch {
storageAvailable = false;
}
loaded = true;
generation = storedGeneration;
snapshot = { attempts: validated, hydrated: true, storageAvailable };
emit();
};
const addMany = (attempts: readonly AttemptRecord[]) => {
for (const attempt of attempts) createdHere.add(attempt.id);
replace([...attempts, ...getSnapshot().attempts]);
};
return {
key,
getSnapshot,
getServerSnapshot: () => initial,
replace,
runExclusive: (operation: () => Promise<void>) => {
const next = pending.then(operation, operation);
pending = next.catch(() => {});
return next;
},
getGeneration: () => {
getSnapshot();
return generation;
},
getNewAttempts: () =>
getSnapshot().attempts.filter((attempt) => createdHere.has(attempt.id)),
addMany,
add: (attempt: AttemptRecord) => addMany([attempt]),
subscribe: (listener: () => void) => {
const first = listeners.size === 0;
listeners.add(listener);
if (first && typeof window !== "undefined")
window.addEventListener("storage", onStorage);
return () => {
listeners.delete(listener);
if (!listeners.size && typeof window !== "undefined")
window.removeEventListener("storage", onStorage);
};
},
};
}