-
Notifications
You must be signed in to change notification settings - Fork 0
/
hooks.mjs
47 lines (37 loc) · 1.24 KB
/
hooks.mjs
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
import {
useCallback, useMemo, useState, useEffect
} from "./vdom.mjs";
import { CoreStore } from "./cbdb.mjs";
// WORKAROUND: https://twitter.com/natevw/status/1344063848521760769
// TODO: if the imported `useMemo` stops providing semantic stability,
// replace this with an implementation that does.
// see `useMemo` at https://github.com/preactjs/preact/blob/91016b/hooks/src/index.js#L214
// and `argsChanged` at https://github.com/preactjs/preact/blob/91016b/hooks/src/index.js#L376
const useStable = useMemo;
function useRefresh() {
// HT: https://stackoverflow.com/a/53215514/179583
const [, setShamValue] = useState();
return useCallback(() =>
setShamValue(new Object()),
[]);
}
const objectIdentity = d => d;
export function useSyncableStore(initialObjs, key=objectIdentity) {
const db = useStable(() =>
new CoreStore(initialObjs),
[]);
// TODO: could/should we do anything if `initialObjs` changes?
// (seems like we would need to know external identifiers…)
const forceUpdate = useRefresh();
useEffect(() =>
db.subscribe(() => {
console.log("Database changed.");
forceUpdate();
}),
[db]);
return db;
}
export function useFilter() {
}
export function useIndex() {
}