@livequery/react 1.0.105 → 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>>) => CollectionData<T>;
13
+ export declare const useCollectionData: <T extends LivequeryBaseEntity>(ref: CollectionRef, collection_options?: Partial<useCollectionDataOptions<T>> & {
14
+ vkl?: any;
15
+ }) => CollectionData<T>;
@@ -1,41 +1,46 @@
1
1
  "use client";
2
- import { useEffect, useRef, useState } from "react";
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 [stream, ss] = useState({
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
- loading: collection_options.lazy ? false : !!ref,
16
- error: undefined
17
+ items: [],
18
+ loaded: false,
19
+ n: 0
17
20
  });
18
- const [n, sn] = useState(0);
19
- const collection_ref = useRef();
20
- useEffect(() => {
21
- if (!ref || typeof window == 'undefined')
22
- return;
23
- const collection = collection_ref.current = new CollectionObservable(ref, { transporter, ...collection_options });
24
- const subscription = collection.subscribe(data => {
25
- collection_options.load_all && data.loading == false && data.has_more && collection?.fetch_more();
26
- ss(data);
27
- sn(Math.random());
21
+ const DEBUG = ref?.includes('webhooks');
22
+ const client = useSyncMemo(() => {
23
+ const client = new CollectionObservable(ref, { transporter, ...collection_options });
24
+ n.current = 0;
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
+ }
28
33
  });
29
- !collection_options?.lazy && collection?.fetch_more();
30
- return () => subscription.unsubscribe();
31
- }, [ref, collection_options?.lazy]);
32
- const client = collection_ref.current;
33
- const loaded = n > 0;
34
- const empty = loaded && !stream.loading && stream.items.length == 0;
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;
35
40
  const result = {
36
41
  ...stream,
37
42
  error: stream.error,
38
- loading: !!stream.loading,
43
+ loading: !!stream.loading || (!collection_options?.lazy && !!ref && n.current == 0),
39
44
  empty,
40
45
  add: assert(client?.add, client),
41
46
  fetch_more: assert(client?.fetch_more, client),
@@ -45,7 +50,6 @@ export const useCollectionData = (ref, collection_options = {}) => {
45
50
  trigger: assert(client?.trigger, client),
46
51
  update: assert(client?.update, client),
47
52
  $changes: client?.$changes || new Subject(),
48
- loaded
49
53
  };
50
54
  return result;
51
55
  };
@@ -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
  };
@@ -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, reload, $changes } = useCollectionData(ref, options);
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
  };
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "url": "https://github.com/livequery/react"
5
5
  },
6
6
  "type": "module",
7
- "version": "1.0.105",
7
+ "version": "1.0.107",
8
8
  "description": "",
9
9
  "main": "build/index.js",
10
10
  "types": "build/index.d.ts",