@livequery/react 2.0.139 → 2.0.141
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/README.md +1 -1
- package/dist/useCollection.d.ts.map +1 -1
- package/dist/useCollection.js +3 -1
- package/dist/useCollection.js.map +1 -1
- package/dist/useObservable.d.ts +5 -4
- package/dist/useObservable.d.ts.map +1 -1
- package/dist/useObservable.js +20 -8
- package/dist/useObservable.js.map +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -97,7 +97,7 @@ The hook must be used under a matching provider. If it is called outside the pro
|
|
|
97
97
|
|
|
98
98
|
## `useCollection`
|
|
99
99
|
|
|
100
|
-
`useCollection<T>(ref, options)` creates
|
|
100
|
+
`useCollection<T>(ref, options)` creates a `LivequeryCollection<T>` for the current `ref`, initializes it when `ref` is truthy, and returns the collection instance. When `ref` changes, a fresh collection is created — its state is reset and reloaded for the new ref.
|
|
101
101
|
|
|
102
102
|
Use it when a component needs the full collection API: reactive state plus methods such as querying or mutations.
|
|
103
103
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useCollection.d.ts","sourceRoot":"","sources":["../src/useCollection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,KAAK,GAAG,EAAE,KAAK,0BAA0B,EAAE,MAAM,mBAAmB,CAAA;AAMlG,eAAO,MAAM,aAAa,GAAI,CAAC,SAAS,GAAG,EAAE,KAAK,MAAM,GAAG,SAAS,GAAG,EAAE,GAAG,IAAI,GAAG,KAAK,EAAE,UAAS,OAAO,CAAC,0BAA0B,CAAC,CAAC,CAAC,CAAM,
|
|
1
|
+
{"version":3,"file":"useCollection.d.ts","sourceRoot":"","sources":["../src/useCollection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,KAAK,GAAG,EAAE,KAAK,0BAA0B,EAAE,MAAM,mBAAmB,CAAA;AAMlG,eAAO,MAAM,aAAa,GAAI,CAAC,SAAS,GAAG,EAAE,KAAK,MAAM,GAAG,SAAS,GAAG,EAAE,GAAG,IAAI,GAAG,KAAK,EAAE,UAAS,OAAO,CAAC,0BAA0B,CAAC,CAAC,CAAC,CAAM,2BAa7I,CAAA"}
|
package/dist/useCollection.js
CHANGED
|
@@ -3,7 +3,9 @@ import { useMemo, useEffect } from "react";
|
|
|
3
3
|
import { useLivequeryClient } from "./LivequeryClientContext.js";
|
|
4
4
|
export const useCollection = (ref, options = {}) => {
|
|
5
5
|
const client = useLivequeryClient();
|
|
6
|
-
|
|
6
|
+
// Recreate the collection whenever the ref (or client) changes so each ref gets a
|
|
7
|
+
// fresh, fully-reset instance instead of mutating one long-lived collection in place.
|
|
8
|
+
const collection = useMemo(() => new LivequeryCollection(client, options), [client, ref]);
|
|
7
9
|
useEffect(() => {
|
|
8
10
|
if (!ref || !client)
|
|
9
11
|
return;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useCollection.js","sourceRoot":"","sources":["../src/useCollection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAA6C,MAAM,mBAAmB,CAAA;AAClG,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AAC1C,OAAO,EAAE,kBAAkB,EAAE,MAAM,6BAA6B,CAAA;AAIhE,MAAM,CAAC,MAAM,aAAa,GAAG,CAAgB,GAA2C,EAAE,UAAkD,EAAE,EAAE,EAAE;IAC9I,MAAM,MAAM,GAAG,kBAAkB,EAAE,CAAA;IACnC,MAAM,UAAU,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC,IAAI,mBAAmB,CAAI,MAAM,EAAE,OAAO,CAAC,EAAE,EAAE,CAAC,CAAA;
|
|
1
|
+
{"version":3,"file":"useCollection.js","sourceRoot":"","sources":["../src/useCollection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAA6C,MAAM,mBAAmB,CAAA;AAClG,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AAC1C,OAAO,EAAE,kBAAkB,EAAE,MAAM,6BAA6B,CAAA;AAIhE,MAAM,CAAC,MAAM,aAAa,GAAG,CAAgB,GAA2C,EAAE,UAAkD,EAAE,EAAE,EAAE;IAC9I,MAAM,MAAM,GAAG,kBAAkB,EAAE,CAAA;IACnC,kFAAkF;IAClF,sFAAsF;IACtF,MAAM,UAAU,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC,IAAI,mBAAmB,CAAI,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,CAAA;IAC5F,SAAS,CAAC,GAAG,EAAE;QACX,IAAI,CAAC,GAAG,IAAI,CAAC,MAAM;YAAE,OAAM;QAC3B,MAAM,MAAM,GAAG,UAAU,CAAC,UAAU,CAAC,GAAG,CAAC,CAAA;QACzC,OAAO,GAAG,EAAE;YACR,MAAM,EAAE,WAAW,EAAE,CAAA;QACzB,CAAC,CAAA;IACL,CAAC,EAAE,CAAC,UAAU,EAAE,MAAM,EAAE,GAAG,CAAC,CAAC,CAAA;IAC7B,OAAO,UAAU,CAAA;AACrB,CAAC,CAAA"}
|
package/dist/useObservable.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { Observable, BehaviorSubject } from "rxjs";
|
|
2
|
-
export type MaybeFunction<T> = T | (() => T);
|
|
3
2
|
type Source<T> = BehaviorSubject<T> | Observable<T>;
|
|
4
|
-
type ObservableSource<T> =
|
|
3
|
+
type ObservableSource<T> = Source<T> | undefined;
|
|
4
|
+
type Factory<T> = () => ObservableSource<T>;
|
|
5
|
+
type Input<T> = ObservableSource<T> | Factory<T>;
|
|
5
6
|
export declare function useObservable<T>(o: BehaviorSubject<T>): T;
|
|
6
|
-
export declare function useObservable<T>(o:
|
|
7
|
-
export declare function useObservable<T>(o:
|
|
7
|
+
export declare function useObservable<T>(o: Input<T>): T | undefined;
|
|
8
|
+
export declare function useObservable<T>(o: Input<T>, default_value: T): T;
|
|
8
9
|
export {};
|
|
9
10
|
//# sourceMappingURL=useObservable.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useObservable.d.ts","sourceRoot":"","sources":["../src/useObservable.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,eAAe,EAAO,MAAM,MAAM,CAAC;AAGxD,
|
|
1
|
+
{"version":3,"file":"useObservable.d.ts","sourceRoot":"","sources":["../src/useObservable.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,eAAe,EAAO,MAAM,MAAM,CAAC;AAGxD,KAAK,MAAM,CAAC,CAAC,IAAI,eAAe,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,CAAA;AACnD,KAAK,gBAAgB,CAAC,CAAC,IAAI,MAAM,CAAC,CAAC,CAAC,GAAG,SAAS,CAAA;AAChD,KAAK,OAAO,CAAC,CAAC,IAAI,MAAM,gBAAgB,CAAC,CAAC,CAAC,CAAA;AAC3C,KAAK,KAAK,CAAC,CAAC,IAAI,gBAAgB,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAA;AAiBhD,wBAAgB,aAAa,CAAC,CAAC,EAAE,CAAC,EAAE,eAAe,CAAC,CAAC,CAAC,GAAG,CAAC,CAAA;AAC1D,wBAAgB,aAAa,CAAC,CAAC,EAAE,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,SAAS,CAAA;AAC5D,wBAAgB,aAAa,CAAC,CAAC,EAAE,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC,EAAE,aAAa,EAAE,CAAC,GAAG,CAAC,CAAA"}
|
package/dist/useObservable.js
CHANGED
|
@@ -1,29 +1,41 @@
|
|
|
1
1
|
import { useEffect, useRef, useState } from "react";
|
|
2
2
|
import { Observable, BehaviorSubject, tap } from "rxjs";
|
|
3
3
|
import { skip } from "rxjs/operators";
|
|
4
|
+
// A value is "observable-like" if it exposes pipe/subscribe — this is what lets us tell a
|
|
5
|
+
// real source (including the @livequery/rpc callable Proxy, which is a function that ALSO
|
|
6
|
+
// exposes pipe/subscribe/getValue) apart from a plain lazy factory `() => source`.
|
|
7
|
+
const isObservableLike = (x) => !!x && (typeof x === 'object' || typeof x === 'function')
|
|
8
|
+
&& (typeof x.pipe === 'function' || typeof x.subscribe === 'function');
|
|
4
9
|
const isBehaviorSubject = (source) => {
|
|
5
|
-
return typeof source
|
|
10
|
+
return isObservableLike(source) && typeof source.getValue === 'function';
|
|
6
11
|
};
|
|
7
12
|
const hasPipe = (source) => {
|
|
8
13
|
return typeof source?.pipe === 'function';
|
|
9
14
|
};
|
|
15
|
+
// `o` is either the source directly (BehaviorSubject / Observable, incl. the callable RPC
|
|
16
|
+
// proxy) or a lazy factory `() => source`. A plain function WITHOUT pipe/subscribe is treated
|
|
17
|
+
// as a factory and resolved exactly once — so it subscribes a single time even when the caller
|
|
18
|
+
// passes a fresh arrow on every render. While there is no value yet the hook returns
|
|
19
|
+
// `default_value` (or `undefined`).
|
|
10
20
|
export function useObservable(o, default_value) {
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
: o;
|
|
21
|
+
const isFactory = typeof o === 'function' && !isObservableLike(o);
|
|
22
|
+
const resolved = useRef(null);
|
|
23
|
+
if (isFactory && !resolved.current)
|
|
24
|
+
resolved.current = { source: o() };
|
|
25
|
+
const source = isFactory ? resolved.current.source : o;
|
|
15
26
|
const prev = useRef(source);
|
|
16
|
-
const
|
|
27
|
+
const withDefault = (value) => value ?? default_value;
|
|
28
|
+
const [v, s] = useState(() => withDefault(isBehaviorSubject(source) ? source.getValue() : default_value));
|
|
17
29
|
useEffect(() => {
|
|
18
30
|
const diff = prev.current !== source;
|
|
19
31
|
prev.current = source;
|
|
20
32
|
if (!hasPipe(source))
|
|
21
33
|
return;
|
|
22
|
-
const subscription = source.pipe(skip(isBehaviorSubject(source) && !diff ? 1 : 0), tap(s)).subscribe();
|
|
34
|
+
const subscription = source.pipe(skip(isBehaviorSubject(source) && !diff ? 1 : 0), tap((value) => s(withDefault(value)))).subscribe();
|
|
23
35
|
return () => {
|
|
24
36
|
subscription.unsubscribe();
|
|
25
37
|
};
|
|
26
|
-
},
|
|
38
|
+
}, [source]);
|
|
27
39
|
return v;
|
|
28
40
|
}
|
|
29
41
|
//# sourceMappingURL=useObservable.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useObservable.js","sourceRoot":"","sources":["../src/useObservable.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AACpD,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,GAAG,EAAE,MAAM,MAAM,CAAC;AACxD,OAAO,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAOtC,MAAM,iBAAiB,GAAG,CAAI,
|
|
1
|
+
{"version":3,"file":"useObservable.js","sourceRoot":"","sources":["../src/useObservable.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AACpD,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,GAAG,EAAE,MAAM,MAAM,CAAC;AACxD,OAAO,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAOtC,0FAA0F;AAC1F,0FAA0F;AAC1F,mFAAmF;AACnF,MAAM,gBAAgB,GAAG,CAAC,CAAU,EAAoB,EAAE,CACtD,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,QAAQ,IAAI,OAAO,CAAC,KAAK,UAAU,CAAC;OACtD,CAAC,OAAQ,CAAS,CAAC,IAAI,KAAK,UAAU,IAAI,OAAQ,CAAS,CAAC,SAAS,KAAK,UAAU,CAAC,CAAA;AAE5F,MAAM,iBAAiB,GAAG,CAAI,MAAe,EAAgC,EAAE;IAC3E,OAAO,gBAAgB,CAAC,MAA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,OAAM;QAE5B,MAAM,YAAY,GAAG,MAAM,CAAC,IAAI,CAC5B,IAAI,CAAC,iBAAiB,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAChD,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,CAAC,WAAW,CAAC,KAA6B,CAAC,CAAC,CAAC,CAChE,CAAC,SAAS,EAAE,CAAA;QACb,OAAO,GAAG,EAAE;YACR,YAAY,CAAC,WAAW,EAAE,CAAA;QAC9B,CAAC,CAAA;IACL,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,CAAA;IAEZ,OAAO,CAAC,CAAA;AACZ,CAAC"}
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"url": "https://github.com/livequery/react"
|
|
5
5
|
},
|
|
6
6
|
"type": "module",
|
|
7
|
-
"version": "2.0.
|
|
7
|
+
"version": "2.0.141",
|
|
8
8
|
"description": "",
|
|
9
9
|
"main": "./dist/index.js",
|
|
10
10
|
"types": "./dist/index.d.ts",
|
|
@@ -49,14 +49,14 @@
|
|
|
49
49
|
"dist/**/*"
|
|
50
50
|
],
|
|
51
51
|
"devDependencies": {
|
|
52
|
-
"@livequery/client": "^2.0.
|
|
52
|
+
"@livequery/client": "^2.0.141",
|
|
53
53
|
"@types/bun": "^1.3.14",
|
|
54
54
|
"@types/react": "^19.2.14",
|
|
55
55
|
"@types/react-test-renderer": "^19.1.0",
|
|
56
56
|
"react-test-renderer": "^19.2.6"
|
|
57
57
|
},
|
|
58
58
|
"peerDependencies": {
|
|
59
|
-
"@livequery/client": "^2.0.
|
|
59
|
+
"@livequery/client": "^2.0.141",
|
|
60
60
|
"react": "^19.2.5",
|
|
61
61
|
"rxjs": "^7.8.2"
|
|
62
62
|
},
|