@livequery/react 1.0.97 → 1.0.99

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,6 @@
1
1
  import { LivequeryBaseEntity, QueryOption } from "@livequery/types";
2
2
  import { CollectionObservable, CollectionOption, CollectionStream } from "@livequery/client";
3
+ import { Subject } from 'rxjs';
3
4
  export type useCollectionDataOptions<T extends LivequeryBaseEntity = LivequeryBaseEntity> = CollectionOption<T> & {
4
5
  lazy?: boolean;
5
6
  filters: Partial<QueryOption<T>>;
@@ -9,4 +10,19 @@ export type CollectionData<T extends LivequeryBaseEntity> = (CollectionStream<T>
9
10
  empty: boolean;
10
11
  });
11
12
  export type CollectionRef = string | undefined | '' | null | false;
12
- 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>>) => CollectionData<T> | {
14
+ error: undefined;
15
+ $changes: Subject<unknown>;
16
+ add: () => void;
17
+ empty: boolean;
18
+ filter: () => void;
19
+ fetch_more: () => void;
20
+ filters: {};
21
+ has_more: boolean;
22
+ items: never[];
23
+ reload: () => void;
24
+ reset: () => void;
25
+ trigger: () => void;
26
+ update: () => void;
27
+ loading: boolean;
28
+ };
@@ -1,15 +1,15 @@
1
1
  "use client";
2
- import { useEffect, useMemo } from "react";
2
+ import { useEffect, useRef, useState } from "react";
3
3
  import { useLiveQueryContext } from "./LiveQueryContext.js";
4
- import { useObservable } from "./useObservable.js";
5
4
  import { CollectionObservable } from "@livequery/client";
6
5
  import { Subject } from 'rxjs';
7
6
  function assert(fn, thiss) {
8
7
  return (fn || (() => { })).bind(thiss);
9
8
  }
10
9
  export const useCollectionData = (ref, collection_options = {}) => {
11
- if (typeof window == 'undefined') {
10
+ if (typeof window == 'undefined' || !ref) {
12
11
  return {
12
+ error: undefined,
13
13
  $changes: new Subject(),
14
14
  add: (() => { }),
15
15
  empty: false,
@@ -26,28 +26,28 @@ export const useCollectionData = (ref, collection_options = {}) => {
26
26
  };
27
27
  }
28
28
  const { transporter } = useLiveQueryContext();
29
- const client = useMemo(() => new CollectionObservable(ref, { transporter, ...collection_options }), [ref]);
30
- const stream = useObservable(client, {
29
+ const [stream, ss] = useState({
31
30
  filters: {},
32
31
  items: [],
33
32
  has_more: false,
34
33
  loading: collection_options.lazy ? false : true,
35
34
  error: undefined
36
35
  });
37
- const { loading, has_more, items, error } = stream;
36
+ const collection_ref = useRef();
38
37
  useEffect(() => {
39
- try {
40
- ref && !collection_options?.lazy && client?.fetch_more();
41
- }
42
- catch (e) {
43
- }
38
+ const collection = collection_ref.current = new CollectionObservable(ref, { transporter, ...collection_options });
39
+ const subscription = collection.subscribe(data => {
40
+ collection_options.load_all && data.loading == false && data.has_more && collection?.fetch_more();
41
+ ss(data);
42
+ });
43
+ !collection_options?.lazy && collection?.fetch_more();
44
+ return () => subscription.unsubscribe();
44
45
  }, [ref]);
45
- useEffect(() => {
46
- collection_options.load_all && !loading && has_more && items.length > 0 && client?.fetch_more();
47
- }, [loading]);
48
- const empty = !!(ref && !error && items.length == 0 && !loading);
46
+ const client = collection_ref.current;
47
+ const empty = !!(stream.loading && stream.items.length == 0);
49
48
  const result = {
50
49
  ...stream,
50
+ error: stream.error,
51
51
  loading: !!stream.loading,
52
52
  empty,
53
53
  add: assert(client?.add, client),
@@ -57,7 +57,7 @@ export const useCollectionData = (ref, collection_options = {}) => {
57
57
  reset: assert(client?.reset, client),
58
58
  trigger: assert(client?.trigger, client),
59
59
  update: assert(client?.update, client),
60
- $changes: client?.$changes
60
+ $changes: client?.$changes || new Subject()
61
61
  };
62
62
  return result;
63
63
  };
@@ -9,6 +9,6 @@ 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
- $changes: import("rxjs").Subject<import("@livequery/types").UpdatedData<T>>;
12
+ reload: (() => void) | (() => void);
13
+ $changes: import("rxjs").Subject<unknown> | import("rxjs").Subject<import("@livequery/types").UpdatedData<T>>;
14
14
  };
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.97",
7
+ "version": "1.0.99",
8
8
  "description": "",
9
9
  "main": "build/index.js",
10
10
  "types": "build/index.d.ts",
@@ -1,2 +0,0 @@
1
- import { Observable } from 'rxjs';
2
- export declare const useObservable: <T>(o: Observable<T> | null, default_value: T) => T;
@@ -1,17 +0,0 @@
1
- "use client";
2
- import { useEffect, useState } from 'react';
3
- export const useObservable = (o, default_value) => {
4
- const [s, ss] = useState();
5
- useEffect(() => {
6
- if (o) {
7
- const subcription = o.subscribe(d => ss({ ...d }));
8
- return () => {
9
- subcription.unsubscribe();
10
- };
11
- }
12
- else {
13
- ss(undefined);
14
- }
15
- }, [o]);
16
- return s || default_value;
17
- };