@livequery/react 2.0.1 → 2.0.6

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.
@@ -8,8 +8,13 @@ export type useCollectionDataOptions<T extends LivequeryBaseEntity = LivequeryBa
8
8
  export type CollectionRef = string | undefined | '' | null | false;
9
9
  export type CollectionData<T extends LivequeryBaseEntity> = (CollectionStream<T> & Pick<CollectionObservable<T>, 'add' | 'fetch_more' | 'fetch_prev' | 'reset' | 'trigger' | 'update' | '$changes' | 'filter'> & {
10
10
  empty: boolean;
11
- loaded: boolean;
12
11
  ref: CollectionRef;
13
12
  filters: CollectionStream<T>['options'];
14
13
  });
15
- export declare const useCollectionData: <T extends LivequeryBaseEntity>(ref: CollectionRef, collection_options?: Partial<useCollectionDataOptions<T>>) => CollectionData<T>;
14
+ export declare const useCollectionData: <T extends LivequeryBaseEntity>(ref: CollectionRef, collection_options?: Partial<useCollectionDataOptions<T>>) => CollectionStream<T> & Pick<CollectionObservable<T>, "add" | "fetch_more" | "fetch_prev" | "reset" | "trigger" | "update" | "$changes" | "filter"> & {
15
+ empty: boolean;
16
+ ref: CollectionRef;
17
+ filters: Partial<QueryOption<T>>;
18
+ } & {
19
+ state: number;
20
+ };
@@ -1,57 +1,49 @@
1
1
  "use client";
2
- import { useEffect, useRef, useState } from "react";
2
+ import { useEffect, useMemo, 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";
7
6
  function assert(fn, thiss) {
8
7
  return (fn || (() => { })).bind(thiss);
9
8
  }
10
9
  export const useCollectionData = (ref, collection_options = {}) => {
11
10
  const { transporter } = useLiveQueryContext();
12
- const n = useRef(0);
13
- const lazy = collection_options.lazy;
14
- const [, set_version] = useState(0);
15
- const client = useSyncMemo(() => {
16
- n.current = 0;
17
- const client = new CollectionObservable(ref, { transporter, ...collection_options });
18
- const a = client.$changes.subscribe(changes => {
19
- n.current++;
20
- });
21
- const subscription = client.subscribe(data => {
22
- if (!lazy && collection_options.load_all && data.paging.has?.next) {
11
+ const client = useMemo(() => new CollectionObservable(ref, { transporter, ...collection_options }), [ref]);
12
+ const [$, set_$] = useState(client.getValue());
13
+ const [state, set_state] = useState(0);
14
+ // Sync stream effect
15
+ useEffect(() => {
16
+ if (collection_options.lazy)
17
+ return;
18
+ client.fetch_more();
19
+ const s = client.subscribe(data => {
20
+ if (collection_options.load_all && data.paging.has?.next) {
23
21
  client.fetch_more();
24
22
  }
25
23
  else {
26
- set_version(Math.random());
24
+ set_$(data);
25
+ set_state(Date.now());
27
26
  }
28
27
  });
29
- return [client, () => {
30
- subscription.unsubscribe();
31
- a.unsubscribe();
32
- }];
33
- }, [ref]);
34
- const loaded = n.current > 0;
35
- const $ = client.$.getValue();
36
- const empty = loaded && !$.loading && $.items.length == 0;
28
+ return () => {
29
+ s.unsubscribe();
30
+ };
31
+ }, [client, set_$, collection_options.lazy, set_state]);
32
+ const empty = !!(!$.loading && $.items.length == 0);
37
33
  const result = {
38
34
  ...$,
39
- loaded,
35
+ state,
40
36
  empty,
41
37
  filters: $.options,
42
38
  add: assert(client?.add, client),
43
39
  fetch_more: assert(client?.fetch_more, client),
44
40
  fetch_prev: assert(client?.fetch_prev, client),
45
41
  filter: assert(client?.filter, client),
46
- // fetch_around_cursor: assert(client?.fetch_around_cursor, client),
47
42
  reset: assert(client?.reset, client),
48
43
  trigger: assert(client?.trigger, client),
49
44
  update: assert(client?.update, client),
50
45
  $changes: client?.$changes || new Subject(),
51
46
  ref
52
47
  };
53
- useEffect(() => {
54
- !lazy && !loaded && client.fetch_more();
55
- }, [lazy]);
56
48
  return result;
57
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": "2.0.1",
7
+ "version": "2.0.6",
8
8
  "description": "",
9
9
  "main": "build/index.js",
10
10
  "types": "build/index.d.ts",