Elffuss BraidHub

kikocis/utopia public

dev · 668 BRawPermalink
1// 当前工作区/知识库选择:localStorage 持久化 + useSyncExternalStore 订阅。
2function makeStore(key: string) {
3 let current: string | null =
4 typeof localStorage !== "undefined" ? localStorage.getItem(key) : null;
5 const listeners = new Set<() => void>();
6 return {
7 get: () => current,
8 set: (id: string) => {
9 current = id;
10 localStorage.setItem(key, id);
11 listeners.forEach((l) => l());
12 },
13 subscribe: (l: () => void) => {
14 listeners.add(l);
15 return () => {
16 listeners.delete(l);
17 };
18 },
19 };
20}
21
22export const wsStore = makeStore("utopia.ws");
23export const kbStore = makeStore("utopia.kb");