@livequery/react 1.0.109 → 1.0.111

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,14 +1,11 @@
1
- import { useEffect, useRef } from "react";
1
+ import { useRef } from "react";
2
2
  export const useSyncMemo = (fn, deps) => {
3
- const ref = useRef({ value: undefined, deps: [], n: 0 });
3
+ const ref = useRef({ value: undefined, deps: [], n: 0, cleaner: () => { } });
4
4
  const outdated = ref.current.n == 0 || deps.some((e, i) => e != ref.current.deps[i]);
5
- const [value, cleaner] = outdated ? fn() : [
5
+ const [value, cleaner] = outdated ? (ref.current.cleaner(), fn()) : [
6
6
  ref.current.value,
7
7
  () => { }
8
8
  ];
9
- ref.current = { deps, value, n: ref.current.n + 1 };
10
- useEffect(() => {
11
- return cleaner;
12
- }, [ref.current.value]);
9
+ ref.current = { deps, value, n: ref.current.n + 1, cleaner };
13
10
  return value;
14
11
  };
@@ -5,11 +5,12 @@ export type useCollectionDataOptions<T extends LivequeryBaseEntity = LivequeryBa
5
5
  filters: Partial<QueryOption<T>>;
6
6
  load_all: boolean;
7
7
  };
8
+ export type CollectionRef = string | undefined | '' | null | false;
8
9
  export type CollectionData<T extends LivequeryBaseEntity> = (CollectionStream<T> & Pick<CollectionObservable<T>, 'add' | 'fetch_more' | 'filter' | 'reload' | 'reset' | 'trigger' | 'update' | '$changes'> & {
9
10
  empty: boolean;
10
11
  loaded: boolean;
12
+ ref: CollectionRef;
11
13
  });
12
- export type CollectionRef = string | undefined | '' | null | false;
13
14
  export declare const useCollectionData: <T extends LivequeryBaseEntity>(ref: CollectionRef, collection_options?: Partial<useCollectionDataOptions<T>> & {
14
15
  vkl?: any;
15
16
  }) => CollectionData<T>;
@@ -11,14 +11,7 @@ export const useCollectionData = (ref, collection_options = {}) => {
11
11
  const { transporter } = useLiveQueryContext();
12
12
  const n = useRef(0);
13
13
  const lazy = collection_options.lazy;
14
- const [stream, set_stream] = useState({
15
- filters: {},
16
- has_more: false,
17
- items: [],
18
- loaded: false,
19
- n: 0,
20
- loading: lazy ? false : !!ref
21
- });
14
+ const [version, set_version] = useState(0);
22
15
  const client = useSyncMemo(() => {
23
16
  const client = new CollectionObservable(ref, { transporter, ...collection_options });
24
17
  n.current = 0;
@@ -28,7 +21,7 @@ export const useCollectionData = (ref, collection_options = {}) => {
28
21
  }
29
22
  else {
30
23
  n.current++;
31
- set_stream({ ...data, loaded: true, n: n.current });
24
+ set_version(Math.random());
32
25
  }
33
26
  });
34
27
  if (!lazy) {
@@ -36,11 +29,11 @@ export const useCollectionData = (ref, collection_options = {}) => {
36
29
  }
37
30
  return [client, () => subscription.unsubscribe()];
38
31
  }, [ref]);
39
- const empty = !client.value.loading && client.value.items.length == 0 && stream.loaded;
32
+ const loaded = n.current > 0;
33
+ const empty = loaded && !client.value.loading && client.value.items.length == 0;
40
34
  const result = {
41
- ...stream,
42
- error: stream.error,
43
- loading: !!stream.loading || (!collection_options?.lazy && !!ref && n.current == 0),
35
+ ...client.value,
36
+ loaded,
44
37
  empty,
45
38
  add: assert(client?.add, client),
46
39
  fetch_more: assert(client?.fetch_more, client),
@@ -50,6 +43,7 @@ export const useCollectionData = (ref, collection_options = {}) => {
50
43
  trigger: assert(client?.trigger, client),
51
44
  update: assert(client?.update, client),
52
45
  $changes: client?.$changes || new Subject(),
46
+ ref
53
47
  };
54
48
  return result;
55
49
  };
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.109",
7
+ "version": "1.0.111",
8
8
  "description": "",
9
9
  "main": "build/index.js",
10
10
  "types": "build/index.d.ts",