react-rx 4.1.1 → 4.1.2-canary.1
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 +48 -62
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +49 -64
- package/dist/index.js.map +1 -1
- package/package.json +10 -12
- package/src/useObservable.ts +53 -44
package/dist/index.cjs
CHANGED
|
@@ -1,76 +1,62 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: !0 });
|
|
3
|
-
var
|
|
3
|
+
var react = require("react"), rxjs = require("rxjs"), operators = require("rxjs/operators"), observableCallback = require("observable-callback"), useEffectEvent = require("use-effect-event");
|
|
4
4
|
function getValue(value) {
|
|
5
5
|
return typeof value == "function" ? value() : value;
|
|
6
6
|
}
|
|
7
7
|
const cache = /* @__PURE__ */ new WeakMap();
|
|
8
8
|
function useObservable(observable, initialValue) {
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
const [getInitialValue] = react.useState(
|
|
10
|
+
() => () => getValue(initialValue)
|
|
11
|
+
), store = react.useMemo(() => {
|
|
12
|
+
if (!cache.has(observable)) {
|
|
13
|
+
const entry = {
|
|
14
|
+
snapshot: getInitialValue()
|
|
15
|
+
};
|
|
16
|
+
entry.observable = observable.pipe(
|
|
17
|
+
operators.map((value) => ({ snapshot: value, error: void 0 })),
|
|
18
|
+
rxjs.catchError((error) => rxjs.of({ snapshot: void 0, error })),
|
|
19
|
+
operators.tap(({ snapshot, error }) => {
|
|
20
|
+
entry.snapshot = snapshot, entry.error = error;
|
|
21
|
+
}),
|
|
22
|
+
// Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state
|
|
23
|
+
// and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.
|
|
24
|
+
operators.map((value) => {
|
|
25
|
+
}),
|
|
26
|
+
// Ensure that the cache entry is deleted when the observable completes or errors.
|
|
27
|
+
rxjs.finalize(() => cache.delete(observable)),
|
|
28
|
+
rxjs.share({ resetOnRefCountZero: () => rxjs.timer(0, rxjs.asapScheduler) })
|
|
29
|
+
), entry.observable.subscribe().unsubscribe(), cache.set(observable, entry);
|
|
30
|
+
}
|
|
31
|
+
const instance = cache.get(observable);
|
|
32
|
+
return {
|
|
33
|
+
subscribe: (onStoreChange) => {
|
|
34
|
+
const subscription = instance.observable.subscribe(onStoreChange);
|
|
35
|
+
return () => {
|
|
36
|
+
subscription.unsubscribe();
|
|
37
|
+
};
|
|
38
|
+
},
|
|
39
|
+
getSnapshot: () => {
|
|
40
|
+
if (instance.error)
|
|
41
|
+
throw instance.error;
|
|
42
|
+
return instance.snapshot;
|
|
43
|
+
}
|
|
13
44
|
};
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
})), entry.observable.subscribe().unsubscribe(), cache.set(observable, entry);
|
|
23
|
-
}
|
|
24
|
-
let t0;
|
|
25
|
-
$[0] !== observable ? (t0 = cache.get(observable), $[0] = observable, $[1] = t0) : t0 = $[1];
|
|
26
|
-
const instance = t0;
|
|
27
|
-
let t1;
|
|
28
|
-
$[2] !== instance.observable ? (t1 = (onStoreChange) => {
|
|
29
|
-
const subscription_0 = instance.observable.subscribe(onStoreChange);
|
|
30
|
-
return () => {
|
|
31
|
-
subscription_0.unsubscribe();
|
|
32
|
-
};
|
|
33
|
-
}, $[2] = instance.observable, $[3] = t1) : t1 = $[3];
|
|
34
|
-
const subscribe = t1;
|
|
35
|
-
let t2;
|
|
36
|
-
$[4] !== instance.error || $[5] !== instance.snapshot ? (t2 = () => {
|
|
37
|
-
if (instance.error)
|
|
38
|
-
throw instance.error;
|
|
39
|
-
return instance.snapshot;
|
|
40
|
-
}, $[4] = instance.error, $[5] = instance.snapshot, $[6] = t2) : t2 = $[6];
|
|
41
|
-
let t3;
|
|
42
|
-
return $[7] !== initialValue ? (t3 = typeof initialValue > "u" ? void 0 : () => getValue(initialValue), $[7] = initialValue, $[8] = t3) : t3 = $[8], react.useSyncExternalStore(subscribe, t2, t3);
|
|
43
|
-
}
|
|
44
|
-
function _temp4() {
|
|
45
|
-
return rxjs.timer(0, rxjs.asapScheduler);
|
|
46
|
-
}
|
|
47
|
-
function _temp3(value_0) {
|
|
48
|
-
}
|
|
49
|
-
function _temp2(error) {
|
|
50
|
-
return rxjs.of({
|
|
51
|
-
snapshot: void 0,
|
|
52
|
-
error
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
function _temp$1(value) {
|
|
56
|
-
return {
|
|
57
|
-
snapshot: value,
|
|
58
|
-
error: void 0
|
|
59
|
-
};
|
|
45
|
+
}, [observable, getInitialValue]);
|
|
46
|
+
return react.useEffect(() => {
|
|
47
|
+
console.log("stor.subscribe changed", store.subscribe);
|
|
48
|
+
}, [store.subscribe]), react.useSyncExternalStore(
|
|
49
|
+
store.subscribe,
|
|
50
|
+
store.getSnapshot,
|
|
51
|
+
typeof initialValue > "u" ? void 0 : () => getInitialValue()
|
|
52
|
+
);
|
|
60
53
|
}
|
|
61
54
|
function useObservableEvent(handleEvent) {
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const onEvent = useEffectEvent.useEffectEvent(t1);
|
|
66
|
-
let t2, t3;
|
|
67
|
-
return $[2] !== calls$ || $[3] !== onEvent ? (t2 = () => {
|
|
68
|
-
const subscription = calls$.pipe((observable_0) => onEvent(observable_0)).subscribe();
|
|
55
|
+
const [[calls$, call]] = react.useState(() => observableCallback.observableCallback()), onEvent = useEffectEvent.useEffectEvent((observable) => handleEvent(observable));
|
|
56
|
+
return react.useEffect(() => {
|
|
57
|
+
const subscription = calls$.pipe((observable) => onEvent(observable)).subscribe();
|
|
69
58
|
return () => subscription.unsubscribe();
|
|
70
|
-
},
|
|
71
|
-
}
|
|
72
|
-
function _temp() {
|
|
73
|
-
return observableCallback.observableCallback();
|
|
59
|
+
}, [calls$, onEvent]), call;
|
|
74
60
|
}
|
|
75
61
|
exports.useObservable = useObservable;
|
|
76
62
|
exports.useObservableEvent = useObservableEvent;
|
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 {useEffect, useMemo, useState, 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 snapshot: T\n error?: unknown\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 [getInitialValue] = useState(\n () => () => getValue(initialValue) as ObservedValueOf<ObservableType>,\n )\n\n const store = useMemo(() => {\n if (!cache.has(observable)) {\n const entry: Partial<CacheRecord<ObservedValueOf<ObservableType>>> = {\n snapshot: getInitialValue(),\n }\n entry.observable = observable.pipe(\n map((value) => ({snapshot: value, error: undefined})),\n catchError((error) => of({snapshot: undefined, error})),\n tap(({snapshot, error}) => {\n entry.snapshot = snapshot\n entry.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\n // Eagerly subscribe to sync set `entry.currentValue` 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 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const instance = cache.get(observable)!\n\n return {\n subscribe: (onStoreChange: () => void) => {\n const subscription = instance.observable.subscribe(onStoreChange)\n return () => {\n subscription.unsubscribe()\n }\n },\n getSnapshot: () => {\n if (instance.error) {\n throw instance.error\n }\n return instance.snapshot\n },\n }\n }, [observable, getInitialValue])\n\n useEffect(() => {\n console.log('stor.subscribe changed', store.subscribe)\n }, [store.subscribe])\n\n return useSyncExternalStore<ObservedValueOf<ObservableType>>(\n store.subscribe,\n store.getSnapshot,\n typeof initialValue === 'undefined' ? undefined : () => getInitialValue(),\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":["useState","useMemo","map","catchError","of","tap","finalize","share","timer","asapScheduler","useEffect","useSyncExternalStore","observableCallback","useEffectEvent"],"mappings":";;;AAaA,SAAS,SAAY,OAA2C;AAC9D,SAAO,OAAO,SAAU,aAAa,MAAU,IAAA;AACjD;AAQA,MAAM,4BAAY,QAA2C;AAiB7C,SAAA,cACd,YACA,cACgD;AAC1C,QAAA,CAAC,eAAe,IAAIA,MAAA;AAAA,IACxB,MAAM,MAAM,SAAS,YAAY;AAAA,EAAA,GAG7B,QAAQC,MAAAA,QAAQ,MAAM;AAC1B,QAAI,CAAC,MAAM,IAAI,UAAU,GAAG;AAC1B,YAAM,QAA+D;AAAA,QACnE,UAAU,gBAAgB;AAAA,MAC5B;AACA,YAAM,aAAa,WAAW;AAAA,QAC5BC,cAAI,CAAC,WAAW,EAAC,UAAU,OAAO,OAAO,SAAW;AAAA,QACpDC,gBAAW,CAAC,UAAUC,QAAG,EAAC,UAAU,QAAW,MAAK,CAAC,CAAC;AAAA,QACtDC,UAAAA,IAAI,CAAC,EAAC,UAAU,YAAW;AACnB,gBAAA,WAAW,UACjB,MAAM,QAAQ;AAAA,QAAA,CACf;AAAA;AAAA;AAAA,QAGDH,UAAA,IAAI,CAAC,UAAO;AAAA,QAAA,CAAa;AAAA;AAAA,QAEzBI,KAAAA,SAAS,MAAM,MAAM,OAAO,UAAU,CAAC;AAAA,QACvCC,KAAA,MAAM,EAAC,qBAAqB,MAAMC,WAAM,GAAGC,KAAa,aAAA,EAAE,CAAA;AAAA,MAAA,GAIvC,MAAM,WAAW,YACzB,YAEb,GAAA,MAAM,IAAI,YAAY,KAAqD;AAAA,IAAA;AAGvE,UAAA,WAAW,MAAM,IAAI,UAAU;AAE9B,WAAA;AAAA,MACL,WAAW,CAAC,kBAA8B;AACxC,cAAM,eAAe,SAAS,WAAW,UAAU,aAAa;AAChE,eAAO,MAAM;AACX,uBAAa,YAAY;AAAA,QAC3B;AAAA,MACF;AAAA,MACA,aAAa,MAAM;AACjB,YAAI,SAAS;AACX,gBAAM,SAAS;AAEjB,eAAO,SAAS;AAAA,MAAA;AAAA,IAEpB;AAAA,EAAA,GACC,CAAC,YAAY,eAAe,CAAC;AAEhC,SAAAC,MAAA,UAAU,MAAM;AACN,YAAA,IAAI,0BAA0B,MAAM,SAAS;AAAA,EACpD,GAAA,CAAC,MAAM,SAAS,CAAC,GAEbC,MAAA;AAAA,IACL,MAAM;AAAA,IACN,MAAM;AAAA,IACN,OAAO,eAAiB,MAAc,SAAY,MAAM,gBAAgB;AAAA,EAC1E;AACF;AChGO,SAAS,mBACd,aACkB;AAClB,QAAM,CAAC,CAAC,QAAQ,IAAI,CAAC,IAAIX,MAAS,SAAA,MAAMY,mBAAAA,mBAAuB,CAAA,GAEzD,UAAUC,eAAAA,eAAe,CAAC,eAA8B,YAAY,UAAU,CAAC;AAErF,SAAAH,MAAA,UAAU,MAAM;AACR,UAAA,eAAe,OAAO,KAAK,CAAC,eAAe,QAAQ,UAAU,CAAC,EAAE,UAAU;AACzE,WAAA,MAAM,aAAa,YAAY;AAAA,EACrC,GAAA,CAAC,QAAQ,OAAO,CAAC,GAEb;AACT;;;"}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { catchError, finalize, share, timer, asapScheduler, of } from "rxjs";
|
|
1
|
+
import { useState, useMemo, useEffect, useSyncExternalStore } from "react";
|
|
2
|
+
import { catchError, of, finalize, share, timer, asapScheduler } from "rxjs";
|
|
4
3
|
import { map, tap } from "rxjs/operators";
|
|
5
4
|
import { observableCallback } from "observable-callback";
|
|
6
5
|
import { useEffectEvent } from "use-effect-event";
|
|
@@ -9,71 +8,57 @@ function getValue(value) {
|
|
|
9
8
|
}
|
|
10
9
|
const cache = /* @__PURE__ */ new WeakMap();
|
|
11
10
|
function useObservable(observable, initialValue) {
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
11
|
+
const [getInitialValue] = useState(
|
|
12
|
+
() => () => getValue(initialValue)
|
|
13
|
+
), store = useMemo(() => {
|
|
14
|
+
if (!cache.has(observable)) {
|
|
15
|
+
const entry = {
|
|
16
|
+
snapshot: getInitialValue()
|
|
17
|
+
};
|
|
18
|
+
entry.observable = observable.pipe(
|
|
19
|
+
map((value) => ({ snapshot: value, error: void 0 })),
|
|
20
|
+
catchError((error) => of({ snapshot: void 0, error })),
|
|
21
|
+
tap(({ snapshot, error }) => {
|
|
22
|
+
entry.snapshot = snapshot, entry.error = error;
|
|
23
|
+
}),
|
|
24
|
+
// Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state
|
|
25
|
+
// and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.
|
|
26
|
+
map((value) => {
|
|
27
|
+
}),
|
|
28
|
+
// Ensure that the cache entry is deleted when the observable completes or errors.
|
|
29
|
+
finalize(() => cache.delete(observable)),
|
|
30
|
+
share({ resetOnRefCountZero: () => timer(0, asapScheduler) })
|
|
31
|
+
), entry.observable.subscribe().unsubscribe(), cache.set(observable, entry);
|
|
32
|
+
}
|
|
33
|
+
const instance = cache.get(observable);
|
|
34
|
+
return {
|
|
35
|
+
subscribe: (onStoreChange) => {
|
|
36
|
+
const subscription = instance.observable.subscribe(onStoreChange);
|
|
37
|
+
return () => {
|
|
38
|
+
subscription.unsubscribe();
|
|
39
|
+
};
|
|
40
|
+
},
|
|
41
|
+
getSnapshot: () => {
|
|
42
|
+
if (instance.error)
|
|
43
|
+
throw instance.error;
|
|
44
|
+
return instance.snapshot;
|
|
45
|
+
}
|
|
16
46
|
};
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
})), entry.observable.subscribe().unsubscribe(), cache.set(observable, entry);
|
|
26
|
-
}
|
|
27
|
-
let t0;
|
|
28
|
-
$[0] !== observable ? (t0 = cache.get(observable), $[0] = observable, $[1] = t0) : t0 = $[1];
|
|
29
|
-
const instance = t0;
|
|
30
|
-
let t1;
|
|
31
|
-
$[2] !== instance.observable ? (t1 = (onStoreChange) => {
|
|
32
|
-
const subscription_0 = instance.observable.subscribe(onStoreChange);
|
|
33
|
-
return () => {
|
|
34
|
-
subscription_0.unsubscribe();
|
|
35
|
-
};
|
|
36
|
-
}, $[2] = instance.observable, $[3] = t1) : t1 = $[3];
|
|
37
|
-
const subscribe = t1;
|
|
38
|
-
let t2;
|
|
39
|
-
$[4] !== instance.error || $[5] !== instance.snapshot ? (t2 = () => {
|
|
40
|
-
if (instance.error)
|
|
41
|
-
throw instance.error;
|
|
42
|
-
return instance.snapshot;
|
|
43
|
-
}, $[4] = instance.error, $[5] = instance.snapshot, $[6] = t2) : t2 = $[6];
|
|
44
|
-
let t3;
|
|
45
|
-
return $[7] !== initialValue ? (t3 = typeof initialValue > "u" ? void 0 : () => getValue(initialValue), $[7] = initialValue, $[8] = t3) : t3 = $[8], useSyncExternalStore(subscribe, t2, t3);
|
|
46
|
-
}
|
|
47
|
-
function _temp4() {
|
|
48
|
-
return timer(0, asapScheduler);
|
|
49
|
-
}
|
|
50
|
-
function _temp3(value_0) {
|
|
51
|
-
}
|
|
52
|
-
function _temp2(error) {
|
|
53
|
-
return of({
|
|
54
|
-
snapshot: void 0,
|
|
55
|
-
error
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
function _temp$1(value) {
|
|
59
|
-
return {
|
|
60
|
-
snapshot: value,
|
|
61
|
-
error: void 0
|
|
62
|
-
};
|
|
47
|
+
}, [observable, getInitialValue]);
|
|
48
|
+
return useEffect(() => {
|
|
49
|
+
console.log("stor.subscribe changed", store.subscribe);
|
|
50
|
+
}, [store.subscribe]), useSyncExternalStore(
|
|
51
|
+
store.subscribe,
|
|
52
|
+
store.getSnapshot,
|
|
53
|
+
typeof initialValue > "u" ? void 0 : () => getInitialValue()
|
|
54
|
+
);
|
|
63
55
|
}
|
|
64
56
|
function useObservableEvent(handleEvent) {
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
const onEvent = useEffectEvent(t1);
|
|
69
|
-
let t2, t3;
|
|
70
|
-
return $[2] !== calls$ || $[3] !== onEvent ? (t2 = () => {
|
|
71
|
-
const subscription = calls$.pipe((observable_0) => onEvent(observable_0)).subscribe();
|
|
57
|
+
const [[calls$, call]] = useState(() => observableCallback()), onEvent = useEffectEvent((observable) => handleEvent(observable));
|
|
58
|
+
return useEffect(() => {
|
|
59
|
+
const subscription = calls$.pipe((observable) => onEvent(observable)).subscribe();
|
|
72
60
|
return () => subscription.unsubscribe();
|
|
73
|
-
},
|
|
74
|
-
}
|
|
75
|
-
function _temp() {
|
|
76
|
-
return observableCallback();
|
|
61
|
+
}, [calls$, onEvent]), call;
|
|
77
62
|
}
|
|
78
63
|
export {
|
|
79
64
|
useObservable,
|
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 {useEffect, useMemo, useState, 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 snapshot: T\n error?: unknown\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 [getInitialValue] = useState(\n () => () => getValue(initialValue) as ObservedValueOf<ObservableType>,\n )\n\n const store = useMemo(() => {\n if (!cache.has(observable)) {\n const entry: Partial<CacheRecord<ObservedValueOf<ObservableType>>> = {\n snapshot: getInitialValue(),\n }\n entry.observable = observable.pipe(\n map((value) => ({snapshot: value, error: undefined})),\n catchError((error) => of({snapshot: undefined, error})),\n tap(({snapshot, error}) => {\n entry.snapshot = snapshot\n entry.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\n // Eagerly subscribe to sync set `entry.currentValue` 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 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const instance = cache.get(observable)!\n\n return {\n subscribe: (onStoreChange: () => void) => {\n const subscription = instance.observable.subscribe(onStoreChange)\n return () => {\n subscription.unsubscribe()\n }\n },\n getSnapshot: () => {\n if (instance.error) {\n throw instance.error\n }\n return instance.snapshot\n },\n }\n }, [observable, getInitialValue])\n\n useEffect(() => {\n console.log('stor.subscribe changed', store.subscribe)\n }, [store.subscribe])\n\n return useSyncExternalStore<ObservedValueOf<ObservableType>>(\n store.subscribe,\n store.getSnapshot,\n typeof initialValue === 'undefined' ? undefined : () => getInitialValue(),\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":[],"mappings":";;;;;AAaA,SAAS,SAAY,OAA2C;AAC9D,SAAO,OAAO,SAAU,aAAa,MAAU,IAAA;AACjD;AAQA,MAAM,4BAAY,QAA2C;AAiB7C,SAAA,cACd,YACA,cACgD;AAC1C,QAAA,CAAC,eAAe,IAAI;AAAA,IACxB,MAAM,MAAM,SAAS,YAAY;AAAA,EAAA,GAG7B,QAAQ,QAAQ,MAAM;AAC1B,QAAI,CAAC,MAAM,IAAI,UAAU,GAAG;AAC1B,YAAM,QAA+D;AAAA,QACnE,UAAU,gBAAgB;AAAA,MAC5B;AACA,YAAM,aAAa,WAAW;AAAA,QAC5B,IAAI,CAAC,WAAW,EAAC,UAAU,OAAO,OAAO,SAAW;AAAA,QACpD,WAAW,CAAC,UAAU,GAAG,EAAC,UAAU,QAAW,MAAK,CAAC,CAAC;AAAA,QACtD,IAAI,CAAC,EAAC,UAAU,YAAW;AACnB,gBAAA,WAAW,UACjB,MAAM,QAAQ;AAAA,QAAA,CACf;AAAA;AAAA;AAAA,QAGD,IAAI,CAAC,UAAO;AAAA,QAAA,CAAa;AAAA;AAAA,QAEzB,SAAS,MAAM,MAAM,OAAO,UAAU,CAAC;AAAA,QACvC,MAAM,EAAC,qBAAqB,MAAM,MAAM,GAAG,aAAa,EAAE,CAAA;AAAA,MAAA,GAIvC,MAAM,WAAW,YACzB,YAEb,GAAA,MAAM,IAAI,YAAY,KAAqD;AAAA,IAAA;AAGvE,UAAA,WAAW,MAAM,IAAI,UAAU;AAE9B,WAAA;AAAA,MACL,WAAW,CAAC,kBAA8B;AACxC,cAAM,eAAe,SAAS,WAAW,UAAU,aAAa;AAChE,eAAO,MAAM;AACX,uBAAa,YAAY;AAAA,QAC3B;AAAA,MACF;AAAA,MACA,aAAa,MAAM;AACjB,YAAI,SAAS;AACX,gBAAM,SAAS;AAEjB,eAAO,SAAS;AAAA,MAAA;AAAA,IAEpB;AAAA,EAAA,GACC,CAAC,YAAY,eAAe,CAAC;AAEhC,SAAA,UAAU,MAAM;AACN,YAAA,IAAI,0BAA0B,MAAM,SAAS;AAAA,EACpD,GAAA,CAAC,MAAM,SAAS,CAAC,GAEb;AAAA,IACL,MAAM;AAAA,IACN,MAAM;AAAA,IACN,OAAO,eAAiB,MAAc,SAAY,MAAM,gBAAgB;AAAA,EAC1E;AACF;AChGO,SAAS,mBACd,aACkB;AAClB,QAAM,CAAC,CAAC,QAAQ,IAAI,CAAC,IAAI,SAAS,MAAM,mBAAuB,CAAA,GAEzD,UAAU,eAAe,CAAC,eAA8B,YAAY,UAAU,CAAC;AAErF,SAAA,UAAU,MAAM;AACR,UAAA,eAAe,OAAO,KAAK,CAAC,eAAe,QAAQ,UAAU,CAAC,EAAE,UAAU;AACzE,WAAA,MAAM,aAAa,YAAY;AAAA,EACrC,GAAA,CAAC,QAAQ,OAAO,CAAC,GAEb;AACT;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-rx",
|
|
3
|
-
"version": "4.1.1",
|
|
3
|
+
"version": "4.1.2-canary.1",
|
|
4
4
|
"description": "React + RxJS = <3",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"action",
|
|
@@ -62,15 +62,6 @@
|
|
|
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
|
-
},
|
|
74
65
|
"browserslist": "extends @sanity/browserslist-config",
|
|
75
66
|
"prettier": "@sanity/prettier-config",
|
|
76
67
|
"dependencies": {
|
|
@@ -112,5 +103,12 @@
|
|
|
112
103
|
"react": "^18.3 || >=19.0.0-0",
|
|
113
104
|
"rxjs": "^7"
|
|
114
105
|
},
|
|
115
|
-
"
|
|
116
|
-
|
|
106
|
+
"scripts": {
|
|
107
|
+
"build": "pkg build --strict --clean --check",
|
|
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
|
+
}
|
package/src/useObservable.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {useEffect, useMemo, useState, useSyncExternalStore} from 'react'
|
|
2
2
|
import {
|
|
3
3
|
asapScheduler,
|
|
4
4
|
catchError,
|
|
@@ -42,53 +42,62 @@ export function useObservable<ObservableType extends Observable<any>, InitialVal
|
|
|
42
42
|
observable: ObservableType,
|
|
43
43
|
initialValue?: InitialValue | (() => InitialValue),
|
|
44
44
|
): InitialValue | ObservedValueOf<ObservableType> {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
entry.observable = observable.pipe(
|
|
50
|
-
map((value) => ({snapshot: value, error: undefined})),
|
|
51
|
-
catchError((error) => of({snapshot: undefined, error})),
|
|
52
|
-
tap(({snapshot, error}) => {
|
|
53
|
-
entry.snapshot = snapshot
|
|
54
|
-
entry.error = error
|
|
55
|
-
}),
|
|
56
|
-
// Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state
|
|
57
|
-
// and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.
|
|
58
|
-
map((value) => void value),
|
|
59
|
-
// Ensure that the cache entry is deleted when the observable completes or errors.
|
|
60
|
-
finalize(() => cache.delete(observable)),
|
|
61
|
-
share({resetOnRefCountZero: () => timer(0, asapScheduler)}),
|
|
62
|
-
)
|
|
45
|
+
const [getInitialValue] = useState(
|
|
46
|
+
() => () => getValue(initialValue) as ObservedValueOf<ObservableType>,
|
|
47
|
+
)
|
|
63
48
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
49
|
+
const store = useMemo(() => {
|
|
50
|
+
if (!cache.has(observable)) {
|
|
51
|
+
const entry: Partial<CacheRecord<ObservedValueOf<ObservableType>>> = {
|
|
52
|
+
snapshot: getInitialValue(),
|
|
53
|
+
}
|
|
54
|
+
entry.observable = observable.pipe(
|
|
55
|
+
map((value) => ({snapshot: value, error: undefined})),
|
|
56
|
+
catchError((error) => of({snapshot: undefined, error})),
|
|
57
|
+
tap(({snapshot, error}) => {
|
|
58
|
+
entry.snapshot = snapshot
|
|
59
|
+
entry.error = error
|
|
60
|
+
}),
|
|
61
|
+
// Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state
|
|
62
|
+
// and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.
|
|
63
|
+
map((value) => void value),
|
|
64
|
+
// Ensure that the cache entry is deleted when the observable completes or errors.
|
|
65
|
+
finalize(() => cache.delete(observable)),
|
|
66
|
+
share({resetOnRefCountZero: () => timer(0, asapScheduler)}),
|
|
67
|
+
)
|
|
67
68
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
69
|
+
// Eagerly subscribe to sync set `entry.currentValue` to what the observable returns, and keep the observable alive until the component unmounts.
|
|
70
|
+
const subscription = entry.observable.subscribe()
|
|
71
|
+
subscription.unsubscribe()
|
|
71
72
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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
|
+
const subscription = instance.observable.subscribe(onStoreChange)
|
|
81
|
+
return () => {
|
|
82
|
+
subscription.unsubscribe()
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
getSnapshot: () => {
|
|
86
|
+
if (instance.error) {
|
|
87
|
+
throw instance.error
|
|
88
|
+
}
|
|
89
|
+
return instance.snapshot
|
|
90
|
+
},
|
|
91
|
+
}
|
|
92
|
+
}, [observable, getInitialValue])
|
|
93
|
+
|
|
94
|
+
useEffect(() => {
|
|
95
|
+
console.log('stor.subscribe changed', store.subscribe)
|
|
96
|
+
}, [store.subscribe])
|
|
81
97
|
|
|
82
98
|
return useSyncExternalStore<ObservedValueOf<ObservableType>>(
|
|
83
|
-
subscribe,
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
throw instance.error
|
|
87
|
-
}
|
|
88
|
-
return instance.snapshot
|
|
89
|
-
},
|
|
90
|
-
typeof initialValue === 'undefined'
|
|
91
|
-
? undefined
|
|
92
|
-
: () => getValue(initialValue) as ObservedValueOf<ObservableType>,
|
|
99
|
+
store.subscribe,
|
|
100
|
+
store.getSnapshot,
|
|
101
|
+
typeof initialValue === 'undefined' ? undefined : () => getInitialValue(),
|
|
93
102
|
)
|
|
94
103
|
}
|