@livequery/react 1.0.96 → 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,36 +1,28 @@
|
|
|
1
1
|
import { LivequeryBaseEntity, QueryOption } from "@livequery/types";
|
|
2
|
-
import { CollectionOption } from "@livequery/client";
|
|
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>>;
|
|
6
7
|
load_all: boolean;
|
|
7
8
|
};
|
|
8
|
-
export
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
export type CollectionData<T extends LivequeryBaseEntity> = (CollectionStream<T> & Pick<CollectionObservable<T>, 'add' | 'fetch_more' | 'filter' | 'reload' | 'reset' | 'trigger' | 'update' | '$changes'> & {
|
|
10
|
+
empty: boolean;
|
|
11
|
+
});
|
|
12
|
+
export type CollectionRef = string | undefined | '' | null | false;
|
|
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;
|
|
13
17
|
empty: boolean;
|
|
14
|
-
|
|
15
|
-
data: {
|
|
16
|
-
item: T;
|
|
17
|
-
};
|
|
18
|
-
}>(payload: Partial<T>) => Promise<R>;
|
|
18
|
+
filter: () => void;
|
|
19
19
|
fetch_more: () => void;
|
|
20
|
-
|
|
20
|
+
filters: {};
|
|
21
|
+
has_more: boolean;
|
|
22
|
+
items: never[];
|
|
21
23
|
reload: () => void;
|
|
22
24
|
reset: () => void;
|
|
23
|
-
trigger:
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
update: <R_2 = {
|
|
27
|
-
data: {
|
|
28
|
-
item: T;
|
|
29
|
-
};
|
|
30
|
-
}>({ id: update_payload_id, ...payload }: Partial<T>) => Promise<R_2 | undefined>;
|
|
31
|
-
$changes: import("rxjs").Subject<import("@livequery/types").UpdatedData<T>>;
|
|
32
|
-
items: import("@livequery/client").SmartQueryItem<T>[];
|
|
33
|
-
error?: import("@livequery/types").ErrorInfo | undefined;
|
|
34
|
-
has_more: boolean;
|
|
35
|
-
options: Partial<QueryOption<T>>;
|
|
25
|
+
trigger: () => void;
|
|
26
|
+
update: () => void;
|
|
27
|
+
loading: boolean;
|
|
36
28
|
};
|
|
@@ -1,36 +1,53 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { useEffect,
|
|
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";
|
|
5
|
+
import { Subject } from 'rxjs';
|
|
6
6
|
function assert(fn, thiss) {
|
|
7
7
|
return (fn || (() => { })).bind(thiss);
|
|
8
8
|
}
|
|
9
9
|
export const useCollectionData = (ref, collection_options = {}) => {
|
|
10
|
+
if (typeof window == 'undefined' || !ref) {
|
|
11
|
+
return {
|
|
12
|
+
error: undefined,
|
|
13
|
+
$changes: new Subject(),
|
|
14
|
+
add: (() => { }),
|
|
15
|
+
empty: false,
|
|
16
|
+
filter: (() => { }),
|
|
17
|
+
fetch_more: (() => { }),
|
|
18
|
+
filters: {},
|
|
19
|
+
has_more: false,
|
|
20
|
+
items: [],
|
|
21
|
+
reload: (() => { }),
|
|
22
|
+
reset: (() => { }),
|
|
23
|
+
trigger: (() => { }),
|
|
24
|
+
update: (() => { }),
|
|
25
|
+
loading: false
|
|
26
|
+
};
|
|
27
|
+
}
|
|
10
28
|
const { transporter } = useLiveQueryContext();
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
options: {},
|
|
29
|
+
const [stream, ss] = useState({
|
|
30
|
+
filters: {},
|
|
14
31
|
items: [],
|
|
15
32
|
has_more: false,
|
|
16
33
|
loading: collection_options.lazy ? false : true,
|
|
17
34
|
error: undefined
|
|
18
35
|
});
|
|
19
|
-
const
|
|
36
|
+
const collection_ref = useRef();
|
|
20
37
|
useEffect(() => {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
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();
|
|
26
45
|
}, [ref]);
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const empty = !!(ref && !error && items.length == 0 && !loading);
|
|
31
|
-
return {
|
|
46
|
+
const client = collection_ref.current;
|
|
47
|
+
const empty = !!(stream.loading && stream.items.length == 0);
|
|
48
|
+
const result = {
|
|
32
49
|
...stream,
|
|
33
|
-
|
|
50
|
+
error: stream.error,
|
|
34
51
|
loading: !!stream.loading,
|
|
35
52
|
empty,
|
|
36
53
|
add: assert(client?.add, client),
|
|
@@ -40,6 +57,7 @@ export const useCollectionData = (ref, collection_options = {}) => {
|
|
|
40
57
|
reset: assert(client?.reset, client),
|
|
41
58
|
trigger: assert(client?.trigger, client),
|
|
42
59
|
update: assert(client?.update, client),
|
|
43
|
-
$changes: client?.$changes
|
|
60
|
+
$changes: client?.$changes || new Subject()
|
|
44
61
|
};
|
|
62
|
+
return result;
|
|
45
63
|
};
|
|
@@ -7,8 +7,8 @@ export declare const useDocumentData: <T extends {
|
|
|
7
7
|
id: string;
|
|
8
8
|
}>(ref: string | undefined | '' | null | false, options?: useDocumentDataOptions) => {
|
|
9
9
|
item: import("@livequery/client").SmartQueryItem<T>;
|
|
10
|
-
loading: boolean;
|
|
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.
|
|
7
|
+
"version": "1.0.99",
|
|
8
8
|
"description": "",
|
|
9
9
|
"main": "build/index.js",
|
|
10
10
|
"types": "build/index.d.ts",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"build/**/*"
|
|
13
13
|
],
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@livequery/client": "^1.0.
|
|
15
|
+
"@livequery/client": "^1.0.93"
|
|
16
16
|
},
|
|
17
17
|
"devDependencies": {
|
|
18
18
|
"@livequery/types": "^1.0.82",
|
package/build/useObservable.d.ts
DELETED
package/build/useObservable.js
DELETED
|
@@ -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
|
-
};
|