@livequery/react 1.0.85 → 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.
- package/build/hooks/createContextFromHook.d.ts +1 -1
- package/build/useCollectionData.d.ts +18 -11
- package/build/useCollectionData.js +17 -8
- package/build/useDocumentData.d.ts +2 -2
- package/build/useMonitor.d.ts +2 -2
- package/build/useObservable.d.ts +1 -1
- package/build/useObservable.js +8 -10
- package/package.json +14 -6
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import React, { PropsWithChildren } from 'react';
|
|
2
|
-
export declare const createContextFromHook: <T, K>(fn: (props
|
|
2
|
+
export declare const createContextFromHook: <T, K>(fn: ((props: T) => K) | (() => K)) => [() => K, ({ children, ...props }: React.PropsWithChildren<T>) => React.JSX.Element];
|
|
@@ -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:
|
|
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:
|
|
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: <
|
|
27
|
-
|
|
28
|
-
|
|
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
|
};
|
|
@@ -3,33 +3,42 @@ import { useEffect, useMemo } from "react";
|
|
|
3
3
|
import { useLiveQueryContext } from "./LiveQueryContext";
|
|
4
4
|
import { useObservable } from "./useObservable";
|
|
5
5
|
import { CollectionObservable } from "@livequery/client";
|
|
6
|
+
import { Subject } from 'rxjs';
|
|
6
7
|
function assert(fn, thiss) {
|
|
7
8
|
return (fn || (() => { })).bind(thiss);
|
|
8
9
|
}
|
|
9
10
|
export const useCollectionData = (ref, collection_options = {}) => {
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
const
|
|
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;
|
|
13
16
|
useEffect(() => {
|
|
14
|
-
|
|
17
|
+
try {
|
|
18
|
+
ref && !collection_options?.lazy && client?.fetch_more();
|
|
19
|
+
}
|
|
20
|
+
catch (e) {
|
|
21
|
+
}
|
|
15
22
|
}, [ref]);
|
|
16
23
|
useEffect(() => {
|
|
17
|
-
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()
|
|
18
25
|
}, [loading]);
|
|
19
26
|
return {
|
|
20
27
|
items,
|
|
21
28
|
loading,
|
|
22
29
|
error,
|
|
23
30
|
has_more,
|
|
24
|
-
empty: ref && !error &&
|
|
31
|
+
empty: ref && !error && items.length == 0 && loading === false,
|
|
25
32
|
filters: (options || {}),
|
|
26
33
|
add: assert(client?.add, client),
|
|
27
|
-
fetch_more:
|
|
34
|
+
fetch_more: () => {
|
|
35
|
+
alert('Fetch more');
|
|
36
|
+
},
|
|
28
37
|
filter: assert(client?.filter, client),
|
|
29
38
|
reload: assert(client?.reload, client),
|
|
30
39
|
reset: assert(client?.reset, client),
|
|
31
40
|
trigger: assert(client?.trigger, client),
|
|
32
41
|
update: assert(client?.update, client),
|
|
33
|
-
$changes: client?.$changes
|
|
42
|
+
$changes: client?.$changes || new Subject()
|
|
34
43
|
};
|
|
35
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
|
};
|
package/build/useMonitor.d.ts
CHANGED
|
@@ -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
|
};
|
package/build/useObservable.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { Observable } from 'rxjs';
|
|
2
|
-
export declare const useObservable: <T>(o: Observable<T
|
|
2
|
+
export declare const useObservable: <T>(o: Observable<T> | null, default_value?: T | undefined) => T | undefined;
|
package/build/useObservable.js
CHANGED
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { useEffect, useState } from 'react';
|
|
3
3
|
export const useObservable = (o, default_value) => {
|
|
4
|
-
|
|
5
|
-
const [s, ss] = useState(default_value);
|
|
4
|
+
const [s, ss] = useState();
|
|
6
5
|
useEffect(() => {
|
|
7
|
-
if (
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
};
|
|
6
|
+
if (o) {
|
|
7
|
+
const subcription = o.subscribe(d => ss(d));
|
|
8
|
+
return () => {
|
|
9
|
+
subcription.unsubscribe();
|
|
10
|
+
};
|
|
11
|
+
}
|
|
14
12
|
}, [o]);
|
|
15
|
-
return s;
|
|
13
|
+
return s || default_value;
|
|
16
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.
|
|
7
|
+
"version": "1.0.86",
|
|
8
8
|
"description": "",
|
|
9
9
|
"main": "build/index.js",
|
|
10
10
|
"types": "build/index.d.ts",
|
|
@@ -15,9 +15,9 @@
|
|
|
15
15
|
"@livequery/client": "^1.0.53"
|
|
16
16
|
},
|
|
17
17
|
"devDependencies": {
|
|
18
|
-
"
|
|
19
|
-
"@
|
|
20
|
-
"
|
|
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
|
+
}
|