@livequery/react 2.0.15 → 2.0.16
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/tsconfig.tsbuildinfo +1 -1
- package/build/useCollectionData.d.ts +1 -0
- package/build/useCollectionData.js +5 -0
- package/build/useDocumentData.d.ts +7 -1
- package/build/useDocumentData.js +31 -2
- package/package.json +1 -1
- package/build/hooks/useSyncMemo.d.ts +0 -1
- package/build/hooks/useSyncMemo.js +0 -11
|
@@ -1 +1 @@
|
|
|
1
|
-
{"root":["../src/index.ts","../src/livequerycontext.tsx","../src/usecollectiondata.ts","../src/usedocumentdata.ts","../src/usemonitor.ts","../src/hooks/createcontextfromhook.tsx","../src/hooks/createstaticcontext.tsx"
|
|
1
|
+
{"root":["../src/index.ts","../src/livequerycontext.tsx","../src/usecollectiondata.ts","../src/usedocumentdata.ts","../src/usemonitor.ts","../src/hooks/createcontextfromhook.tsx","../src/hooks/createstaticcontext.tsx"],"version":"5.6.3"}
|
|
@@ -11,6 +11,7 @@ export type CollectionData<T extends LivequeryBaseEntity> = (CollectionStream<T>
|
|
|
11
11
|
ref: CollectionRef;
|
|
12
12
|
filters: CollectionStream<T>['options'];
|
|
13
13
|
});
|
|
14
|
+
export declare const CollectionMap: Map<string, CollectionData<any>>;
|
|
14
15
|
export declare const useCollectionData: <T extends LivequeryBaseEntity>(ref: CollectionRef, collection_options?: Partial<useCollectionDataOptions<T>>) => CollectionStream<T> & Pick<CollectionObservable<T>, "add" | "reset" | "filter" | "update" | "fetch_more" | "fetch_prev" | "trigger" | "$changes"> & {
|
|
15
16
|
empty: boolean;
|
|
16
17
|
ref: CollectionRef;
|
|
@@ -6,6 +6,7 @@ import { Subject } from 'rxjs';
|
|
|
6
6
|
function assert(fn, thiss) {
|
|
7
7
|
return (fn || (() => { })).bind(thiss);
|
|
8
8
|
}
|
|
9
|
+
export const CollectionMap = new Map();
|
|
9
10
|
export const useCollectionData = (ref, collection_options = {}) => {
|
|
10
11
|
const { transporter } = useLivequeryContext();
|
|
11
12
|
if (!transporter)
|
|
@@ -50,5 +51,9 @@ export const useCollectionData = (ref, collection_options = {}) => {
|
|
|
50
51
|
$changes: client?.$changes || new Subject(),
|
|
51
52
|
ref
|
|
52
53
|
};
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
ref && CollectionMap.set(ref, result);
|
|
56
|
+
return () => { ref && CollectionMap.delete(ref); };
|
|
57
|
+
}, [client]);
|
|
53
58
|
return result;
|
|
54
59
|
};
|
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
import { useCollectionDataOptions } from "./useCollectionData.js";
|
|
2
2
|
import { LivequeryBaseEntity } from "@livequery/types";
|
|
3
|
+
import { SmartQueryItem } from "@livequery/client";
|
|
3
4
|
export type useDocumentDataOptions<T extends LivequeryBaseEntity = LivequeryBaseEntity> = Partial<Omit<useCollectionDataOptions<T>, 'filters'>>;
|
|
4
5
|
export declare const useDocumentData: <T extends LivequeryBaseEntity>(ref: string | undefined | "" | null | false, config?: useDocumentDataOptions<T>) => {
|
|
5
|
-
item:
|
|
6
|
+
item: SmartQueryItem<T>;
|
|
7
|
+
error: null;
|
|
8
|
+
loading: boolean;
|
|
9
|
+
$changes: import("rxjs").Subject<import("@livequery/types").UpdatedData<T>>;
|
|
10
|
+
} | {
|
|
11
|
+
item: SmartQueryItem<T>;
|
|
6
12
|
loading: import("@livequery/client").LoadingIndicator | undefined;
|
|
7
13
|
error: boolean | undefined;
|
|
8
14
|
$changes: import("rxjs").Subject<import("@livequery/types").UpdatedData<T>>;
|
package/build/useDocumentData.js
CHANGED
|
@@ -1,7 +1,36 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import {
|
|
2
|
+
import { useEffect, useState } from "react";
|
|
3
|
+
import { CollectionMap, useCollectionData } from "./useCollectionData.js";
|
|
4
|
+
import { filter, tap } from "rxjs";
|
|
3
5
|
export const useDocumentData = (ref, config = {}) => {
|
|
4
|
-
const
|
|
6
|
+
const [old, set_old] = useState((() => {
|
|
7
|
+
if (!ref)
|
|
8
|
+
return;
|
|
9
|
+
const id = ref.split('/').pop();
|
|
10
|
+
const collection_ref = ref.split('/').slice(0, -1).join('/');
|
|
11
|
+
const collection = CollectionMap.get(collection_ref);
|
|
12
|
+
if (!collection)
|
|
13
|
+
return;
|
|
14
|
+
const item = collection.items.find(item => item.id == id);
|
|
15
|
+
if (!item)
|
|
16
|
+
return;
|
|
17
|
+
return { collection, item };
|
|
18
|
+
})());
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
if (!old || !old.item || !ref)
|
|
21
|
+
return;
|
|
22
|
+
const s = old.collection.$changes.pipe(filter(id => id.data.id == old.item.id), tap(d => set_old({ ...old, item: { ...old.item, ...d.data } }))).subscribe();
|
|
23
|
+
return () => s.unsubscribe();
|
|
24
|
+
}, [ref]);
|
|
25
|
+
const { items, loading, error, $changes } = useCollectionData(old ? null : ref, config);
|
|
26
|
+
if (old && old.item) {
|
|
27
|
+
return {
|
|
28
|
+
item: old.item,
|
|
29
|
+
error: null,
|
|
30
|
+
loading: false,
|
|
31
|
+
$changes: old.collection.$changes
|
|
32
|
+
};
|
|
33
|
+
}
|
|
5
34
|
return {
|
|
6
35
|
item: items[0],
|
|
7
36
|
loading,
|
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const useSyncMemo: <T>(fn: () => [value: T, cleaner: () => void], deps: any[]) => T;
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { useRef } from "react";
|
|
2
|
-
export const useSyncMemo = (fn, deps) => {
|
|
3
|
-
const ref = useRef({ value: undefined, deps: [], n: 0, cleaner: () => { } });
|
|
4
|
-
const outdated = ref.current.n == 0 || deps.some((e, i) => e != ref.current.deps[i]);
|
|
5
|
-
const [value, cleaner] = outdated ? (ref.current.cleaner(), fn()) : [
|
|
6
|
-
ref.current.value,
|
|
7
|
-
() => { }
|
|
8
|
-
];
|
|
9
|
-
ref.current = { deps, value, n: ref.current.n + 1, cleaner };
|
|
10
|
-
return value;
|
|
11
|
-
};
|