@livequery/react 1.0.97 → 1.0.100
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/useCollectionData.js +16 -32
- package/package.json +1 -1
- package/build/useObservable.d.ts +0 -2
- package/build/useObservable.js +0 -17
|
@@ -1,53 +1,37 @@
|
|
|
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";
|
|
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') {
|
|
12
|
-
return {
|
|
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
|
-
}
|
|
28
10
|
const { transporter } = useLiveQueryContext();
|
|
29
|
-
const
|
|
30
|
-
const stream = useObservable(client, {
|
|
11
|
+
const [stream, ss] = useState({
|
|
31
12
|
filters: {},
|
|
32
13
|
items: [],
|
|
33
14
|
has_more: false,
|
|
34
15
|
loading: collection_options.lazy ? false : true,
|
|
35
16
|
error: undefined
|
|
36
17
|
});
|
|
37
|
-
const
|
|
18
|
+
const collection_ref = useRef();
|
|
38
19
|
useEffect(() => {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
|
|
20
|
+
if (!ref || typeof window == 'undefined')
|
|
21
|
+
return;
|
|
22
|
+
const collection = collection_ref.current = new CollectionObservable(ref, { transporter, ...collection_options });
|
|
23
|
+
const subscription = collection.subscribe(data => {
|
|
24
|
+
collection_options.load_all && data.loading == false && data.has_more && collection?.fetch_more();
|
|
25
|
+
ss(data);
|
|
26
|
+
});
|
|
27
|
+
!collection_options?.lazy && collection?.fetch_more();
|
|
28
|
+
return () => subscription.unsubscribe();
|
|
44
29
|
}, [ref]);
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}, [loading]);
|
|
48
|
-
const empty = !!(ref && !error && items.length == 0 && !loading);
|
|
30
|
+
const client = collection_ref.current;
|
|
31
|
+
const empty = !!(stream.loading && stream.items.length == 0);
|
|
49
32
|
const result = {
|
|
50
33
|
...stream,
|
|
34
|
+
error: stream.error,
|
|
51
35
|
loading: !!stream.loading,
|
|
52
36
|
empty,
|
|
53
37
|
add: assert(client?.add, client),
|
|
@@ -57,7 +41,7 @@ export const useCollectionData = (ref, collection_options = {}) => {
|
|
|
57
41
|
reset: assert(client?.reset, client),
|
|
58
42
|
trigger: assert(client?.trigger, client),
|
|
59
43
|
update: assert(client?.update, client),
|
|
60
|
-
$changes: client?.$changes
|
|
44
|
+
$changes: client?.$changes || new Subject()
|
|
61
45
|
};
|
|
62
46
|
return result;
|
|
63
47
|
};
|
package/package.json
CHANGED
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
|
-
};
|