@livequery/react 2.0.15 → 2.0.17

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.
@@ -5,5 +5,7 @@ export type LivequeryContext = {
5
5
  hooks?: TransporterHook[];
6
6
  };
7
7
  export declare function mergeTransporterHooks(...hooks: TransporterHook[]): TransporterHook;
8
- export declare const useLivequeryContext: () => LivequeryContext;
8
+ export declare const useLivequeryContext: () => {
9
+ transporter: Transporter;
10
+ };
9
11
  export declare const LiveQueryContextProvider: (props: PropsWithChildren<LivequeryContext>) => React.JSX.Element;
@@ -6,9 +6,16 @@ export function mergeTransporterHooks(...hooks) {
6
6
  return hooks.reduce((p, c) => (next) => p(() => c(next)), (next) => pipe(next()));
7
7
  }
8
8
  const LivequeryContext = createContext({});
9
- export const useLivequeryContext = () => useContext(LivequeryContext);
9
+ export const useLivequeryContext = () => {
10
+ const ctx = useContext(LivequeryContext);
11
+ if (!ctx.transporter)
12
+ throw 'MISSING_LIVEQUERY_TRANSPORTER';
13
+ return {
14
+ transporter: ctx.transporter
15
+ };
16
+ };
10
17
  export const LiveQueryContextProvider = (props) => {
11
- const ctx = useLivequeryContext();
18
+ const ctx = useContext(LivequeryContext);
12
19
  const merged = mergeTransporterHooks(...props.hooks || []);
13
20
  const transporter = ctx.transporter ? ctx.transporter.hook(merged) : props.transporter?.hook(merged);
14
21
  return (React.createElement(LivequeryContext.Provider, { value: { transporter } }, props.children));
@@ -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","../src/hooks/usesyncmemo.ts"],"version":"5.6.3"}
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,10 +6,9 @@ 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
- if (!transporter)
12
- throw 'MISSING_LIVEQUERY_TRANSPORTER';
13
12
  const client = useMemo(() => new CollectionObservable(ref, {
14
13
  ...collection_options,
15
14
  transporter
@@ -50,5 +49,9 @@ export const useCollectionData = (ref, collection_options = {}) => {
50
49
  $changes: client?.$changes || new Subject(),
51
50
  ref
52
51
  };
52
+ useEffect(() => {
53
+ ref && CollectionMap.set(ref, result);
54
+ return () => { ref && CollectionMap.delete(ref); };
55
+ }, [client]);
53
56
  return result;
54
57
  };
@@ -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: import("@livequery/client").SmartQueryItem<T>;
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>>;
@@ -1,7 +1,36 @@
1
1
  "use client";
2
- import { useCollectionData } from "./useCollectionData.js";
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 { items, loading, error, $changes } = useCollectionData(ref, config);
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
@@ -4,7 +4,7 @@
4
4
  "url": "https://github.com/livequery/react"
5
5
  },
6
6
  "type": "module",
7
- "version": "2.0.15",
7
+ "version": "2.0.17",
8
8
  "description": "",
9
9
  "main": "build/index.js",
10
10
  "types": "build/index.d.ts",
@@ -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
- };