react-rx 4.1.2-canary.9 → 4.1.2
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 +50 -44
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +0 -2
- package/dist/index.d.ts +0 -2
- package/dist/index.js +51 -45
- package/dist/index.js.map +1 -1
- package/package.json +12 -10
- package/src/useObservable.ts +45 -54
package/dist/index.cjs
CHANGED
|
@@ -5,57 +5,63 @@ function getValue(value) {
|
|
|
5
5
|
return typeof value == "function" ? value() : value;
|
|
6
6
|
}
|
|
7
7
|
const cache = /* @__PURE__ */ new WeakMap();
|
|
8
|
-
function useObservable(observable, initialValue
|
|
9
|
-
const $ = reactCompilerRuntime.c(
|
|
10
|
-
let t0;
|
|
8
|
+
function useObservable(observable, initialValue) {
|
|
9
|
+
const $ = reactCompilerRuntime.c(9);
|
|
11
10
|
if (!cache.has(observable)) {
|
|
12
|
-
const
|
|
13
|
-
entry.observable = observable.pipe(operators.map((value) => ({
|
|
14
|
-
snapshot: value,
|
|
15
|
-
error: void 0
|
|
16
|
-
})), rxjs.catchError((error) => rxjs.of({
|
|
11
|
+
const state = {
|
|
17
12
|
snapshot: void 0,
|
|
18
|
-
error
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
13
|
+
error: void 0
|
|
14
|
+
}, entry = {
|
|
15
|
+
state,
|
|
16
|
+
observable: observable.pipe(operators.map(_temp$1), rxjs.catchError(_temp2), operators.tap((t02) => {
|
|
17
|
+
const {
|
|
18
|
+
snapshot,
|
|
19
|
+
error: error_0
|
|
20
|
+
} = t02;
|
|
21
|
+
state.snapshot = snapshot, state.error = error_0;
|
|
22
|
+
}), operators.map(_temp3), rxjs.finalize(() => cache.delete(observable)), rxjs.share({
|
|
23
|
+
resetOnRefCountZero: _temp4
|
|
24
|
+
})),
|
|
25
|
+
getSnapshot: (initialValue_0) => {
|
|
26
|
+
if (state.error)
|
|
27
|
+
throw state.error;
|
|
28
|
+
return state.snapshot ?? getValue(initialValue_0);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
entry.observable.subscribe().unsubscribe(), cache.set(observable, entry);
|
|
29
32
|
}
|
|
33
|
+
let t0;
|
|
34
|
+
$[0] !== observable ? (t0 = cache.get(observable), $[0] = observable, $[1] = t0) : t0 = $[1];
|
|
35
|
+
const instance = t0;
|
|
30
36
|
let t1;
|
|
31
|
-
$[
|
|
32
|
-
|
|
33
|
-
let t2;
|
|
34
|
-
$[2] !== debug || $[3] !== observable || $[4] !== instance.observable ? (t2 = (onStoreChange) => {
|
|
35
|
-
debug && console.log("subscribe", observable);
|
|
36
|
-
const subscription_0 = instance.observable.subscribe(() => {
|
|
37
|
-
debug && console.log("onStoreChange", observable), onStoreChange();
|
|
38
|
-
});
|
|
37
|
+
$[2] !== instance.observable ? (t1 = (onStoreChange) => {
|
|
38
|
+
const subscription_0 = instance.observable.subscribe(onStoreChange);
|
|
39
39
|
return () => {
|
|
40
|
-
|
|
40
|
+
subscription_0.unsubscribe();
|
|
41
41
|
};
|
|
42
|
-
}, $[2] =
|
|
42
|
+
}, $[2] = instance.observable, $[3] = t1) : t1 = $[3];
|
|
43
|
+
const subscribe = t1;
|
|
44
|
+
let t2;
|
|
45
|
+
$[4] !== instance || $[5] !== initialValue ? (t2 = () => instance.getSnapshot(initialValue), $[4] = instance, $[5] = initialValue, $[6] = t2) : t2 = $[6];
|
|
43
46
|
let t3;
|
|
44
|
-
$[
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
return
|
|
47
|
+
return $[7] !== initialValue ? (t3 = typeof initialValue > "u" ? void 0 : () => getValue(initialValue), $[7] = initialValue, $[8] = t3) : t3 = $[8], react.useSyncExternalStore(subscribe, t2, t3);
|
|
48
|
+
}
|
|
49
|
+
function _temp4() {
|
|
50
|
+
return rxjs.timer(0, rxjs.asapScheduler);
|
|
51
|
+
}
|
|
52
|
+
function _temp3(value_0) {
|
|
53
|
+
}
|
|
54
|
+
function _temp2(error) {
|
|
55
|
+
return rxjs.of({
|
|
56
|
+
snapshot: void 0,
|
|
57
|
+
error
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
function _temp$1(value) {
|
|
61
|
+
return {
|
|
62
|
+
snapshot: value,
|
|
63
|
+
error: void 0
|
|
64
|
+
};
|
|
59
65
|
}
|
|
60
66
|
function useObservableEvent(handleEvent) {
|
|
61
67
|
const $ = reactCompilerRuntime.c(6), [t0] = react.useState(_temp), [calls$, call] = t0;
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/useObservable.ts","../src/useObservableEvent.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/useObservable.ts","../src/useObservableEvent.ts"],"sourcesContent":["import {useCallback, 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() : value\n}\n\ninterface CacheRecord<T> {\n observable: Observable<void>\n state: {\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 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 = {\n snapshot: undefined as ObservedValueOf<ObservableType>,\n error: undefined,\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.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 state.snapshot ?? (getValue(initialValue) 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 as CacheRecord<ObservedValueOf<ObservableType>>)\n }\n const instance = cache.get(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$, onEvent])\n\n return call\n}\n"],"names":["getValue","value","cache","WeakMap","useObservable","observable","initialValue","$","_c","has","state","snapshot","undefined","error","entry","pipe","map","_temp","catchError","_temp2","tap","t0","error_0","_temp3","finalize","delete","share","resetOnRefCountZero","_temp4","getSnapshot","initialValue_0","subscribe","unsubscribe","set","get","instance","t1","onStoreChange","subscription_0","subscription","t2","t3","useSyncExternalStore","timer","asapScheduler","value_0","of","useObservableEvent","handleEvent","useState","calls$","call","onEvent","useEffectEvent","observable_0","useEffect","observableCallback"],"mappings":";;;AAaA,SAASA,SAAYC,OAA2C;AAC9D,SAAO,OAAOA,SAAU,aAAaA,MAAUA,IAAAA;AACjD;AAWA,MAAMC,4BAAYC,QAA2C;AAiBtDC,SAAAA,cAAAC,YAAAC,cAAA;AAAAC,QAAAA,IAAAC,uBAAA,CAAA;AAAA,MAAA,CAIAN,MAAAO,IAAUJ,UAAU,GAAC;AAGxB,UAAAK,QAAA;AAAA,MAAAC,UAAAC;AAAAA,MAAAC,OAAAD;AAAAA,OAIAE,QAAA;AAAA,MAAAJ;AAAAA,MAAAL,YAEcA,WAAUU,KACpBC,UAAAA,IAAAC,OAAoD,GACpDC,KAAAA,WAAAC,MAAsD,GACtDC,cAAAC,CAAAA,QAAA;AAAK,cAAA;AAAA,UAAAV;AAAAA,UAAAE,OAAAS;AAAAA,QAAAA,IAA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
package/dist/index.d.cts
CHANGED
|
@@ -5,7 +5,6 @@ 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
|
-
debug?: boolean,
|
|
9
8
|
): ObservedValueOf<ObservableType>
|
|
10
9
|
|
|
11
10
|
/** @public */
|
|
@@ -17,7 +16,6 @@ export declare function useObservable<ObservableType extends Observable<any>>(
|
|
|
17
16
|
export declare function useObservable<ObservableType extends Observable<any>, InitialValue>(
|
|
18
17
|
observable: ObservableType,
|
|
19
18
|
initialValue: InitialValue | (() => InitialValue),
|
|
20
|
-
debug?: boolean,
|
|
21
19
|
): InitialValue | ObservedValueOf<ObservableType>
|
|
22
20
|
|
|
23
21
|
/** @public */
|
package/dist/index.d.ts
CHANGED
|
@@ -5,7 +5,6 @@ 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
|
-
debug?: boolean,
|
|
9
8
|
): ObservedValueOf<ObservableType>
|
|
10
9
|
|
|
11
10
|
/** @public */
|
|
@@ -17,7 +16,6 @@ export declare function useObservable<ObservableType extends Observable<any>>(
|
|
|
17
16
|
export declare function useObservable<ObservableType extends Observable<any>, InitialValue>(
|
|
18
17
|
observable: ObservableType,
|
|
19
18
|
initialValue: InitialValue | (() => InitialValue),
|
|
20
|
-
debug?: boolean,
|
|
21
19
|
): InitialValue | ObservedValueOf<ObservableType>
|
|
22
20
|
|
|
23
21
|
/** @public */
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { c } from "react-compiler-runtime";
|
|
2
2
|
import { useSyncExternalStore, useState, useEffect } from "react";
|
|
3
|
-
import { catchError,
|
|
3
|
+
import { catchError, finalize, share, timer, asapScheduler, of } from "rxjs";
|
|
4
4
|
import { map, tap } from "rxjs/operators";
|
|
5
5
|
import { observableCallback } from "observable-callback";
|
|
6
6
|
import { useEffectEvent } from "use-effect-event";
|
|
@@ -8,57 +8,63 @@ function getValue(value) {
|
|
|
8
8
|
return typeof value == "function" ? value() : value;
|
|
9
9
|
}
|
|
10
10
|
const cache = /* @__PURE__ */ new WeakMap();
|
|
11
|
-
function useObservable(observable, initialValue
|
|
12
|
-
const $ = c(
|
|
13
|
-
let t0;
|
|
11
|
+
function useObservable(observable, initialValue) {
|
|
12
|
+
const $ = c(9);
|
|
14
13
|
if (!cache.has(observable)) {
|
|
15
|
-
const
|
|
16
|
-
entry.observable = observable.pipe(map((value) => ({
|
|
17
|
-
snapshot: value,
|
|
18
|
-
error: void 0
|
|
19
|
-
})), catchError((error) => of({
|
|
14
|
+
const state = {
|
|
20
15
|
snapshot: void 0,
|
|
21
|
-
error
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
16
|
+
error: void 0
|
|
17
|
+
}, entry = {
|
|
18
|
+
state,
|
|
19
|
+
observable: observable.pipe(map(_temp$1), catchError(_temp2), tap((t02) => {
|
|
20
|
+
const {
|
|
21
|
+
snapshot,
|
|
22
|
+
error: error_0
|
|
23
|
+
} = t02;
|
|
24
|
+
state.snapshot = snapshot, state.error = error_0;
|
|
25
|
+
}), map(_temp3), finalize(() => cache.delete(observable)), share({
|
|
26
|
+
resetOnRefCountZero: _temp4
|
|
27
|
+
})),
|
|
28
|
+
getSnapshot: (initialValue_0) => {
|
|
29
|
+
if (state.error)
|
|
30
|
+
throw state.error;
|
|
31
|
+
return state.snapshot ?? getValue(initialValue_0);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
entry.observable.subscribe().unsubscribe(), cache.set(observable, entry);
|
|
32
35
|
}
|
|
36
|
+
let t0;
|
|
37
|
+
$[0] !== observable ? (t0 = cache.get(observable), $[0] = observable, $[1] = t0) : t0 = $[1];
|
|
38
|
+
const instance = t0;
|
|
33
39
|
let t1;
|
|
34
|
-
$[
|
|
35
|
-
|
|
36
|
-
let t2;
|
|
37
|
-
$[2] !== debug || $[3] !== observable || $[4] !== instance.observable ? (t2 = (onStoreChange) => {
|
|
38
|
-
debug && console.log("subscribe", observable);
|
|
39
|
-
const subscription_0 = instance.observable.subscribe(() => {
|
|
40
|
-
debug && console.log("onStoreChange", observable), onStoreChange();
|
|
41
|
-
});
|
|
40
|
+
$[2] !== instance.observable ? (t1 = (onStoreChange) => {
|
|
41
|
+
const subscription_0 = instance.observable.subscribe(onStoreChange);
|
|
42
42
|
return () => {
|
|
43
|
-
|
|
43
|
+
subscription_0.unsubscribe();
|
|
44
44
|
};
|
|
45
|
-
}, $[2] =
|
|
45
|
+
}, $[2] = instance.observable, $[3] = t1) : t1 = $[3];
|
|
46
|
+
const subscribe = t1;
|
|
47
|
+
let t2;
|
|
48
|
+
$[4] !== instance || $[5] !== initialValue ? (t2 = () => instance.getSnapshot(initialValue), $[4] = instance, $[5] = initialValue, $[6] = t2) : t2 = $[6];
|
|
46
49
|
let t3;
|
|
47
|
-
$[
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
return
|
|
50
|
+
return $[7] !== initialValue ? (t3 = typeof initialValue > "u" ? void 0 : () => getValue(initialValue), $[7] = initialValue, $[8] = t3) : t3 = $[8], useSyncExternalStore(subscribe, t2, t3);
|
|
51
|
+
}
|
|
52
|
+
function _temp4() {
|
|
53
|
+
return timer(0, asapScheduler);
|
|
54
|
+
}
|
|
55
|
+
function _temp3(value_0) {
|
|
56
|
+
}
|
|
57
|
+
function _temp2(error) {
|
|
58
|
+
return of({
|
|
59
|
+
snapshot: void 0,
|
|
60
|
+
error
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
function _temp$1(value) {
|
|
64
|
+
return {
|
|
65
|
+
snapshot: value,
|
|
66
|
+
error: void 0
|
|
67
|
+
};
|
|
62
68
|
}
|
|
63
69
|
function useObservableEvent(handleEvent) {
|
|
64
70
|
const $ = c(6), [t0] = useState(_temp), [calls$, call] = t0;
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/useObservable.ts","../src/useObservableEvent.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/useObservable.ts","../src/useObservableEvent.ts"],"sourcesContent":["import {useCallback, 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() : value\n}\n\ninterface CacheRecord<T> {\n observable: Observable<void>\n state: {\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 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 = {\n snapshot: undefined as ObservedValueOf<ObservableType>,\n error: undefined,\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.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 state.snapshot ?? (getValue(initialValue) 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 as CacheRecord<ObservedValueOf<ObservableType>>)\n }\n const instance = cache.get(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$, onEvent])\n\n return call\n}\n"],"names":["getValue","value","cache","WeakMap","useObservable","observable","initialValue","$","_c","has","state","snapshot","undefined","error","entry","pipe","map","_temp","catchError","_temp2","tap","t0","error_0","_temp3","finalize","delete","share","resetOnRefCountZero","_temp4","getSnapshot","initialValue_0","subscribe","unsubscribe","set","get","instance","t1","onStoreChange","subscription_0","subscription","t2","t3","useSyncExternalStore","timer","asapScheduler","value_0","of","useObservableEvent","handleEvent","useState","calls$","call","onEvent","useEffectEvent","observable_0","useEffect","observableCallback"],"mappings":";;;;;;AAaA,SAASA,SAAYC,OAA2C;AAC9D,SAAO,OAAOA,SAAU,aAAaA,MAAUA,IAAAA;AACjD;AAWA,MAAMC,4BAAYC,QAA2C;AAiBtDC,SAAAA,cAAAC,YAAAC,cAAA;AAAAC,QAAAA,IAAAC,EAAA,CAAA;AAAA,MAAA,CAIAN,MAAAO,IAAUJ,UAAU,GAAC;AAGxB,UAAAK,QAAA;AAAA,MAAAC,UAAAC;AAAAA,MAAAC,OAAAD;AAAAA,OAIAE,QAAA;AAAA,MAAAJ;AAAAA,MAAAL,YAEcA,WAAUU,KACpBC,IAAAC,OAAoD,GACpDC,WAAAC,MAAsD,GACtDC,IAAAC,CAAAA,QAAA;AAAK,cAAA;AAAA,UAAAV;AAAAA,UAAAE,OAAAS;AAAAA,QAAAA,IAAAD;AACEV,cAAAA,WAAYA,UACjBD,MAAKG,QAASA;AAAAA,MAAAA,CACf,GAGDG,IAAAO,MAAyB,GAEzBC,SAAetB,MAAAA,MAAAuB,OAAapB,UAAU,CAAC,GACvCqB,MAAA;AAAA,QAAAC,qBAAAC;AAAAA,MAAAA,CAA0D,CAC5D;AAAA,MAACC,aAAAC,CAAA,mBAAA;AAAA,YAEKpB,MAAKG;AAAA,gB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SAAAI,QAAAhB,OAAA;AAAA,SAAA;AAAA,IAAAU,UAc4BV;AAAAA,IAAKY,OAAAD;AAAAA,EAAA;AAAA;ACnDjC,SAAAmC,mBAAAC,aAAA;AAAA,QAAAzC,IAAAC,EAAA,CAAA,GAGL,CAAAa,EAAA,IAAyB4B,SAAAhC,KAAsC,GAAxD,CAAAiC,QAAAC,IAAA,IAAA9B;AAAce,MAAAA;AAAA7B,WAAAyC,eAEUZ,KAAA/B,CAAAA,eAA+B2C,YAAY3C,UAAU,GAACE,OAAAyC,aAAAzC,OAAA6B,MAAAA,KAAA7B,EAAA,CAAA;AAArF6C,QAAAA,UAAgBC,eAAejB,EAAsD;AAAC,MAAAI,IAAAC;AAAAlC,SAAAA,EAAA2C,CAAAA,MAAAA,UAAA3C,SAAA6C,WAE5EZ,KAAAA,MAAA;AACRD,UAAAA,eAAqBW,OAAMnC,KAAAuC,CAAAA,iBAAsBF,QAAQ/C,YAAU,CAAC,EAAC0B,UAAW;AAAC,WAAA,MACpEQ,aAAYP,YAAa;AAAA,EACrCS,GAAAA,KAAA,CAACS,QAAQE,OAAO,GAAC7C,OAAA2C,QAAA3C,OAAA6C,SAAA7C,OAAAiC,IAAAjC,OAAAkC,OAAAD,KAAAjC,EAAA,CAAA,GAAAkC,KAAAlC,EAAA,CAAA,IAHpBgD,UAAUf,IAGPC,EAAiB,GAEbU;AAAI;AAZN,SAAAlC,QAAA;AAAA,SAGmCuC,mBAAsB;AAAC;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-rx",
|
|
3
|
-
"version": "4.1.2
|
|
3
|
+
"version": "4.1.2",
|
|
4
4
|
"description": "React + RxJS = <3",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"action",
|
|
@@ -62,6 +62,15 @@
|
|
|
62
62
|
"dist",
|
|
63
63
|
"src"
|
|
64
64
|
],
|
|
65
|
+
"scripts": {
|
|
66
|
+
"build": "pkg build --strict --clean --check",
|
|
67
|
+
"dev": "pnpm --filter 'react-rx-website' dev",
|
|
68
|
+
"format": "prettier --cache --write .",
|
|
69
|
+
"lint": "eslint --cache .",
|
|
70
|
+
"prepublishOnly": "pnpm build",
|
|
71
|
+
"test": "vitest run --typecheck",
|
|
72
|
+
"watch": "pnpm build -- --watch"
|
|
73
|
+
},
|
|
65
74
|
"browserslist": "extends @sanity/browserslist-config",
|
|
66
75
|
"prettier": "@sanity/prettier-config",
|
|
67
76
|
"dependencies": {
|
|
@@ -103,12 +112,5 @@
|
|
|
103
112
|
"react": "^18.3 || >=19.0.0-0",
|
|
104
113
|
"rxjs": "^7"
|
|
105
114
|
},
|
|
106
|
-
"
|
|
107
|
-
|
|
108
|
-
"dev": "pnpm --filter 'react-rx-website' dev",
|
|
109
|
-
"format": "prettier --cache --write .",
|
|
110
|
-
"lint": "eslint --cache .",
|
|
111
|
-
"test": "vitest run --typecheck",
|
|
112
|
-
"watch": "pnpm build -- --watch"
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
+
"packageManager": "pnpm@9.12.3"
|
|
116
|
+
}
|
package/src/useObservable.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {useCallback, useSyncExternalStore} from 'react'
|
|
2
2
|
import {
|
|
3
3
|
asapScheduler,
|
|
4
4
|
catchError,
|
|
@@ -17,8 +17,11 @@ function getValue<T>(value: T): T extends () => infer U ? U : T {
|
|
|
17
17
|
|
|
18
18
|
interface CacheRecord<T> {
|
|
19
19
|
observable: Observable<void>
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
state: {
|
|
21
|
+
snapshot: T
|
|
22
|
+
error?: unknown
|
|
23
|
+
}
|
|
24
|
+
getSnapshot: (initialValue: unknown) => T
|
|
22
25
|
}
|
|
23
26
|
|
|
24
27
|
const cache = new WeakMap<Observable<any>, CacheRecord<any>>()
|
|
@@ -27,7 +30,6 @@ const cache = new WeakMap<Observable<any>, CacheRecord<any>>()
|
|
|
27
30
|
export function useObservable<ObservableType extends Observable<any>>(
|
|
28
31
|
observable: ObservableType,
|
|
29
32
|
initialValue: ObservedValueOf<ObservableType> | (() => ObservedValueOf<ObservableType>),
|
|
30
|
-
debug?: boolean,
|
|
31
33
|
): ObservedValueOf<ObservableType>
|
|
32
34
|
/** @public */
|
|
33
35
|
export function useObservable<ObservableType extends Observable<any>>(
|
|
@@ -37,26 +39,27 @@ export function useObservable<ObservableType extends Observable<any>>(
|
|
|
37
39
|
export function useObservable<ObservableType extends Observable<any>, InitialValue>(
|
|
38
40
|
observable: ObservableType,
|
|
39
41
|
initialValue: InitialValue | (() => InitialValue),
|
|
40
|
-
debug?: boolean,
|
|
41
42
|
): InitialValue | ObservedValueOf<ObservableType>
|
|
42
43
|
/** @public */
|
|
43
44
|
export function useObservable<ObservableType extends Observable<any>, InitialValue>(
|
|
44
45
|
observable: ObservableType,
|
|
45
46
|
initialValue?: InitialValue | (() => InitialValue),
|
|
46
|
-
debug?: boolean,
|
|
47
47
|
): InitialValue | ObservedValueOf<ObservableType> {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
48
|
+
if (!cache.has(observable)) {
|
|
49
|
+
// This separate object is used as a stable reference to the cache entry's snapshot and error.
|
|
50
|
+
// It's used by the `getSnapshot` closure.
|
|
51
|
+
const state = {
|
|
52
|
+
snapshot: undefined as ObservedValueOf<ObservableType>,
|
|
53
|
+
error: undefined,
|
|
54
|
+
}
|
|
55
|
+
const entry: CacheRecord<ObservedValueOf<ObservableType>> = {
|
|
56
|
+
state,
|
|
57
|
+
observable: observable.pipe(
|
|
52
58
|
map((value) => ({snapshot: value, error: undefined})),
|
|
53
59
|
catchError((error) => of({snapshot: undefined, error})),
|
|
54
60
|
tap(({snapshot, error}) => {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
}
|
|
58
|
-
entry.snapshot = snapshot
|
|
59
|
-
entry.error = error
|
|
61
|
+
state.snapshot = snapshot
|
|
62
|
+
state.error = error
|
|
60
63
|
}),
|
|
61
64
|
// Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state
|
|
62
65
|
// and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.
|
|
@@ -64,50 +67,38 @@ export function useObservable<ObservableType extends Observable<any>, InitialVal
|
|
|
64
67
|
// Ensure that the cache entry is deleted when the observable completes or errors.
|
|
65
68
|
finalize(() => cache.delete(observable)),
|
|
66
69
|
share({resetOnRefCountZero: () => timer(0, asapScheduler)}),
|
|
67
|
-
)
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
subscription.unsubscribe()
|
|
72
|
-
|
|
73
|
-
cache.set(observable, entry as CacheRecord<ObservedValueOf<ObservableType>>)
|
|
74
|
-
}
|
|
75
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
76
|
-
const instance = cache.get(observable)!
|
|
77
|
-
|
|
78
|
-
return {
|
|
79
|
-
subscribe: (onStoreChange: () => void) => {
|
|
80
|
-
if (debug) {
|
|
81
|
-
console.log('subscribe', observable)
|
|
82
|
-
}
|
|
83
|
-
const subscription = instance.observable.subscribe(() => {
|
|
84
|
-
if (debug) {
|
|
85
|
-
console.log('onStoreChange', observable)
|
|
86
|
-
}
|
|
87
|
-
onStoreChange()
|
|
88
|
-
})
|
|
89
|
-
return () => {
|
|
90
|
-
if (debug) {
|
|
91
|
-
console.log('unsubscribe', observable)
|
|
92
|
-
}
|
|
93
|
-
subscription.unsubscribe()
|
|
70
|
+
),
|
|
71
|
+
getSnapshot: (initialValue) => {
|
|
72
|
+
if (state.error) {
|
|
73
|
+
throw state.error
|
|
94
74
|
}
|
|
95
|
-
|
|
96
|
-
getSnapshot: () => {
|
|
97
|
-
if (debug) {
|
|
98
|
-
console.log('getSnapshot', instance.snapshot, instance.error)
|
|
99
|
-
}
|
|
100
|
-
if (instance.error) {
|
|
101
|
-
throw instance.error
|
|
102
|
-
}
|
|
103
|
-
return instance.snapshot
|
|
75
|
+
return state.snapshot ?? (getValue(initialValue) as ObservedValueOf<ObservableType>)
|
|
104
76
|
},
|
|
105
77
|
}
|
|
106
|
-
|
|
78
|
+
|
|
79
|
+
// Eagerly subscribe to sync set `state.snapshot` to what the observable returns, and keep the observable alive until the component unmounts.
|
|
80
|
+
const subscription = entry.observable.subscribe()
|
|
81
|
+
subscription.unsubscribe()
|
|
82
|
+
|
|
83
|
+
cache.set(observable, entry as CacheRecord<ObservedValueOf<ObservableType>>)
|
|
84
|
+
}
|
|
85
|
+
const instance = cache.get(observable)!
|
|
86
|
+
|
|
87
|
+
const subscribe = useCallback(
|
|
88
|
+
(onStoreChange: () => void) => {
|
|
89
|
+
const subscription = instance.observable.subscribe(onStoreChange)
|
|
90
|
+
return () => {
|
|
91
|
+
subscription.unsubscribe()
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
[instance.observable],
|
|
95
|
+
)
|
|
107
96
|
|
|
108
97
|
return useSyncExternalStore<ObservedValueOf<ObservableType>>(
|
|
109
|
-
|
|
110
|
-
() =>
|
|
98
|
+
subscribe,
|
|
99
|
+
() => {
|
|
100
|
+
return instance.getSnapshot(initialValue)
|
|
101
|
+
},
|
|
111
102
|
typeof initialValue === 'undefined'
|
|
112
103
|
? undefined
|
|
113
104
|
: () => getValue(initialValue) as ObservedValueOf<ObservableType>,
|