@livequery/react 2.0.0 → 2.0.1
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.
- package/build/useCollectionData.js +13 -8
- package/package.json +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { useRef, useState } from "react";
|
|
2
|
+
import { useEffect, useRef, useState } from "react";
|
|
3
3
|
import { useLiveQueryContext } from "./LiveQueryContext.js";
|
|
4
4
|
import { CollectionObservable } from "@livequery/client";
|
|
5
5
|
import { Subject } from 'rxjs';
|
|
@@ -13,21 +13,23 @@ export const useCollectionData = (ref, collection_options = {}) => {
|
|
|
13
13
|
const lazy = collection_options.lazy;
|
|
14
14
|
const [, set_version] = useState(0);
|
|
15
15
|
const client = useSyncMemo(() => {
|
|
16
|
-
const client = new CollectionObservable(ref, { transporter, ...collection_options });
|
|
17
16
|
n.current = 0;
|
|
17
|
+
const client = new CollectionObservable(ref, { transporter, ...collection_options });
|
|
18
|
+
const a = client.$changes.subscribe(changes => {
|
|
19
|
+
n.current++;
|
|
20
|
+
});
|
|
18
21
|
const subscription = client.subscribe(data => {
|
|
19
|
-
if (collection_options.load_all && data.paging.has?.next) {
|
|
22
|
+
if (!lazy && collection_options.load_all && data.paging.has?.next) {
|
|
20
23
|
client.fetch_more();
|
|
21
24
|
}
|
|
22
25
|
else {
|
|
23
|
-
n.current++;
|
|
24
26
|
set_version(Math.random());
|
|
25
27
|
}
|
|
26
28
|
});
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
return [client, () => {
|
|
30
|
+
subscription.unsubscribe();
|
|
31
|
+
a.unsubscribe();
|
|
32
|
+
}];
|
|
31
33
|
}, [ref]);
|
|
32
34
|
const loaded = n.current > 0;
|
|
33
35
|
const $ = client.$.getValue();
|
|
@@ -48,5 +50,8 @@ export const useCollectionData = (ref, collection_options = {}) => {
|
|
|
48
50
|
$changes: client?.$changes || new Subject(),
|
|
49
51
|
ref
|
|
50
52
|
};
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
!lazy && !loaded && client.fetch_more();
|
|
55
|
+
}, [lazy]);
|
|
51
56
|
return result;
|
|
52
57
|
};
|