-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstorage.ts
79 lines (67 loc) · 2.36 KB
/
storage.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { atom, Atom, getDefaultStore, useAtom } from "jotai";
import type { EmeraPlugin } from '../plugin';
import { useEmeraContext } from "./context";
import { normalizePath } from "obsidian";
export const createEmeraStorage = (plugin: EmeraPlugin) => {
const filePath = normalizePath(`${plugin.settings.componentsFolder}/storage.json`);
let state: Record<string, any> = {};
let flushTimerId: null | ReturnType<typeof setTimeout> = null;
const atoms: Record<string, Atom<any>> = {};
const unsubFunction: VoidFunction[] = [];
const init = async () => {
const exists = await plugin.app.vault.adapter.exists(filePath);
if (exists) {
try {
const content = await plugin.app.vault.adapter.read(filePath);
state = JSON.parse(content);
} catch (err) {
console.log(`Emera storage file exists, but Emera couldn't read or parse it`);
}
}
};
const destroy = () => {
unsubFunction.forEach(cb => cb());
};
const flush = async () => {
const stateStr = JSON.stringify(state, null, 4);
await plugin.app.vault.adapter.write(filePath, stateStr);
};
const set = (prop: string, val: any) => {
state[prop] = val;
if (flushTimerId !== null) clearTimeout(flushTimerId);
flushTimerId = setTimeout(async () => {
await flush();
flushTimerId = null;
}, 100);
};
const get = (prop: string) => {
return state[prop];
};
const getAtom = (prop: string, defaultValue: any) => {
if (atoms[prop]) return atoms[prop];
const primitiveAtom = atom((prop in state) ? state[prop] : defaultValue);
atoms[prop] = primitiveAtom;
const store = getDefaultStore();
unsubFunction.push(
store.sub(primitiveAtom, () => {
const value = store.get(primitiveAtom);
set(prop, value);
})
);
return primitiveAtom;
};
return {
init,
destroy,
set,
get,
flush,
getAtom,
};
};
export type EmeraStorage = ReturnType<typeof createEmeraStorage>;
export const useStorage = <T>(key: string, defaultValue: T) => {
const { storage } = useEmeraContext();
const atom = storage.getAtom(key, defaultValue);
return useAtom(atom);
};