@livequery/react 1.0.82 → 1.0.86

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,2 +1,3 @@
1
+ "use client";
1
2
  import { createContextFromHook } from './hooks/createContextFromHook';
2
3
  export const [useLiveQueryContext, LiveQueryContextProvider] = createContextFromHook((props) => props);
@@ -1,2 +1,2 @@
1
1
  import React, { PropsWithChildren } from 'react';
2
- export declare const createContextFromHook: <T, K>(fn: (props?: T) => K) => [() => K, ({ children, ...props }: React.PropsWithChildren<T>) => React.JSX.Element];
2
+ export declare const createContextFromHook: <T, K>(fn: ((props: T) => K) | (() => K)) => [() => K, ({ children, ...props }: React.PropsWithChildren<T>) => React.JSX.Element];
@@ -1,3 +1,4 @@
1
+ "use client";
1
2
  import React from 'react';
2
3
  import { createContext, useContext } from "react";
3
4
  export const createContextFromHook = (fn) => {
@@ -1,2 +1,3 @@
1
+ "use client";
1
2
  import { createContextFromHook } from './createContextFromHook';
2
3
  export const createStaticContext = () => createContextFromHook((props) => props.value);
@@ -1,5 +1,6 @@
1
1
  import { LivequeryBaseEntity, QueryOption } from "@livequery/types";
2
- import { CollectionOption } from "@livequery/client";
2
+ import { CollectionOption, SmartQueryItem } 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>>;
@@ -8,22 +9,28 @@ export type useCollectionDataOptions<T extends LivequeryBaseEntity = LivequeryBa
8
9
  export declare const useCollectionData: <T extends {
9
10
  id: string;
10
11
  }>(ref: string | undefined | '' | null | 0 | false, collection_options?: Partial<useCollectionDataOptions<T>>) => {
11
- items: import("@livequery/client").SmartQueryItem<T>[];
12
- loading: boolean;
13
- error: import("@livequery/types").ErrorInfo;
12
+ items: SmartQueryItem<T>[];
13
+ loading: boolean | undefined;
14
+ error: import("@livequery/types").ErrorInfo | null | undefined;
14
15
  has_more: boolean;
15
- empty: boolean;
16
- filters: Partial<QueryOption<T>>;
17
- add: (payload: Partial<T>) => Promise<{
16
+ empty: boolean | "" | 0 | null | undefined;
17
+ filters: Partial<QueryOption<T>> | {};
18
+ add: <R = {
18
19
  data: {
19
20
  item: T;
20
21
  };
21
- }>;
22
+ }>(payload: Partial<T>) => Promise<R>;
22
23
  fetch_more: () => void;
23
24
  filter: (filters: Partial<QueryOption<T>>) => void;
24
25
  reload: () => void;
25
26
  reset: () => void;
26
- trigger: <T_1>(name: string, payload?: object, trigger_document_id?: string) => Promise<T_1>;
27
- update: ({ id: update_payload_id, ...payload }: Partial<T>) => Promise<any>;
28
- $changes: import("rxjs").Subject<import("@livequery/types").UpdatedData<T>>;
27
+ trigger: <R_1>(name: string, payload?: object | undefined, trigger_document_id?: string | undefined, query?: {
28
+ [key: string]: string | number | boolean;
29
+ } | undefined) => Promise<R_1 | undefined>;
30
+ update: <R_2 = {
31
+ data: {
32
+ item: T;
33
+ };
34
+ }>({ id: update_payload_id, ...payload }: Partial<T>) => Promise<R_2 | undefined>;
35
+ $changes: Subject<import("@livequery/types").UpdatedData<T>>;
29
36
  };
@@ -1,34 +1,44 @@
1
+ "use client";
1
2
  import { useEffect, useMemo } from "react";
2
3
  import { useLiveQueryContext } from "./LiveQueryContext";
3
4
  import { useObservable } from "./useObservable";
4
5
  import { CollectionObservable } from "@livequery/client";
6
+ import { Subject } from 'rxjs';
5
7
  function assert(fn, thiss) {
6
8
  return (fn || (() => { })).bind(thiss);
7
9
  }
8
10
  export const useCollectionData = (ref, collection_options = {}) => {
9
- const { transporter } = useLiveQueryContext();
10
- const client = useMemo(() => ref && new CollectionObservable(ref, { transporter, ...collection_options }), [ref]);
11
- const { loading, has_more, error, items, options } = useObservable(client, { options: {}, items: [], has_more: false, loading: collection_options.lazy ? false : true });
11
+ const lqct = useLiveQueryContext();
12
+ const transporter = lqct;
13
+ const client = useMemo(() => ref ? new CollectionObservable(ref, { transporter, ...collection_options }) : null, [ref]);
14
+ const data = useObservable(client) || { options: {}, items: [], has_more: false, loading: false, error: null };
15
+ const { loading, has_more, error, items, options } = data;
12
16
  useEffect(() => {
13
- ref && !collection_options?.lazy && client.fetch_more();
17
+ try {
18
+ ref && !collection_options?.lazy && client?.fetch_more();
19
+ }
20
+ catch (e) {
21
+ }
14
22
  }, [ref]);
15
23
  useEffect(() => {
16
- collection_options.load_all && !loading && has_more && items.length > 0 && client.fetch_more();
24
+ // collection_options.load_all && !loading && has_more && items.length > 0 && client.fetch_more()
17
25
  }, [loading]);
18
26
  return {
19
27
  items,
20
28
  loading,
21
29
  error,
22
30
  has_more,
23
- empty: ref && !error && Object.keys(items).length == 0 && loading === false,
31
+ empty: ref && !error && items.length == 0 && loading === false,
24
32
  filters: (options || {}),
25
33
  add: assert(client?.add, client),
26
- fetch_more: assert(client?.fetch_more, client),
34
+ fetch_more: () => {
35
+ alert('Fetch more');
36
+ },
27
37
  filter: assert(client?.filter, client),
28
38
  reload: assert(client?.reload, client),
29
39
  reset: assert(client?.reset, client),
30
40
  trigger: assert(client?.trigger, client),
31
41
  update: assert(client?.update, client),
32
- $changes: client?.$changes
42
+ $changes: client?.$changes || new Subject()
33
43
  };
34
44
  };
@@ -7,8 +7,8 @@ export declare const useDocumentData: <T extends {
7
7
  id: string;
8
8
  }>(ref: string | undefined | '' | null | 0 | false, options?: useDocumentDataOptions) => {
9
9
  item: import("@livequery/client").SmartQueryItem<T>;
10
- loading: boolean;
11
- error: import("@livequery/types").ErrorInfo;
10
+ loading: boolean | undefined;
11
+ error: import("@livequery/types").ErrorInfo | null | undefined;
12
12
  reload: () => void;
13
13
  $changes: import("rxjs").Subject<import("@livequery/types").UpdatedData<T>>;
14
14
  };
@@ -1,3 +1,4 @@
1
+ "use client";
1
2
  import { useCollectionData } from "./useCollectionData";
2
3
  export const useDocumentData = (ref, options) => {
3
4
  const { items, loading, error, reload, $changes } = useCollectionData(ref, options);
@@ -1,8 +1,8 @@
1
1
  type PromiseType<T> = T extends PromiseLike<infer U> ? U : T;
2
2
  export declare const useMonitor: <T extends (...args: any) => any>(fn: T) => {
3
3
  excute: T;
4
- loading: boolean;
5
- data: PromiseType<ReturnType<T>>;
4
+ loading: boolean | undefined;
5
+ data: PromiseType<ReturnType<T>> | null | undefined;
6
6
  error: any;
7
7
  vaild: boolean;
8
8
  };
@@ -1,3 +1,4 @@
1
+ "use client";
1
2
  import { useState } from "react";
2
3
  export const useMonitor = (fn) => {
3
4
  const [{ error, data, loading }, update] = useState({});
@@ -1,2 +1,2 @@
1
1
  import { Observable } from 'rxjs';
2
- export declare const useObservable: <T>(o: Observable<T>, default_value: T) => T;
2
+ export declare const useObservable: <T>(o: Observable<T> | null, default_value?: T | undefined) => T | undefined;
@@ -1,15 +1,14 @@
1
+ "use client";
1
2
  import { useEffect, useState } from 'react';
2
3
  export const useObservable = (o, default_value) => {
3
- let mounting = true;
4
- const [s, ss] = useState(default_value);
4
+ const [s, ss] = useState();
5
5
  useEffect(() => {
6
- if (!o)
7
- return;
8
- const subcription = o?.subscribe(d => ss({ ...d }));
9
- return () => {
10
- subcription?.unsubscribe();
11
- mounting = false;
12
- };
6
+ if (o) {
7
+ const subcription = o.subscribe(d => ss(d));
8
+ return () => {
9
+ subcription.unsubscribe();
10
+ };
11
+ }
13
12
  }, [o]);
14
- return s;
13
+ return s || default_value;
15
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.82",
7
+ "version": "1.0.86",
8
8
  "description": "",
9
9
  "main": "build/index.js",
10
10
  "types": "build/index.d.ts",
@@ -12,12 +12,12 @@
12
12
  "build/**/*"
13
13
  ],
14
14
  "dependencies": {
15
- "@livequery/client": "^1.0.29"
15
+ "@livequery/client": "^1.0.53"
16
16
  },
17
17
  "devDependencies": {
18
- "react": "^17.0.2",
19
- "@livequery/types": "^1.0.34",
20
- "@types/react": "^17.0.11"
18
+ "@livequery/types": "^1.0.82",
19
+ "@types/react": "^17.0.11",
20
+ "react": "^17.0.2"
21
21
  },
22
22
  "peerDependencies": {
23
23
  "react": "^17.0.2"
@@ -28,5 +28,13 @@
28
28
  "deploy": "yarn build; git add .; git commit -m \"Update\"; git push origin master; npm publish --access public"
29
29
  },
30
30
  "author": "",
31
- "license": "ISC"
32
- }
31
+ "license": "ISC",
32
+ "exports": {
33
+ ".": {
34
+ "import": {
35
+ "types": "./build/src/index.d.ts",
36
+ "default": "./build/src/index.js"
37
+ }
38
+ }
39
+ }
40
+ }