@livequery/react 1.0.106 → 1.0.107
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.
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const useSyncMemo: <T>(fn: () => [value: T, cleaner: () => void], deps: any[]) => T;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { useEffect, useRef } from "react";
|
|
2
|
+
export const useSyncMemo = (fn, deps) => {
|
|
3
|
+
const ref = useRef({ value: undefined, deps: [], n: 0 });
|
|
4
|
+
const outdated = ref.current.n == 0 || deps.some((e, i) => e != ref.current.deps[i]);
|
|
5
|
+
const [value, cleaner] = outdated ? fn() : [
|
|
6
|
+
ref.current.value,
|
|
7
|
+
() => { }
|
|
8
|
+
];
|
|
9
|
+
ref.current = { deps, value, n: ref.current.n + 1 };
|
|
10
|
+
useEffect(() => {
|
|
11
|
+
return cleaner;
|
|
12
|
+
}, [ref.current.value]);
|
|
13
|
+
return value;
|
|
14
|
+
};
|
|
@@ -10,4 +10,6 @@ export type CollectionData<T extends LivequeryBaseEntity> = (CollectionStream<T>
|
|
|
10
10
|
loaded: boolean;
|
|
11
11
|
});
|
|
12
12
|
export type CollectionRef = string | undefined | '' | null | false;
|
|
13
|
-
export declare const useCollectionData: <T extends LivequeryBaseEntity>(ref: CollectionRef, collection_options?: Partial<useCollectionDataOptions<T>>
|
|
13
|
+
export declare const useCollectionData: <T extends LivequeryBaseEntity>(ref: CollectionRef, collection_options?: Partial<useCollectionDataOptions<T>> & {
|
|
14
|
+
vkl?: any;
|
|
15
|
+
}) => CollectionData<T>;
|
|
@@ -1,38 +1,42 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import {
|
|
2
|
+
import { useRef, 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";
|
|
6
7
|
function assert(fn, thiss) {
|
|
7
8
|
return (fn || (() => { })).bind(thiss);
|
|
8
9
|
}
|
|
9
10
|
export const useCollectionData = (ref, collection_options = {}) => {
|
|
10
11
|
const { transporter } = useLiveQueryContext();
|
|
11
|
-
const
|
|
12
|
+
const n = useRef(0);
|
|
13
|
+
const lazy = collection_options.lazy;
|
|
14
|
+
const [stream, set_stream] = useState({
|
|
12
15
|
filters: {},
|
|
13
|
-
items: [],
|
|
14
16
|
has_more: false,
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
17
|
+
items: [],
|
|
18
|
+
loaded: false,
|
|
19
|
+
n: 0
|
|
18
20
|
});
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
if (!ref || typeof window == 'undefined')
|
|
23
|
-
return;
|
|
24
|
-
const collection = collection_ref.current = new CollectionObservable(ref, { transporter, ...collection_options });
|
|
21
|
+
const DEBUG = ref?.includes('webhooks');
|
|
22
|
+
const client = useSyncMemo(() => {
|
|
23
|
+
const client = new CollectionObservable(ref, { transporter, ...collection_options });
|
|
25
24
|
n.current = 0;
|
|
26
|
-
const subscription =
|
|
27
|
-
collection_options.load_all && data.
|
|
28
|
-
|
|
29
|
-
|
|
25
|
+
const subscription = client.subscribe(data => {
|
|
26
|
+
if (collection_options.load_all && data.has_more) {
|
|
27
|
+
client.fetch_more();
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
n.current++;
|
|
31
|
+
set_stream({ ...data, loaded: true, n: n.current });
|
|
32
|
+
}
|
|
30
33
|
});
|
|
31
|
-
!
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
if (!lazy) {
|
|
35
|
+
client.fetch_more();
|
|
36
|
+
}
|
|
37
|
+
return [client, () => subscription.unsubscribe()];
|
|
38
|
+
}, [ref]);
|
|
39
|
+
const empty = !client.value.loading && client.value.items.length == 0 && stream.loaded;
|
|
36
40
|
const result = {
|
|
37
41
|
...stream,
|
|
38
42
|
error: stream.error,
|
|
@@ -9,6 +9,5 @@ export declare const useDocumentData: <T extends {
|
|
|
9
9
|
item: import("@livequery/client").SmartQueryItem<T>;
|
|
10
10
|
loading: boolean | undefined;
|
|
11
11
|
error: import("@livequery/types").ErrorInfo | undefined;
|
|
12
|
-
reload: () => void;
|
|
13
12
|
$changes: import("rxjs").Subject<import("@livequery/types").UpdatedData<T>>;
|
|
14
13
|
};
|
package/build/useDocumentData.js
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { useCollectionData } from "./useCollectionData.js";
|
|
3
3
|
export const useDocumentData = (ref, options) => {
|
|
4
|
-
const { items, loading, error,
|
|
4
|
+
const { items, loading, error, $changes } = useCollectionData(ref, options);
|
|
5
5
|
return {
|
|
6
6
|
item: items[0],
|
|
7
7
|
loading,
|
|
8
8
|
error,
|
|
9
|
-
reload,
|
|
10
9
|
$changes
|
|
11
10
|
};
|
|
12
11
|
};
|