@livequery/react 1.0.109 → 1.0.110
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useRef } from "react";
|
|
2
2
|
export const useSyncMemo = (fn, deps) => {
|
|
3
|
-
const ref = useRef({ value: undefined, deps: [], n: 0 });
|
|
3
|
+
const ref = useRef({ value: undefined, deps: [], n: 0, cleaner: () => { } });
|
|
4
4
|
const outdated = ref.current.n == 0 || deps.some((e, i) => e != ref.current.deps[i]);
|
|
5
|
-
const [value, cleaner] = outdated ? fn() : [
|
|
5
|
+
const [value, cleaner] = outdated ? (ref.current.cleaner(), fn()) : [
|
|
6
6
|
ref.current.value,
|
|
7
7
|
() => { }
|
|
8
8
|
];
|
|
9
|
-
ref.current = { deps, value, n: ref.current.n + 1 };
|
|
10
|
-
useEffect(() => {
|
|
11
|
-
return cleaner;
|
|
12
|
-
}, [ref.current.value]);
|
|
9
|
+
ref.current = { deps, value, n: ref.current.n + 1, cleaner };
|
|
13
10
|
return value;
|
|
14
11
|
};
|
|
@@ -11,14 +11,7 @@ export const useCollectionData = (ref, collection_options = {}) => {
|
|
|
11
11
|
const { transporter } = useLiveQueryContext();
|
|
12
12
|
const n = useRef(0);
|
|
13
13
|
const lazy = collection_options.lazy;
|
|
14
|
-
const [
|
|
15
|
-
filters: {},
|
|
16
|
-
has_more: false,
|
|
17
|
-
items: [],
|
|
18
|
-
loaded: false,
|
|
19
|
-
n: 0,
|
|
20
|
-
loading: lazy ? false : !!ref
|
|
21
|
-
});
|
|
14
|
+
const [version, set_version] = useState(0);
|
|
22
15
|
const client = useSyncMemo(() => {
|
|
23
16
|
const client = new CollectionObservable(ref, { transporter, ...collection_options });
|
|
24
17
|
n.current = 0;
|
|
@@ -28,7 +21,7 @@ export const useCollectionData = (ref, collection_options = {}) => {
|
|
|
28
21
|
}
|
|
29
22
|
else {
|
|
30
23
|
n.current++;
|
|
31
|
-
|
|
24
|
+
set_version(Math.random());
|
|
32
25
|
}
|
|
33
26
|
});
|
|
34
27
|
if (!lazy) {
|
|
@@ -36,11 +29,11 @@ export const useCollectionData = (ref, collection_options = {}) => {
|
|
|
36
29
|
}
|
|
37
30
|
return [client, () => subscription.unsubscribe()];
|
|
38
31
|
}, [ref]);
|
|
39
|
-
const
|
|
32
|
+
const loaded = n.current > 0;
|
|
33
|
+
const empty = loaded && !client.value.loading && client.value.items.length == 0;
|
|
40
34
|
const result = {
|
|
41
|
-
...
|
|
42
|
-
|
|
43
|
-
loading: !!stream.loading || (!collection_options?.lazy && !!ref && n.current == 0),
|
|
35
|
+
...client.value,
|
|
36
|
+
loaded,
|
|
44
37
|
empty,
|
|
45
38
|
add: assert(client?.add, client),
|
|
46
39
|
fetch_more: assert(client?.fetch_more, client),
|