@livequery/react 2.0.1 → 2.0.6
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.
|
@@ -8,8 +8,13 @@ export type useCollectionDataOptions<T extends LivequeryBaseEntity = LivequeryBa
|
|
|
8
8
|
export type CollectionRef = string | undefined | '' | null | false;
|
|
9
9
|
export type CollectionData<T extends LivequeryBaseEntity> = (CollectionStream<T> & Pick<CollectionObservable<T>, 'add' | 'fetch_more' | 'fetch_prev' | 'reset' | 'trigger' | 'update' | '$changes' | 'filter'> & {
|
|
10
10
|
empty: boolean;
|
|
11
|
-
loaded: boolean;
|
|
12
11
|
ref: CollectionRef;
|
|
13
12
|
filters: CollectionStream<T>['options'];
|
|
14
13
|
});
|
|
15
|
-
export declare const useCollectionData: <T extends LivequeryBaseEntity>(ref: CollectionRef, collection_options?: Partial<useCollectionDataOptions<T>>) =>
|
|
14
|
+
export declare const useCollectionData: <T extends LivequeryBaseEntity>(ref: CollectionRef, collection_options?: Partial<useCollectionDataOptions<T>>) => CollectionStream<T> & Pick<CollectionObservable<T>, "add" | "fetch_more" | "fetch_prev" | "reset" | "trigger" | "update" | "$changes" | "filter"> & {
|
|
15
|
+
empty: boolean;
|
|
16
|
+
ref: CollectionRef;
|
|
17
|
+
filters: Partial<QueryOption<T>>;
|
|
18
|
+
} & {
|
|
19
|
+
state: number;
|
|
20
|
+
};
|
|
@@ -1,57 +1,49 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { useEffect,
|
|
2
|
+
import { useEffect, useMemo, useState } from "react";
|
|
3
3
|
import { useLiveQueryContext } from "./LiveQueryContext.js";
|
|
4
4
|
import { CollectionObservable } from "@livequery/client";
|
|
5
5
|
import { Subject } from 'rxjs';
|
|
6
|
-
import { useSyncMemo } from "./hooks/useSyncMemo.js";
|
|
7
6
|
function assert(fn, thiss) {
|
|
8
7
|
return (fn || (() => { })).bind(thiss);
|
|
9
8
|
}
|
|
10
9
|
export const useCollectionData = (ref, collection_options = {}) => {
|
|
11
10
|
const { transporter } = useLiveQueryContext();
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
const [,
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
if (!lazy && collection_options.load_all && data.paging.has?.next) {
|
|
11
|
+
const client = useMemo(() => new CollectionObservable(ref, { transporter, ...collection_options }), [ref]);
|
|
12
|
+
const [$, set_$] = useState(client.getValue());
|
|
13
|
+
const [state, set_state] = useState(0);
|
|
14
|
+
// Sync stream effect
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
if (collection_options.lazy)
|
|
17
|
+
return;
|
|
18
|
+
client.fetch_more();
|
|
19
|
+
const s = client.subscribe(data => {
|
|
20
|
+
if (collection_options.load_all && data.paging.has?.next) {
|
|
23
21
|
client.fetch_more();
|
|
24
22
|
}
|
|
25
23
|
else {
|
|
26
|
-
|
|
24
|
+
set_$(data);
|
|
25
|
+
set_state(Date.now());
|
|
27
26
|
}
|
|
28
27
|
});
|
|
29
|
-
return
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
const loaded = n.current > 0;
|
|
35
|
-
const $ = client.$.getValue();
|
|
36
|
-
const empty = loaded && !$.loading && $.items.length == 0;
|
|
28
|
+
return () => {
|
|
29
|
+
s.unsubscribe();
|
|
30
|
+
};
|
|
31
|
+
}, [client, set_$, collection_options.lazy, set_state]);
|
|
32
|
+
const empty = !!(!$.loading && $.items.length == 0);
|
|
37
33
|
const result = {
|
|
38
34
|
...$,
|
|
39
|
-
|
|
35
|
+
state,
|
|
40
36
|
empty,
|
|
41
37
|
filters: $.options,
|
|
42
38
|
add: assert(client?.add, client),
|
|
43
39
|
fetch_more: assert(client?.fetch_more, client),
|
|
44
40
|
fetch_prev: assert(client?.fetch_prev, client),
|
|
45
41
|
filter: assert(client?.filter, client),
|
|
46
|
-
// fetch_around_cursor: assert(client?.fetch_around_cursor, client),
|
|
47
42
|
reset: assert(client?.reset, client),
|
|
48
43
|
trigger: assert(client?.trigger, client),
|
|
49
44
|
update: assert(client?.update, client),
|
|
50
45
|
$changes: client?.$changes || new Subject(),
|
|
51
46
|
ref
|
|
52
47
|
};
|
|
53
|
-
useEffect(() => {
|
|
54
|
-
!lazy && !loaded && client.fetch_more();
|
|
55
|
-
}, [lazy]);
|
|
56
48
|
return result;
|
|
57
49
|
};
|