@livequery/react 2.0.7 → 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.
@@ -1,5 +1,9 @@
1
- import { Transporter } from '@livequery/types';
2
- export type LiveQueryContextOption = {
3
- transporter: Transporter;
1
+ import React, { PropsWithChildren } from 'react';
2
+ import { Transporter, TransporterHook } from '@livequery/types';
3
+ export type LivequeryContext = {
4
+ transporter?: Transporter;
5
+ hooks?: TransporterHook[];
4
6
  };
5
- export declare const useLiveQueryContext: () => LiveQueryContextOption, LiveQueryContextProvider: ({ children, ...props }: import("react").PropsWithChildren<LiveQueryContextOption>) => import("react").JSX.Element;
7
+ export declare function mergeTransporterHooks(...hooks: TransporterHook[]): TransporterHook;
8
+ export declare const useLivequeryContext: () => LivequeryContext;
9
+ export declare const LiveQueryContextProvider: (props: PropsWithChildren<LivequeryContext>) => React.JSX.Element;
@@ -1,3 +1,15 @@
1
1
  "use client";
2
- import { createContextFromHook } from './hooks/createContextFromHook.js';
3
- export const [useLiveQueryContext, LiveQueryContextProvider] = createContextFromHook((props) => props);
2
+ import React from 'react';
3
+ import { createContext, useContext } from "react";
4
+ import { pipe } from 'rxjs';
5
+ export function mergeTransporterHooks(...hooks) {
6
+ return hooks.reduce((p, c) => (next) => p(() => c(next)), (next) => pipe(next()));
7
+ }
8
+ const LivequeryContext = createContext({});
9
+ export const useLivequeryContext = () => useContext(LivequeryContext);
10
+ export const LiveQueryContextProvider = (props) => {
11
+ const ctx = useLivequeryContext();
12
+ const merged = mergeTransporterHooks(...props.hooks || []);
13
+ const transporter = ctx.transporter ? ctx.transporter.hook(merged) : props.transporter?.hook(merged);
14
+ return (React.createElement(LivequeryContext.Provider, { value: { transporter } }, props.children));
15
+ };
@@ -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;
@@ -1,14 +1,20 @@
1
1
  "use client";
2
2
  import { useEffect, useMemo, useState } from "react";
3
- import { useLiveQueryContext } from "./LiveQueryContext.js";
3
+ import { useLivequeryContext } from "./LiveQueryContext.js";
4
4
  import { CollectionObservable } from "@livequery/client";
5
5
  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
- const { transporter } = useLiveQueryContext();
11
- const client = useMemo(() => new CollectionObservable(ref, { transporter, ...collection_options }), [ref]);
11
+ const { transporter } = useLivequeryContext();
12
+ if (!transporter)
13
+ throw 'MISSING_LIVEQUERY_TRANSPORTER';
14
+ const client = useMemo(() => new CollectionObservable(ref, {
15
+ ...collection_options,
16
+ transporter
17
+ }), [ref]);
12
18
  const [$, set_$] = useState(client.getValue());
13
19
  const [state, set_state] = useState(0);
14
20
  // Sync stream effect
@@ -45,5 +51,9 @@ export const useCollectionData = (ref, collection_options = {}) => {
45
51
  $changes: client?.$changes || new Subject(),
46
52
  ref
47
53
  };
54
+ useEffect(() => {
55
+ ref && CollectionMap.set(ref, result);
56
+ return () => { ref && CollectionMap.delete(ref); };
57
+ }, [client]);
48
58
  return result;
49
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: 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.7",
7
+ "version": "2.0.16",
8
8
  "description": "",
9
9
  "main": "build/index.js",
10
10
  "types": "build/index.d.ts",
@@ -12,11 +12,11 @@
12
12
  "build/**/*"
13
13
  ],
14
14
  "dependencies": {
15
- "@livequery/client": "^2.0.7",
15
+ "@livequery/client": "^2.0.15",
16
16
  "rxjs": "^7.8.1"
17
17
  },
18
18
  "devDependencies": {
19
- "@livequery/types": "^2.0.7",
19
+ "@livequery/types": "^2.0.15",
20
20
  "@types/react": "^17.0.11",
21
21
  "typescript": "5.6.3"
22
22
  },
@@ -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
- };