react-rx 4.1.32 → 4.2.0
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/dist/index.cjs +24 -15
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +7 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +24 -15
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
- package/src/__tests__/useObservable.test-d.ts +4 -4
- package/src/__tests__/useObservable.test.tsx +65 -1
- package/src/useObservable.ts +15 -1
package/dist/index.cjs
CHANGED
|
@@ -6,18 +6,23 @@ function getValue(value) {
|
|
|
6
6
|
return typeof value == "function" ? value() : value;
|
|
7
7
|
}
|
|
8
8
|
const cache = /* @__PURE__ */ new WeakMap();
|
|
9
|
-
function useObservable(observable, initialValue) {
|
|
10
|
-
const $ = reactCompilerRuntime.c(
|
|
9
|
+
function useObservable(observable, initialValue, t0) {
|
|
10
|
+
const $ = reactCompilerRuntime.c(12);
|
|
11
|
+
let t1;
|
|
12
|
+
$[0] !== t0 ? (t1 = t0 === void 0 ? {} : t0, $[0] = t0, $[1] = t1) : t1 = $[1];
|
|
13
|
+
const options = t1, {
|
|
14
|
+
disabled: t2
|
|
15
|
+
} = options, disabled = t2 === void 0 ? !1 : t2;
|
|
11
16
|
if (!cache.has(observable)) {
|
|
12
17
|
const state = {
|
|
13
18
|
didEmit: !1
|
|
14
19
|
}, entry = {
|
|
15
20
|
state,
|
|
16
|
-
observable: observable.pipe(operators.map(_temp$1), rxjs.catchError(_temp2), operators.tap((
|
|
21
|
+
observable: observable.pipe(operators.map(_temp$1), rxjs.catchError(_temp2), operators.tap((t32) => {
|
|
17
22
|
const {
|
|
18
23
|
snapshot,
|
|
19
24
|
error: error_0
|
|
20
|
-
} =
|
|
25
|
+
} = t32;
|
|
21
26
|
state.didEmit = !0, state.snapshot = snapshot, state.error = error_0;
|
|
22
27
|
}), operators.map(_temp3), rxjs.finalize(() => cache.delete(observable)), rxjs.share({
|
|
23
28
|
resetOnRefCountZero: _temp4
|
|
@@ -30,21 +35,25 @@ function useObservable(observable, initialValue) {
|
|
|
30
35
|
};
|
|
31
36
|
entry.observable.subscribe().unsubscribe(), cache.set(observable, entry);
|
|
32
37
|
}
|
|
33
|
-
let
|
|
34
|
-
$[
|
|
35
|
-
const instance =
|
|
36
|
-
let
|
|
37
|
-
$[
|
|
38
|
+
let t3;
|
|
39
|
+
$[2] !== observable ? (t3 = cache.get(observable), $[2] = observable, $[3] = t3) : t3 = $[3];
|
|
40
|
+
const instance = t3;
|
|
41
|
+
let t4;
|
|
42
|
+
$[4] !== disabled || $[5] !== instance.observable ? (t4 = (onStoreChange) => {
|
|
43
|
+
if (disabled)
|
|
44
|
+
return _temp5;
|
|
38
45
|
const subscription_0 = instance.observable.subscribe(onStoreChange);
|
|
39
46
|
return () => {
|
|
40
47
|
subscription_0.unsubscribe();
|
|
41
48
|
};
|
|
42
|
-
}, $[
|
|
43
|
-
const subscribe =
|
|
44
|
-
let
|
|
45
|
-
$[
|
|
46
|
-
let
|
|
47
|
-
return $[
|
|
49
|
+
}, $[4] = disabled, $[5] = instance.observable, $[6] = t4) : t4 = $[6];
|
|
50
|
+
const subscribe = t4;
|
|
51
|
+
let t5;
|
|
52
|
+
$[7] !== initialValue || $[8] !== instance ? (t5 = () => instance.getSnapshot(initialValue), $[7] = initialValue, $[8] = instance, $[9] = t5) : t5 = $[9];
|
|
53
|
+
let t6;
|
|
54
|
+
return $[10] !== initialValue ? (t6 = typeof initialValue > "u" ? void 0 : () => getValue(initialValue), $[10] = initialValue, $[11] = t6) : t6 = $[11], react.useSyncExternalStore(subscribe, t5, t6);
|
|
55
|
+
}
|
|
56
|
+
function _temp5() {
|
|
48
57
|
}
|
|
49
58
|
function _temp4() {
|
|
50
59
|
return rxjs.timer(0, rxjs.asapScheduler);
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/useObservable.ts","../src/useObservableEvent.ts"],"sourcesContent":["import {useCallback, useMemo, useSyncExternalStore} from 'react'\nimport {\n asapScheduler,\n catchError,\n finalize,\n type Observable,\n type ObservedValueOf,\n of,\n share,\n timer,\n} from 'rxjs'\nimport {map, tap} from 'rxjs/operators'\n\nfunction getValue<T>(value: T): T extends () => infer U ? U : T {\n return (typeof value === 'function' ? (value as () => any)() : value) as T extends () => infer U\n ? U\n : T\n}\n\ninterface ObservableState<T> {\n didEmit: boolean\n snapshot?: T\n error?: unknown\n}\n\ninterface CacheRecord<T> {\n observable: Observable<void>\n state: {\n didEmit: boolean\n snapshot?: T\n error?: unknown\n }\n getSnapshot: (initialValue: unknown) => T\n}\n\nconst cache = new WeakMap<Observable<any>, CacheRecord<any>>()\n\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n initialValue: ObservedValueOf<ObservableType> | (() => ObservedValueOf<ObservableType>),\n): ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n): undefined | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue: InitialValue | (() => InitialValue),\n): InitialValue | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue?: InitialValue | (() => InitialValue),\n): InitialValue | ObservedValueOf<ObservableType> {\n const instance = useMemo(() => {\n if (!cache.has(observable)) {\n // This separate object is used as a stable reference to the cache entry's snapshot and error.\n // It's used by the `getSnapshot` closure.\n const state: ObservableState<ObservedValueOf<ObservableType>> = {\n didEmit: false,\n }\n const entry: CacheRecord<ObservedValueOf<ObservableType>> = {\n state,\n observable: observable.pipe(\n map((value) => ({snapshot: value, error: undefined})),\n catchError((error) => of({snapshot: undefined, error})),\n tap(({snapshot, error}) => {\n state.didEmit = true\n state.snapshot = snapshot\n state.error = error\n }),\n // Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state\n // and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.\n map((value) => void value),\n // Ensure that the cache entry is deleted when the observable completes or errors.\n finalize(() => cache.delete(observable)),\n share({resetOnRefCountZero: () => timer(0, asapScheduler)}),\n ),\n getSnapshot: (initialValue) => {\n if (state.error) {\n throw state.error\n }\n return (\n state.didEmit ? state.snapshot : getValue(initialValue)\n ) as ObservedValueOf<ObservableType>\n },\n }\n\n // Eagerly subscribe to sync set `state.snapshot` to what the observable returns, and keep the observable alive until the component unmounts.\n const subscription = entry.observable.subscribe()\n subscription.unsubscribe()\n\n cache.set(observable, entry)\n }\n return cache.get(observable)!\n }, [observable])\n\n const subscribe = useCallback(\n (onStoreChange: () => void) => {\n const subscription = instance.observable.subscribe(onStoreChange)\n return () => {\n subscription.unsubscribe()\n }\n },\n [instance.observable],\n )\n\n return useSyncExternalStore<ObservedValueOf<ObservableType>>(\n subscribe,\n () => {\n return instance.getSnapshot(initialValue)\n },\n typeof initialValue === 'undefined'\n ? undefined\n : () => getValue(initialValue) as ObservedValueOf<ObservableType>,\n )\n}\n","import {observableCallback} from 'observable-callback'\nimport {useEffect, useState} from 'react'\nimport {type Observable} from 'rxjs'\nimport {useEffectEvent} from 'use-effect-event'\n\n/** @public */\nexport function useObservableEvent<T, U>(\n handleEvent: (arg: Observable<T>) => Observable<U>,\n): (arg: T) => void {\n const [[calls$, call]] = useState(() => observableCallback<T>())\n\n const onEvent = useEffectEvent((observable: Observable<T>) => handleEvent(observable))\n\n useEffect(() => {\n const subscription = calls$.pipe((observable) => onEvent(observable)).subscribe()\n return () => subscription.unsubscribe()\n }, [calls$])\n\n return call\n}\n"],"names":["getValue","value","cache","WeakMap","useObservable","observable","initialValue","$","_c","has","state","didEmit","entry","pipe","map","_temp","catchError","_temp2","tap","
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/useObservable.ts","../src/useObservableEvent.ts"],"sourcesContent":["import {useCallback, useMemo, useSyncExternalStore} from 'react'\nimport {\n asapScheduler,\n catchError,\n finalize,\n type Observable,\n type ObservedValueOf,\n of,\n share,\n timer,\n} from 'rxjs'\nimport {map, tap} from 'rxjs/operators'\n\nfunction getValue<T>(value: T): T extends () => infer U ? U : T {\n return (typeof value === 'function' ? (value as () => any)() : value) as T extends () => infer U\n ? U\n : T\n}\n\ninterface ObservableState<T> {\n didEmit: boolean\n snapshot?: T\n error?: unknown\n}\n\ninterface CacheRecord<T> {\n observable: Observable<void>\n state: {\n didEmit: boolean\n snapshot?: T\n error?: unknown\n }\n getSnapshot: (initialValue: unknown) => T\n}\n\nconst cache = new WeakMap<Observable<any>, CacheRecord<any>>()\n\n/** @public */\nexport interface UseObservableOptions {\n disabled?: boolean\n}\n\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n initialValue: ObservedValueOf<ObservableType> | (() => ObservedValueOf<ObservableType>),\n options?: UseObservableOptions,\n): ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n): undefined | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue: InitialValue | (() => InitialValue),\n options?: UseObservableOptions,\n): InitialValue | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue?: InitialValue | (() => InitialValue),\n options: UseObservableOptions = {},\n): InitialValue | ObservedValueOf<ObservableType> {\n const {disabled = false} = options\n\n const instance = useMemo(() => {\n if (!cache.has(observable)) {\n // This separate object is used as a stable reference to the cache entry's snapshot and error.\n // It's used by the `getSnapshot` closure.\n const state: ObservableState<ObservedValueOf<ObservableType>> = {\n didEmit: false,\n }\n const entry: CacheRecord<ObservedValueOf<ObservableType>> = {\n state,\n observable: observable.pipe(\n map((value) => ({snapshot: value, error: undefined})),\n catchError((error) => of({snapshot: undefined, error})),\n tap(({snapshot, error}) => {\n state.didEmit = true\n state.snapshot = snapshot\n state.error = error\n }),\n // Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state\n // and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.\n map((value) => void value),\n // Ensure that the cache entry is deleted when the observable completes or errors.\n finalize(() => cache.delete(observable)),\n share({resetOnRefCountZero: () => timer(0, asapScheduler)}),\n ),\n getSnapshot: (initialValue) => {\n if (state.error) {\n throw state.error\n }\n return (\n state.didEmit ? state.snapshot : getValue(initialValue)\n ) as ObservedValueOf<ObservableType>\n },\n }\n\n // Eagerly subscribe to sync set `state.snapshot` to what the observable returns, and keep the observable alive until the component unmounts.\n const subscription = entry.observable.subscribe()\n subscription.unsubscribe()\n\n cache.set(observable, entry)\n }\n return cache.get(observable)!\n }, [observable])\n\n const subscribe = useCallback(\n (onStoreChange: () => void) => {\n if (disabled) {\n return () => {}\n }\n\n const subscription = instance.observable.subscribe(onStoreChange)\n return () => {\n subscription.unsubscribe()\n }\n },\n [instance.observable, disabled],\n )\n\n return useSyncExternalStore<ObservedValueOf<ObservableType>>(\n subscribe,\n () => {\n return instance.getSnapshot(initialValue)\n },\n typeof initialValue === 'undefined'\n ? undefined\n : () => getValue(initialValue) as ObservedValueOf<ObservableType>,\n )\n}\n","import {observableCallback} from 'observable-callback'\nimport {useEffect, useState} from 'react'\nimport {type Observable} from 'rxjs'\nimport {useEffectEvent} from 'use-effect-event'\n\n/** @public */\nexport function useObservableEvent<T, U>(\n handleEvent: (arg: Observable<T>) => Observable<U>,\n): (arg: T) => void {\n const [[calls$, call]] = useState(() => observableCallback<T>())\n\n const onEvent = useEffectEvent((observable: Observable<T>) => handleEvent(observable))\n\n useEffect(() => {\n const subscription = calls$.pipe((observable) => onEvent(observable)).subscribe()\n return () => subscription.unsubscribe()\n }, [calls$])\n\n return call\n}\n"],"names":["getValue","value","cache","WeakMap","useObservable","observable","initialValue","t0","$","_c","t1","undefined","options","disabled","t2","has","state","didEmit","entry","pipe","map","_temp","catchError","_temp2","tap","t3","snapshot","error","error_0","_temp3","finalize","delete","share","resetOnRefCountZero","_temp4","getSnapshot","initialValue_0","subscribe","unsubscribe","set","get","instance","t4","onStoreChange","_temp5","subscription_0","subscription","t5","t6","useSyncExternalStore","timer","asapScheduler","value_0","of","useObservableEvent","handleEvent","useState","calls$","call","onEvent","useEffectEvent","observable_0","useEffect","observableCallback"],"mappings":";;;;AAaA,SAASA,SAAYC,OAA2C;AAC9D,SAAQ,OAAOA,SAAU,aAAcA,MAAAA,IAAwBA;AAGjE;AAkBA,MAAMC,4BAAYC,QAAAA;AAwBX,SAAAC,cAAAC,YAAAC,cAAAC,IAAA;AAAA,QAAAC,IAAAC,qBAAAA,EAAA,EAAA;AAAA,MAAAC;AAAAF,WAAAD,MAGLG,KAAAH,OAAkCI,cAAlCJ,IAAkCC,OAAAD,IAAAC,OAAAE,MAAAA,KAAAF,EAAA,CAAA;AAAlC,QAAAI,UAAAF,IAEA;AAAA,IAAAG,UAAAC;AAAAA,EAAAA,IAA2BF,SAApBC,WAAAC,OAAgBH,cAAhBG;AAAgB,MAAA,CAGhBZ,MAAAa,IAAUV,UAAU,GAAC;AAGxB,UAAAW,QAAA;AAAA,MAAAC,SAAA;AAAA,IAAA,GAGAC,QAAA;AAAA,MAAAF;AAAAA,MAAAX,YAEcA,WAAUc,KACpBC,UAAAA,IAAAC,OAAoD,GACpDC,KAAAA,WAAAC,MAAsD,GACtDC,cAAAC,CAAAA,QAAA;AAAK,cAAA;AAAA,UAAAC;AAAAA,UAAAC,OAAAC;AAAAA,QAAAA,IAAAH;AACHT,cAAKC,UAAA,IACLD,MAAKU,WAAYA,UACjBV,MAAKW,QAASA;AAAAA,MAAK,CACpB,GAGDP,UAAAA,IAAAS,MAAyB,GAEzBC,KAAAA,SAAA,MAAe5B,MAAA6B,OAAa1B,UAAU,CAAC,GACvC2B,WAAA;AAAA,QAAAC,qBAAAC;AAAAA,MAAAA,CAA0D,CAC5D;AAAA,MAACC,aAAAC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observable: ObservableType,
|
|
7
7
|
initialValue: ObservedValueOf<ObservableType> | (() => ObservedValueOf<ObservableType>),
|
|
8
|
+
options?: UseObservableOptions,
|
|
8
9
|
): ObservedValueOf<ObservableType>
|
|
9
10
|
|
|
10
11
|
/** @public */
|
|
@@ -16,6 +17,7 @@ export declare function useObservable<ObservableType extends Observable<any>>(
|
|
|
16
17
|
export declare function useObservable<ObservableType extends Observable<any>, InitialValue>(
|
|
17
18
|
observable: ObservableType,
|
|
18
19
|
initialValue: InitialValue | (() => InitialValue),
|
|
20
|
+
options?: UseObservableOptions,
|
|
19
21
|
): InitialValue | ObservedValueOf<ObservableType>
|
|
20
22
|
|
|
21
23
|
/** @public */
|
|
@@ -23,4 +25,9 @@ export declare function useObservableEvent<T, U>(
|
|
|
23
25
|
handleEvent: (arg: Observable<T>) => Observable<U>,
|
|
24
26
|
): (arg: T) => void
|
|
25
27
|
|
|
28
|
+
/** @public */
|
|
29
|
+
export declare interface UseObservableOptions {
|
|
30
|
+
disabled?: boolean
|
|
31
|
+
}
|
|
32
|
+
|
|
26
33
|
export {}
|
package/dist/index.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ import {ObservedValueOf} from 'rxjs'
|
|
|
5
5
|
export declare function useObservable<ObservableType extends Observable<any>>(
|
|
6
6
|
observable: ObservableType,
|
|
7
7
|
initialValue: ObservedValueOf<ObservableType> | (() => ObservedValueOf<ObservableType>),
|
|
8
|
+
options?: UseObservableOptions,
|
|
8
9
|
): ObservedValueOf<ObservableType>
|
|
9
10
|
|
|
10
11
|
/** @public */
|
|
@@ -16,6 +17,7 @@ export declare function useObservable<ObservableType extends Observable<any>>(
|
|
|
16
17
|
export declare function useObservable<ObservableType extends Observable<any>, InitialValue>(
|
|
17
18
|
observable: ObservableType,
|
|
18
19
|
initialValue: InitialValue | (() => InitialValue),
|
|
20
|
+
options?: UseObservableOptions,
|
|
19
21
|
): InitialValue | ObservedValueOf<ObservableType>
|
|
20
22
|
|
|
21
23
|
/** @public */
|
|
@@ -23,4 +25,9 @@ export declare function useObservableEvent<T, U>(
|
|
|
23
25
|
handleEvent: (arg: Observable<T>) => Observable<U>,
|
|
24
26
|
): (arg: T) => void
|
|
25
27
|
|
|
28
|
+
/** @public */
|
|
29
|
+
export declare interface UseObservableOptions {
|
|
30
|
+
disabled?: boolean
|
|
31
|
+
}
|
|
32
|
+
|
|
26
33
|
export {}
|
package/dist/index.js
CHANGED
|
@@ -9,18 +9,23 @@ function getValue(value) {
|
|
|
9
9
|
return typeof value == "function" ? value() : value;
|
|
10
10
|
}
|
|
11
11
|
const cache = /* @__PURE__ */ new WeakMap();
|
|
12
|
-
function useObservable(observable, initialValue) {
|
|
13
|
-
const $ = c(
|
|
12
|
+
function useObservable(observable, initialValue, t0) {
|
|
13
|
+
const $ = c(12);
|
|
14
|
+
let t1;
|
|
15
|
+
$[0] !== t0 ? (t1 = t0 === void 0 ? {} : t0, $[0] = t0, $[1] = t1) : t1 = $[1];
|
|
16
|
+
const options = t1, {
|
|
17
|
+
disabled: t2
|
|
18
|
+
} = options, disabled = t2 === void 0 ? !1 : t2;
|
|
14
19
|
if (!cache.has(observable)) {
|
|
15
20
|
const state = {
|
|
16
21
|
didEmit: !1
|
|
17
22
|
}, entry = {
|
|
18
23
|
state,
|
|
19
|
-
observable: observable.pipe(map(_temp$1), catchError(_temp2), tap((
|
|
24
|
+
observable: observable.pipe(map(_temp$1), catchError(_temp2), tap((t32) => {
|
|
20
25
|
const {
|
|
21
26
|
snapshot,
|
|
22
27
|
error: error_0
|
|
23
|
-
} =
|
|
28
|
+
} = t32;
|
|
24
29
|
state.didEmit = !0, state.snapshot = snapshot, state.error = error_0;
|
|
25
30
|
}), map(_temp3), finalize(() => cache.delete(observable)), share({
|
|
26
31
|
resetOnRefCountZero: _temp4
|
|
@@ -33,21 +38,25 @@ function useObservable(observable, initialValue) {
|
|
|
33
38
|
};
|
|
34
39
|
entry.observable.subscribe().unsubscribe(), cache.set(observable, entry);
|
|
35
40
|
}
|
|
36
|
-
let
|
|
37
|
-
$[
|
|
38
|
-
const instance =
|
|
39
|
-
let
|
|
40
|
-
$[
|
|
41
|
+
let t3;
|
|
42
|
+
$[2] !== observable ? (t3 = cache.get(observable), $[2] = observable, $[3] = t3) : t3 = $[3];
|
|
43
|
+
const instance = t3;
|
|
44
|
+
let t4;
|
|
45
|
+
$[4] !== disabled || $[5] !== instance.observable ? (t4 = (onStoreChange) => {
|
|
46
|
+
if (disabled)
|
|
47
|
+
return _temp5;
|
|
41
48
|
const subscription_0 = instance.observable.subscribe(onStoreChange);
|
|
42
49
|
return () => {
|
|
43
50
|
subscription_0.unsubscribe();
|
|
44
51
|
};
|
|
45
|
-
}, $[
|
|
46
|
-
const subscribe =
|
|
47
|
-
let
|
|
48
|
-
$[
|
|
49
|
-
let
|
|
50
|
-
return $[
|
|
52
|
+
}, $[4] = disabled, $[5] = instance.observable, $[6] = t4) : t4 = $[6];
|
|
53
|
+
const subscribe = t4;
|
|
54
|
+
let t5;
|
|
55
|
+
$[7] !== initialValue || $[8] !== instance ? (t5 = () => instance.getSnapshot(initialValue), $[7] = initialValue, $[8] = instance, $[9] = t5) : t5 = $[9];
|
|
56
|
+
let t6;
|
|
57
|
+
return $[10] !== initialValue ? (t6 = typeof initialValue > "u" ? void 0 : () => getValue(initialValue), $[10] = initialValue, $[11] = t6) : t6 = $[11], useSyncExternalStore(subscribe, t5, t6);
|
|
58
|
+
}
|
|
59
|
+
function _temp5() {
|
|
51
60
|
}
|
|
52
61
|
function _temp4() {
|
|
53
62
|
return timer(0, asapScheduler);
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/useObservable.ts","../src/useObservableEvent.ts"],"sourcesContent":["import {useCallback, useMemo, useSyncExternalStore} from 'react'\nimport {\n asapScheduler,\n catchError,\n finalize,\n type Observable,\n type ObservedValueOf,\n of,\n share,\n timer,\n} from 'rxjs'\nimport {map, tap} from 'rxjs/operators'\n\nfunction getValue<T>(value: T): T extends () => infer U ? U : T {\n return (typeof value === 'function' ? (value as () => any)() : value) as T extends () => infer U\n ? U\n : T\n}\n\ninterface ObservableState<T> {\n didEmit: boolean\n snapshot?: T\n error?: unknown\n}\n\ninterface CacheRecord<T> {\n observable: Observable<void>\n state: {\n didEmit: boolean\n snapshot?: T\n error?: unknown\n }\n getSnapshot: (initialValue: unknown) => T\n}\n\nconst cache = new WeakMap<Observable<any>, CacheRecord<any>>()\n\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n initialValue: ObservedValueOf<ObservableType> | (() => ObservedValueOf<ObservableType>),\n): ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n): undefined | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue: InitialValue | (() => InitialValue),\n): InitialValue | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue?: InitialValue | (() => InitialValue),\n): InitialValue | ObservedValueOf<ObservableType> {\n const instance = useMemo(() => {\n if (!cache.has(observable)) {\n // This separate object is used as a stable reference to the cache entry's snapshot and error.\n // It's used by the `getSnapshot` closure.\n const state: ObservableState<ObservedValueOf<ObservableType>> = {\n didEmit: false,\n }\n const entry: CacheRecord<ObservedValueOf<ObservableType>> = {\n state,\n observable: observable.pipe(\n map((value) => ({snapshot: value, error: undefined})),\n catchError((error) => of({snapshot: undefined, error})),\n tap(({snapshot, error}) => {\n state.didEmit = true\n state.snapshot = snapshot\n state.error = error\n }),\n // Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state\n // and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.\n map((value) => void value),\n // Ensure that the cache entry is deleted when the observable completes or errors.\n finalize(() => cache.delete(observable)),\n share({resetOnRefCountZero: () => timer(0, asapScheduler)}),\n ),\n getSnapshot: (initialValue) => {\n if (state.error) {\n throw state.error\n }\n return (\n state.didEmit ? state.snapshot : getValue(initialValue)\n ) as ObservedValueOf<ObservableType>\n },\n }\n\n // Eagerly subscribe to sync set `state.snapshot` to what the observable returns, and keep the observable alive until the component unmounts.\n const subscription = entry.observable.subscribe()\n subscription.unsubscribe()\n\n cache.set(observable, entry)\n }\n return cache.get(observable)!\n }, [observable])\n\n const subscribe = useCallback(\n (onStoreChange: () => void) => {\n const subscription = instance.observable.subscribe(onStoreChange)\n return () => {\n subscription.unsubscribe()\n }\n },\n [instance.observable],\n )\n\n return useSyncExternalStore<ObservedValueOf<ObservableType>>(\n subscribe,\n () => {\n return instance.getSnapshot(initialValue)\n },\n typeof initialValue === 'undefined'\n ? undefined\n : () => getValue(initialValue) as ObservedValueOf<ObservableType>,\n )\n}\n","import {observableCallback} from 'observable-callback'\nimport {useEffect, useState} from 'react'\nimport {type Observable} from 'rxjs'\nimport {useEffectEvent} from 'use-effect-event'\n\n/** @public */\nexport function useObservableEvent<T, U>(\n handleEvent: (arg: Observable<T>) => Observable<U>,\n): (arg: T) => void {\n const [[calls$, call]] = useState(() => observableCallback<T>())\n\n const onEvent = useEffectEvent((observable: Observable<T>) => handleEvent(observable))\n\n useEffect(() => {\n const subscription = calls$.pipe((observable) => onEvent(observable)).subscribe()\n return () => subscription.unsubscribe()\n }, [calls$])\n\n return call\n}\n"],"names":["getValue","value","cache","WeakMap","useObservable","observable","initialValue","$","_c","has","state","didEmit","entry","pipe","map","_temp","catchError","_temp2","tap","
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/useObservable.ts","../src/useObservableEvent.ts"],"sourcesContent":["import {useCallback, useMemo, useSyncExternalStore} from 'react'\nimport {\n asapScheduler,\n catchError,\n finalize,\n type Observable,\n type ObservedValueOf,\n of,\n share,\n timer,\n} from 'rxjs'\nimport {map, tap} from 'rxjs/operators'\n\nfunction getValue<T>(value: T): T extends () => infer U ? U : T {\n return (typeof value === 'function' ? (value as () => any)() : value) as T extends () => infer U\n ? U\n : T\n}\n\ninterface ObservableState<T> {\n didEmit: boolean\n snapshot?: T\n error?: unknown\n}\n\ninterface CacheRecord<T> {\n observable: Observable<void>\n state: {\n didEmit: boolean\n snapshot?: T\n error?: unknown\n }\n getSnapshot: (initialValue: unknown) => T\n}\n\nconst cache = new WeakMap<Observable<any>, CacheRecord<any>>()\n\n/** @public */\nexport interface UseObservableOptions {\n disabled?: boolean\n}\n\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n initialValue: ObservedValueOf<ObservableType> | (() => ObservedValueOf<ObservableType>),\n options?: UseObservableOptions,\n): ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n): undefined | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue: InitialValue | (() => InitialValue),\n options?: UseObservableOptions,\n): InitialValue | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue?: InitialValue | (() => InitialValue),\n options: UseObservableOptions = {},\n): InitialValue | ObservedValueOf<ObservableType> {\n const {disabled = false} = options\n\n const instance = useMemo(() => {\n if (!cache.has(observable)) {\n // This separate object is used as a stable reference to the cache entry's snapshot and error.\n // It's used by the `getSnapshot` closure.\n const state: ObservableState<ObservedValueOf<ObservableType>> = {\n didEmit: false,\n }\n const entry: CacheRecord<ObservedValueOf<ObservableType>> = {\n state,\n observable: observable.pipe(\n map((value) => ({snapshot: value, error: undefined})),\n catchError((error) => of({snapshot: undefined, error})),\n tap(({snapshot, error}) => {\n state.didEmit = true\n state.snapshot = snapshot\n state.error = error\n }),\n // Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state\n // and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.\n map((value) => void value),\n // Ensure that the cache entry is deleted when the observable completes or errors.\n finalize(() => cache.delete(observable)),\n share({resetOnRefCountZero: () => timer(0, asapScheduler)}),\n ),\n getSnapshot: (initialValue) => {\n if (state.error) {\n throw state.error\n }\n return (\n state.didEmit ? state.snapshot : getValue(initialValue)\n ) as ObservedValueOf<ObservableType>\n },\n }\n\n // Eagerly subscribe to sync set `state.snapshot` to what the observable returns, and keep the observable alive until the component unmounts.\n const subscription = entry.observable.subscribe()\n subscription.unsubscribe()\n\n cache.set(observable, entry)\n }\n return cache.get(observable)!\n }, [observable])\n\n const subscribe = useCallback(\n (onStoreChange: () => void) => {\n if (disabled) {\n return () => {}\n }\n\n const subscription = instance.observable.subscribe(onStoreChange)\n return () => {\n subscription.unsubscribe()\n }\n },\n [instance.observable, disabled],\n )\n\n return useSyncExternalStore<ObservedValueOf<ObservableType>>(\n subscribe,\n () => {\n return instance.getSnapshot(initialValue)\n },\n typeof initialValue === 'undefined'\n ? undefined\n : () => getValue(initialValue) as ObservedValueOf<ObservableType>,\n )\n}\n","import {observableCallback} from 'observable-callback'\nimport {useEffect, useState} from 'react'\nimport {type Observable} from 'rxjs'\nimport {useEffectEvent} from 'use-effect-event'\n\n/** @public */\nexport function useObservableEvent<T, U>(\n handleEvent: (arg: Observable<T>) => Observable<U>,\n): (arg: T) => void {\n const [[calls$, call]] = useState(() => observableCallback<T>())\n\n const onEvent = useEffectEvent((observable: Observable<T>) => handleEvent(observable))\n\n useEffect(() => {\n const subscription = calls$.pipe((observable) => onEvent(observable)).subscribe()\n return () => subscription.unsubscribe()\n }, [calls$])\n\n return call\n}\n"],"names":["getValue","value","cache","WeakMap","useObservable","observable","initialValue","t0","$","_c","t1","undefined","options","disabled","t2","has","state","didEmit","entry","pipe","map","_temp","catchError","_temp2","tap","t3","snapshot","error","error_0","_temp3","finalize","delete","share","resetOnRefCountZero","_temp4","getSnapshot","initialValue_0","subscribe","unsubscribe","set","get","instance","t4","onStoreChange","_temp5","subscription_0","subscription","t5","t6","useSyncExternalStore","timer","asapScheduler","value_0","of","useObservableEvent","handleEvent","useState","calls$","call","onEvent","useEffectEvent","observable_0","useEffect","observableCallback"],"mappings":";;;;;;;AAaA,SAASA,SAAYC,OAA2C;AAC9D,SAAQ,OAAOA,SAAU,aAAcA,MAAAA,IAAwBA;AAGjE;AAkBA,MAAMC,4BAAYC,QAAAA;AAwBX,SAAAC,cAAAC,YAAAC,cAAAC,IAAA;AAAA,QAAAC,IAAAC,EAAA,EAAA;AAAA,MAAAC;AAAAF,WAAAD,MAGLG,KAAAH,OAAkCI,cAAlCJ,IAAkCC,OAAAD,IAAAC,OAAAE,MAAAA,KAAAF,EAAA,CAAA;AAAlC,QAAAI,UAAAF,IAEA;AAAA,IAAAG,UAAAC;AAAAA,EAAAA,IAA2BF,SAApBC,WAAAC,OAAgBH,cAAhBG;AAAgB,MAAA,CAGhBZ,MAAAa,IAAUV,UAAU,GAAC;AAGxB,UAAAW,QAAA;AAAA,MAAAC,SAAA;AAAA,IAAA,GAGAC,QAAA;AAAA,MAAAF;AAAAA,MAAAX,YAEcA,WAAUc,KACpBC,IAAAC,OAAoD,GACpDC,WAAAC,MAAsD,GACtDC,IAAAC,CAAAA,QAAA;AAAK,cAAA;AAAA,UAAAC;AAAAA,UAAAC,OAAAC;AAAAA,QAAAA,IAAAH;AACHT,cAAKC,UAAA,IACLD,MAAKU,WAAYA,UACjBV,MAAKW,QAASA;AAAAA,MAAK,CACpB,GAGDP,IAAAS,MAAyB,GAEzBC,SAAA,MAAe5B,MAAA6B,OAAa1B,UAAU,CAAC,GACvC2B,MAAA;AAAA,QAAAC,qBAAAC;AAAAA,MAAAA,CAA0D,CAC5D;AAAA,MAACC,aAAAC,CAAAA,mBAAA;AAAA,YAEKpB,MAAKW;AAAA,gBACDX,MAAKW;AAAA,eAGXX,MAAKC,UAAWD,MAAKU,WAAY1B,SAASM,cAAY;AAAA,MAAC;AAAA,IAAA;AAMxCY,UAAKb,WAAAgC,YACdC,eAEZpC,MAAAqC,IAAUlC,YAAYa,KAAK;AAAA,E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B;AAAAA,EAAAA;AAAA;ACtEnC,SAAA2C,mBAAAC,aAAA;AAAA,QAAA/C,IAAAC,EAAA,CAAA,GAGL,CAAAF,EAAA,IAAyBiD,SAAAnC,KAAsC,GAAxD,CAAAoC,QAAAC,IAAA,IAAAnD;AAAc,MAAAG;AAAAF,WAAA+C,eAEU7C,KAAAL,CAAAA,eAA+BkD,YAAYlD,UAAU,GAACG,OAAA+C,aAAA/C,OAAAE,MAAAA,KAAAF,EAAA,CAAA;AAArF,QAAAmD,UAAgBC,eAAelD,EAAsD;AAAC,MAAAI;AAAAN,IAAA,CAAA,MAAAiD,UAAAjD,SAAAmD,WAE5E7C,KAAAA,MAAA;AACR,UAAAgC,eAAqBW,OAAMtC,KAAA0C,CAAAA,iBAAsBF,QAAQtD,YAAU,CAAC,EAACgC,UAAAA;AAAY,WAAA,MACpES,aAAYR,YAAAA;AAAAA,EAAc,GACxC9B,OAAAiD,QAAAjD,OAAAmD,SAAAnD,OAAAM,MAAAA,KAAAN,EAAA,CAAA;AAAA,MAAAiB;AAAA,SAAAjB,SAAAiD,UAAEhC,MAACgC,MAAM,GAACjD,OAAAiD,QAAAjD,OAAAiB,MAAAA,KAAAjB,EAAA,CAAA,GAHXsD,UAAUhD,IAGPW,EAAQ,GAEJiC;AAAI;AAZN,SAAArC,QAAA;AAAA,SAGmC0C,mBAAAA;AAAuB;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-rx",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.2.0",
|
|
4
4
|
"description": "React + RxJS = <3",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"action",
|
|
@@ -90,16 +90,16 @@
|
|
|
90
90
|
"@types/react-dom": "^19.1.9",
|
|
91
91
|
"@typescript-eslint/eslint-plugin": "^8.41.0",
|
|
92
92
|
"@typescript-eslint/parser": "^8.41.0",
|
|
93
|
-
"@vitejs/plugin-react": "^
|
|
93
|
+
"@vitejs/plugin-react": "^5.0.2",
|
|
94
94
|
"babel-plugin-react-compiler": "19.1.0-rc.3",
|
|
95
95
|
"eslint": "^9.34.0",
|
|
96
96
|
"eslint-config-prettier": "^10.1.8",
|
|
97
97
|
"eslint-plugin-react": "^7.37.5",
|
|
98
98
|
"eslint-plugin-react-hooks": "6.0.0-rc.2",
|
|
99
|
-
"eslint-plugin-react-hooks-with-use-effect-event": "npm:eslint-plugin-react-hooks@0.0.0-experimental-
|
|
99
|
+
"eslint-plugin-react-hooks-with-use-effect-event": "npm:eslint-plugin-react-hooks@0.0.0-experimental-d415fd3e-20250919",
|
|
100
100
|
"eslint-plugin-simple-import-sort": "^12.1.1",
|
|
101
|
-
"jsdom": "^
|
|
102
|
-
"prettier": "^3.
|
|
101
|
+
"jsdom": "^26.1.0",
|
|
102
|
+
"prettier": "^3.6.2",
|
|
103
103
|
"react": "^19.1.1",
|
|
104
104
|
"react-dom": "^19.1.1",
|
|
105
105
|
"react-test-renderer": "^19.1.1",
|
|
@@ -15,13 +15,13 @@ test('useObservable with no initial value can be undefined', () => {
|
|
|
15
15
|
test('return type of useObservable with initial value is not undefined', () => {
|
|
16
16
|
const observable = of('foo')
|
|
17
17
|
//@ts-expect-error - because initial value is given, the return type can never be undefined
|
|
18
|
-
expectTypeOf(useObservable(observable, 'bar')).toEqualTypeOf<string | undefined>()
|
|
18
|
+
expectTypeOf(useObservable(observable, {initialValue: 'bar'})).toEqualTypeOf<string | undefined>()
|
|
19
19
|
})
|
|
20
20
|
|
|
21
21
|
test('useObservable with initial value if a different type returns a union of the observed type and the initial value type', () => {
|
|
22
22
|
const observable = of('foo')
|
|
23
23
|
|
|
24
|
-
expectTypeOf(useObservable(observable, 1)).toEqualTypeOf<string | number>()
|
|
25
|
-
expectTypeOf(useObservable(observable, () => 1)).toEqualTypeOf<string | number>()
|
|
26
|
-
expectTypeOf(useObservable(observable, 'foo')).toEqualTypeOf<string>()
|
|
24
|
+
expectTypeOf(useObservable(observable, {initialValue: 1})).toEqualTypeOf<string | number>()
|
|
25
|
+
expectTypeOf(useObservable(observable, {initialValue: () => 1})).toEqualTypeOf<string | number>()
|
|
26
|
+
expectTypeOf(useObservable(observable, {initialValue: 'foo'})).toEqualTypeOf<string>()
|
|
27
27
|
})
|
|
@@ -5,7 +5,7 @@ import {asyncScheduler, Observable, of, ReplaySubject, scheduled, share, Subject
|
|
|
5
5
|
import {map} from 'rxjs/operators'
|
|
6
6
|
import {expect, test} from 'vitest'
|
|
7
7
|
|
|
8
|
-
import {useObservable} from '../useObservable'
|
|
8
|
+
import {useObservable, type UseObservableOptions} from '../useObservable'
|
|
9
9
|
|
|
10
10
|
test('should subscribe immediately on component mount and unsubscribe on component unmount', async () => {
|
|
11
11
|
let subscribed = false
|
|
@@ -309,3 +309,67 @@ test('should throw during SSR if no initial value is defined', () => {
|
|
|
309
309
|
`[Error: Missing getServerSnapshot, which is required for server-rendered content. Will revert to client rendering.]`,
|
|
310
310
|
)
|
|
311
311
|
})
|
|
312
|
+
|
|
313
|
+
test('should not subscribe if the disabled prop is present', () => {
|
|
314
|
+
const values$ = new Subject<string | undefined>()
|
|
315
|
+
const {result, unmount} = renderHook(() => useObservable(values$, 'initial', {disabled: true}))
|
|
316
|
+
|
|
317
|
+
act(() => values$.next('something'))
|
|
318
|
+
expect(result.current).toBe('initial')
|
|
319
|
+
|
|
320
|
+
unmount()
|
|
321
|
+
})
|
|
322
|
+
|
|
323
|
+
test('should return the last value instead of the initial value when the hook is disabled after running', () => {
|
|
324
|
+
const values$ = new Subject<string | undefined>()
|
|
325
|
+
const {result, unmount, rerender} = renderHook<string | undefined, UseObservableOptions>(
|
|
326
|
+
(props) => useObservable(values$, 'initial', props),
|
|
327
|
+
)
|
|
328
|
+
expect(result.current).toBe('initial')
|
|
329
|
+
act(() => values$.next('something'))
|
|
330
|
+
expect(result.current).toBe('something')
|
|
331
|
+
|
|
332
|
+
rerender({
|
|
333
|
+
disabled: true,
|
|
334
|
+
})
|
|
335
|
+
|
|
336
|
+
act(() => values$.next('something else'))
|
|
337
|
+
|
|
338
|
+
expect(result.current).toBe('something')
|
|
339
|
+
|
|
340
|
+
unmount()
|
|
341
|
+
})
|
|
342
|
+
|
|
343
|
+
test('should return the actual value when the hook is disabled and then re-enabled', () => {
|
|
344
|
+
const values$ = new Subject<string | undefined>()
|
|
345
|
+
const {result, unmount, rerender} = renderHook<string | undefined, UseObservableOptions>(
|
|
346
|
+
(props) => useObservable(values$, 'initial', props),
|
|
347
|
+
)
|
|
348
|
+
expect(result.current).toBe('initial')
|
|
349
|
+
act(() => values$.next('something'))
|
|
350
|
+
expect(result.current).toBe('something')
|
|
351
|
+
|
|
352
|
+
rerender({
|
|
353
|
+
disabled: true,
|
|
354
|
+
})
|
|
355
|
+
|
|
356
|
+
act(() => values$.next('something else'))
|
|
357
|
+
|
|
358
|
+
expect(result.current).toBe('something')
|
|
359
|
+
|
|
360
|
+
act(() => values$.next('something again'))
|
|
361
|
+
|
|
362
|
+
expect(result.current).toBe('something')
|
|
363
|
+
|
|
364
|
+
rerender({
|
|
365
|
+
disabled: false,
|
|
366
|
+
})
|
|
367
|
+
|
|
368
|
+
expect(result.current).toBe('something again')
|
|
369
|
+
|
|
370
|
+
act(() => values$.next('something ending'))
|
|
371
|
+
|
|
372
|
+
expect(result.current).toBe('something ending')
|
|
373
|
+
|
|
374
|
+
unmount()
|
|
375
|
+
})
|
package/src/useObservable.ts
CHANGED
|
@@ -35,10 +35,16 @@ interface CacheRecord<T> {
|
|
|
35
35
|
|
|
36
36
|
const cache = new WeakMap<Observable<any>, CacheRecord<any>>()
|
|
37
37
|
|
|
38
|
+
/** @public */
|
|
39
|
+
export interface UseObservableOptions {
|
|
40
|
+
disabled?: boolean
|
|
41
|
+
}
|
|
42
|
+
|
|
38
43
|
/** @public */
|
|
39
44
|
export function useObservable<ObservableType extends Observable<any>>(
|
|
40
45
|
observable: ObservableType,
|
|
41
46
|
initialValue: ObservedValueOf<ObservableType> | (() => ObservedValueOf<ObservableType>),
|
|
47
|
+
options?: UseObservableOptions,
|
|
42
48
|
): ObservedValueOf<ObservableType>
|
|
43
49
|
/** @public */
|
|
44
50
|
export function useObservable<ObservableType extends Observable<any>>(
|
|
@@ -48,12 +54,16 @@ export function useObservable<ObservableType extends Observable<any>>(
|
|
|
48
54
|
export function useObservable<ObservableType extends Observable<any>, InitialValue>(
|
|
49
55
|
observable: ObservableType,
|
|
50
56
|
initialValue: InitialValue | (() => InitialValue),
|
|
57
|
+
options?: UseObservableOptions,
|
|
51
58
|
): InitialValue | ObservedValueOf<ObservableType>
|
|
52
59
|
/** @public */
|
|
53
60
|
export function useObservable<ObservableType extends Observable<any>, InitialValue>(
|
|
54
61
|
observable: ObservableType,
|
|
55
62
|
initialValue?: InitialValue | (() => InitialValue),
|
|
63
|
+
options: UseObservableOptions = {},
|
|
56
64
|
): InitialValue | ObservedValueOf<ObservableType> {
|
|
65
|
+
const {disabled = false} = options
|
|
66
|
+
|
|
57
67
|
const instance = useMemo(() => {
|
|
58
68
|
if (!cache.has(observable)) {
|
|
59
69
|
// This separate object is used as a stable reference to the cache entry's snapshot and error.
|
|
@@ -99,12 +109,16 @@ export function useObservable<ObservableType extends Observable<any>, InitialVal
|
|
|
99
109
|
|
|
100
110
|
const subscribe = useCallback(
|
|
101
111
|
(onStoreChange: () => void) => {
|
|
112
|
+
if (disabled) {
|
|
113
|
+
return () => {}
|
|
114
|
+
}
|
|
115
|
+
|
|
102
116
|
const subscription = instance.observable.subscribe(onStoreChange)
|
|
103
117
|
return () => {
|
|
104
118
|
subscription.unsubscribe()
|
|
105
119
|
}
|
|
106
120
|
},
|
|
107
|
-
[instance.observable],
|
|
121
|
+
[instance.observable, disabled],
|
|
108
122
|
)
|
|
109
123
|
|
|
110
124
|
return useSyncExternalStore<ObservedValueOf<ObservableType>>(
|