@livequery/react 1.0.104 → 1.0.106

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.
@@ -7,6 +7,7 @@ export type useCollectionDataOptions<T extends LivequeryBaseEntity = LivequeryBa
7
7
  };
8
8
  export type CollectionData<T extends LivequeryBaseEntity> = (CollectionStream<T> & Pick<CollectionObservable<T>, 'add' | 'fetch_more' | 'filter' | 'reload' | 'reset' | 'trigger' | 'update' | '$changes'> & {
9
9
  empty: boolean;
10
+ loaded: boolean;
10
11
  });
11
12
  export type CollectionRef = string | undefined | '' | null | false;
12
13
  export declare const useCollectionData: <T extends LivequeryBaseEntity>(ref: CollectionRef, collection_options?: Partial<useCollectionDataOptions<T>>) => CollectionData<T>;
@@ -13,28 +13,30 @@ export const useCollectionData = (ref, collection_options = {}) => {
13
13
  items: [],
14
14
  has_more: false,
15
15
  loading: collection_options.lazy ? false : !!ref,
16
- error: undefined
16
+ error: undefined,
17
+ loaded: false
17
18
  });
18
- const [n, sn] = useState(0);
19
+ const n = useRef(0);
19
20
  const collection_ref = useRef();
20
21
  useEffect(() => {
21
22
  if (!ref || typeof window == 'undefined')
22
23
  return;
23
24
  const collection = collection_ref.current = new CollectionObservable(ref, { transporter, ...collection_options });
25
+ n.current = 0;
24
26
  const subscription = collection.subscribe(data => {
25
27
  collection_options.load_all && data.loading == false && data.has_more && collection?.fetch_more();
26
- ss(data);
27
- sn(Math.random());
28
+ n.current++;
29
+ ss({ ...data, loaded: true });
28
30
  });
29
31
  !collection_options?.lazy && collection?.fetch_more();
30
32
  return () => subscription.unsubscribe();
31
33
  }, [ref, collection_options?.lazy]);
32
34
  const client = collection_ref.current;
33
- const empty = n > 0 && !stream.loading && stream.items.length == 0;
35
+ const empty = stream.loaded && !stream.loading && stream.items.length == 0;
34
36
  const result = {
35
37
  ...stream,
36
38
  error: stream.error,
37
- loading: !!stream.loading,
39
+ loading: !!stream.loading || (!collection_options?.lazy && !!ref && n.current == 0),
38
40
  empty,
39
41
  add: assert(client?.add, client),
40
42
  fetch_more: assert(client?.fetch_more, client),
@@ -43,7 +45,7 @@ export const useCollectionData = (ref, collection_options = {}) => {
43
45
  reset: assert(client?.reset, client),
44
46
  trigger: assert(client?.trigger, client),
45
47
  update: assert(client?.update, client),
46
- $changes: client?.$changes || new Subject()
48
+ $changes: client?.$changes || new Subject(),
47
49
  };
48
50
  return result;
49
51
  };
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.104",
7
+ "version": "1.0.106",
8
8
  "description": "",
9
9
  "main": "build/index.js",
10
10
  "types": "build/index.d.ts",