react-rx 4.1.2-canary.1 → 4.1.2-canary.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 +61 -48
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +62 -48
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/useObservable.ts +4 -6
package/dist/index.cjs
CHANGED
|
@@ -1,62 +1,75 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: !0 });
|
|
3
|
-
var react = require("react"), rxjs = require("rxjs"), operators = require("rxjs/operators"), observableCallback = require("observable-callback"), useEffectEvent = require("use-effect-event");
|
|
3
|
+
var reactCompilerRuntime = require("react-compiler-runtime"), 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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
}
|
|
9
|
+
const $ = reactCompilerRuntime.c(14);
|
|
10
|
+
let t0;
|
|
11
|
+
$[0] !== initialValue ? (t0 = () => () => getValue(initialValue), $[0] = initialValue, $[1] = t0) : t0 = $[1];
|
|
12
|
+
const [getInitialValue] = react.useState(t0);
|
|
13
|
+
let t1;
|
|
14
|
+
if (!cache.has(observable)) {
|
|
15
|
+
const entry = {
|
|
16
|
+
snapshot: getInitialValue()
|
|
44
17
|
};
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
18
|
+
entry.observable = observable.pipe(operators.map((value) => ({
|
|
19
|
+
snapshot: value,
|
|
20
|
+
error: void 0
|
|
21
|
+
})), rxjs.catchError((error) => rxjs.of({
|
|
22
|
+
snapshot: void 0,
|
|
23
|
+
error
|
|
24
|
+
})), operators.tap((t22) => {
|
|
25
|
+
const {
|
|
26
|
+
snapshot,
|
|
27
|
+
error: error_0
|
|
28
|
+
} = t22;
|
|
29
|
+
entry.snapshot = snapshot, entry.error = error_0;
|
|
30
|
+
}), operators.map((value_0) => {
|
|
31
|
+
}), rxjs.finalize(() => cache.delete(observable)), rxjs.share({
|
|
32
|
+
resetOnRefCountZero: () => rxjs.timer(0, rxjs.asapScheduler)
|
|
33
|
+
})), entry.observable.subscribe().unsubscribe(), cache.set(observable, entry);
|
|
34
|
+
}
|
|
35
|
+
let t2;
|
|
36
|
+
$[2] !== observable ? (t2 = cache.get(observable), $[2] = observable, $[3] = t2) : t2 = $[3];
|
|
37
|
+
const instance = t2;
|
|
38
|
+
let t3;
|
|
39
|
+
$[4] !== instance.observable ? (t3 = (onStoreChange) => {
|
|
40
|
+
const subscription_0 = instance.observable.subscribe(onStoreChange);
|
|
41
|
+
return () => {
|
|
42
|
+
subscription_0.unsubscribe();
|
|
43
|
+
};
|
|
44
|
+
}, $[4] = instance.observable, $[5] = t3) : t3 = $[5];
|
|
45
|
+
let t4;
|
|
46
|
+
$[6] !== instance.error || $[7] !== instance.snapshot ? (t4 = () => {
|
|
47
|
+
if (instance.error)
|
|
48
|
+
throw instance.error;
|
|
49
|
+
return instance.snapshot;
|
|
50
|
+
}, $[6] = instance.error, $[7] = instance.snapshot, $[8] = t4) : t4 = $[8];
|
|
51
|
+
let t5;
|
|
52
|
+
$[9] !== t3 || $[10] !== t4 ? (t5 = {
|
|
53
|
+
subscribe: t3,
|
|
54
|
+
getSnapshot: t4
|
|
55
|
+
}, $[9] = t3, $[10] = t4, $[11] = t5) : t5 = $[11], t1 = t5;
|
|
56
|
+
const store = t1;
|
|
57
|
+
let t6;
|
|
58
|
+
return $[12] !== initialValue ? (t6 = typeof initialValue > "u" ? void 0 : () => getValue(initialValue), $[12] = initialValue, $[13] = t6) : t6 = $[13], react.useSyncExternalStore(store.subscribe, store.getSnapshot, t6);
|
|
53
59
|
}
|
|
54
60
|
function useObservableEvent(handleEvent) {
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
61
|
+
const $ = reactCompilerRuntime.c(6), [t0] = react.useState(_temp), [calls$, call] = t0;
|
|
62
|
+
let t1;
|
|
63
|
+
$[0] !== handleEvent ? (t1 = (observable) => handleEvent(observable), $[0] = handleEvent, $[1] = t1) : t1 = $[1];
|
|
64
|
+
const onEvent = useEffectEvent.useEffectEvent(t1);
|
|
65
|
+
let t2, t3;
|
|
66
|
+
return $[2] !== calls$ || $[3] !== onEvent ? (t2 = () => {
|
|
67
|
+
const subscription = calls$.pipe((observable_0) => onEvent(observable_0)).subscribe();
|
|
58
68
|
return () => subscription.unsubscribe();
|
|
59
|
-
}, [calls$, onEvent]), call;
|
|
69
|
+
}, t3 = [calls$, onEvent], $[2] = calls$, $[3] = onEvent, $[4] = t2, $[5] = t3) : (t2 = $[4], t3 = $[5]), react.useEffect(t2, t3), call;
|
|
70
|
+
}
|
|
71
|
+
function _temp() {
|
|
72
|
+
return observableCallback.observableCallback();
|
|
60
73
|
}
|
|
61
74
|
exports.useObservable = useObservable;
|
|
62
75
|
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 {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 return useSyncExternalStore<ObservedValueOf<ObservableType>>(\n store.subscribe,\n store.getSnapshot,\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","t0","getInitialValue","useState","t1","has","entry","snapshot","pipe","map","error","undefined","catchError","of","tap","t2","error_0","value_0","finalize","delete","share","resetOnRefCountZero","timer","asapScheduler","subscribe","unsubscribe","set","get","instance","t3","onStoreChange","subscription_0","subscription","t4","t5","getSnapshot","store","t6","useSyncExternalStore","useObservableEvent","handleEvent","_temp","calls$","call","onEvent","useEffectEvent","observable_0","useEffect","observableCallback"],"mappings":";;;AAaA,SAASA,SAAYC,OAA2C;AAC9D,SAAO,OAAOA,SAAU,aAAaA,MAAUA,IAAAA;AACjD;AAQA,MAAMC,4BAAYC,QAA2C;AAiBtDC,SAAAA,cAAAC,YAAAC,cAAA;AAAAC,QAAAA,IAAAC,uBAAA,EAAA;AAAAC,MAAAA;AAAAF,WAAAD,gBAKHG,KAAAA,MAAA,MAAYT,SAASM,YAAY,GAAoCC,OAAAD,cAAAC,OAAAE,MAAAA,KAAAF,EAAA,CAAA;AADvE,QAAA,CAAAG,eAAA,IAA0BC,MAAAA,SACxBF,EACF;AAACG,MAAAA;AAAA,MAAA,CAGMV,MAAAW,IAAUR,UAAU,GAAC;AACxB,UAAAS,QAAA;AAAA,MAAAC,UACYL,gBAAgB;AAAA,IAAC;AAE7BI,UAAKT,aAAcA,WAAUW,KAC3BC,UAAAA,IAAAhB,CAAA,WAAA;AAAA,MAAAc,UAA2Bd;AAAAA,MAAKiB,OAAAC;AAAAA,IAAoB,EAAA,GACpDC,KAAAA,WAAAF,CAAAA,UAAsB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AA,IAHpBgD,MAAAA,UAAUhC,IAGPc,EAAiB,GAEbc;AAAI;AAZN,SAAAF,QAAA;AAAA,SAGmCO,sCAAsB;AAAC;;;"}
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { c } from "react-compiler-runtime";
|
|
2
|
+
import { useState, useSyncExternalStore, useEffect } from "react";
|
|
2
3
|
import { catchError, of, finalize, share, timer, asapScheduler } from "rxjs";
|
|
3
4
|
import { map, tap } from "rxjs/operators";
|
|
4
5
|
import { observableCallback } from "observable-callback";
|
|
@@ -8,57 +9,70 @@ function getValue(value) {
|
|
|
8
9
|
}
|
|
9
10
|
const cache = /* @__PURE__ */ new WeakMap();
|
|
10
11
|
function useObservable(observable, initialValue) {
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
-
}
|
|
12
|
+
const $ = c(14);
|
|
13
|
+
let t0;
|
|
14
|
+
$[0] !== initialValue ? (t0 = () => () => getValue(initialValue), $[0] = initialValue, $[1] = t0) : t0 = $[1];
|
|
15
|
+
const [getInitialValue] = useState(t0);
|
|
16
|
+
let t1;
|
|
17
|
+
if (!cache.has(observable)) {
|
|
18
|
+
const entry = {
|
|
19
|
+
snapshot: getInitialValue()
|
|
46
20
|
};
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
21
|
+
entry.observable = observable.pipe(map((value) => ({
|
|
22
|
+
snapshot: value,
|
|
23
|
+
error: void 0
|
|
24
|
+
})), catchError((error) => of({
|
|
25
|
+
snapshot: void 0,
|
|
26
|
+
error
|
|
27
|
+
})), tap((t22) => {
|
|
28
|
+
const {
|
|
29
|
+
snapshot,
|
|
30
|
+
error: error_0
|
|
31
|
+
} = t22;
|
|
32
|
+
entry.snapshot = snapshot, entry.error = error_0;
|
|
33
|
+
}), map((value_0) => {
|
|
34
|
+
}), finalize(() => cache.delete(observable)), share({
|
|
35
|
+
resetOnRefCountZero: () => timer(0, asapScheduler)
|
|
36
|
+
})), entry.observable.subscribe().unsubscribe(), cache.set(observable, entry);
|
|
37
|
+
}
|
|
38
|
+
let t2;
|
|
39
|
+
$[2] !== observable ? (t2 = cache.get(observable), $[2] = observable, $[3] = t2) : t2 = $[3];
|
|
40
|
+
const instance = t2;
|
|
41
|
+
let t3;
|
|
42
|
+
$[4] !== instance.observable ? (t3 = (onStoreChange) => {
|
|
43
|
+
const subscription_0 = instance.observable.subscribe(onStoreChange);
|
|
44
|
+
return () => {
|
|
45
|
+
subscription_0.unsubscribe();
|
|
46
|
+
};
|
|
47
|
+
}, $[4] = instance.observable, $[5] = t3) : t3 = $[5];
|
|
48
|
+
let t4;
|
|
49
|
+
$[6] !== instance.error || $[7] !== instance.snapshot ? (t4 = () => {
|
|
50
|
+
if (instance.error)
|
|
51
|
+
throw instance.error;
|
|
52
|
+
return instance.snapshot;
|
|
53
|
+
}, $[6] = instance.error, $[7] = instance.snapshot, $[8] = t4) : t4 = $[8];
|
|
54
|
+
let t5;
|
|
55
|
+
$[9] !== t3 || $[10] !== t4 ? (t5 = {
|
|
56
|
+
subscribe: t3,
|
|
57
|
+
getSnapshot: t4
|
|
58
|
+
}, $[9] = t3, $[10] = t4, $[11] = t5) : t5 = $[11], t1 = t5;
|
|
59
|
+
const store = t1;
|
|
60
|
+
let t6;
|
|
61
|
+
return $[12] !== initialValue ? (t6 = typeof initialValue > "u" ? void 0 : () => getValue(initialValue), $[12] = initialValue, $[13] = t6) : t6 = $[13], useSyncExternalStore(store.subscribe, store.getSnapshot, t6);
|
|
55
62
|
}
|
|
56
63
|
function useObservableEvent(handleEvent) {
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
|
|
64
|
+
const $ = c(6), [t0] = useState(_temp), [calls$, call] = t0;
|
|
65
|
+
let t1;
|
|
66
|
+
$[0] !== handleEvent ? (t1 = (observable) => handleEvent(observable), $[0] = handleEvent, $[1] = t1) : t1 = $[1];
|
|
67
|
+
const onEvent = useEffectEvent(t1);
|
|
68
|
+
let t2, t3;
|
|
69
|
+
return $[2] !== calls$ || $[3] !== onEvent ? (t2 = () => {
|
|
70
|
+
const subscription = calls$.pipe((observable_0) => onEvent(observable_0)).subscribe();
|
|
60
71
|
return () => subscription.unsubscribe();
|
|
61
|
-
}, [calls$, onEvent]), call;
|
|
72
|
+
}, t3 = [calls$, onEvent], $[2] = calls$, $[3] = onEvent, $[4] = t2, $[5] = t3) : (t2 = $[4], t3 = $[5]), useEffect(t2, t3), call;
|
|
73
|
+
}
|
|
74
|
+
function _temp() {
|
|
75
|
+
return observableCallback();
|
|
62
76
|
}
|
|
63
77
|
export {
|
|
64
78
|
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 {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 return useSyncExternalStore<ObservedValueOf<ObservableType>>(\n store.subscribe,\n store.getSnapshot,\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","t0","getInitialValue","useState","t1","has","entry","snapshot","pipe","map","error","undefined","catchError","of","tap","t2","error_0","value_0","finalize","delete","share","resetOnRefCountZero","timer","asapScheduler","subscribe","unsubscribe","set","get","instance","t3","onStoreChange","subscription_0","subscription","t4","t5","getSnapshot","store","t6","useSyncExternalStore","useObservableEvent","handleEvent","_temp","calls$","call","onEvent","useEffectEvent","observable_0","useEffect","observableCallback"],"mappings":";;;;;;AAaA,SAASA,SAAYC,OAA2C;AAC9D,SAAO,OAAOA,SAAU,aAAaA,MAAUA,IAAAA;AACjD;AAQA,MAAMC,4BAAYC,QAA2C;AAiBtDC,SAAAA,cAAAC,YAAAC,cAAA;AAAAC,QAAAA,IAAAC,EAAA,EAAA;AAAAC,MAAAA;AAAAF,WAAAD,gBAKHG,KAAAA,MAAA,MAAYT,SAASM,YAAY,GAAoCC,OAAAD,cAAAC,OAAAE,MAAAA,KAAAF,EAAA,CAAA;AADvE,QAAA,CAAAG,eAAA,IAA0BC,SACxBF,EACF;AAACG,MAAAA;AAAA,MAAA,CAGMV,MAAAW,IAAUR,UAAU,GAAC;AACxB,UAAAS,QAAA;AAAA,MAAAC,UACYL,gBAAgB;AAAA,IAAC;AAE7BI,UAAKT,aAAcA,WAAUW,KAC3BC,IAAAhB,CAAA,WAAA;AAAA,MAAAc,UAA2Bd;AAAAA,MAAKiB,OAAAC;AAAAA,IAAoB,EAAA,GACpDC,WAAAF,CAAAA,UAAsBG,GAAA;AAAA,MAAAN,UAAAI;AAAAA,MAAAD;AAAAA,IAAAA,CAA+B,CAAC,GACtDI,IAAAC,CAAAA,QAAA;AAAK,YAAA;AAAA,QAAAR;AAAAA,QAAAG,OAAAM;AAAAA,MAAAA,IAAAD;AACER,YAAAA,WAAYA,UACjBD,MAAKI,QAASA;AAAAA,IAAAA,CACf,GAGDD,IAAAQ,CAAA,YAAA;AAAA,IAAA,CAAyB,GAEzBC,SAAexB,MAAAA,MAAAyB,OAAatB,UAAU,CAAC,GACvCuB,MAAA;AAAA,MAAAC,qBAAAA,MAAkCC,SAAAC,aAAsB;AAAA,IAAE,CAAA,CAC5D,GAGqBjB,M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,EAAA,GAL9DuC,qBACLF,MAAKZ,WACLY,MAAKD,aACLE,EAGF;AAAC;AC7FI,SAAAE,mBAAAC,aAAA;AAAA,QAAAzC,IAAAC,EAAA,CAAA,GAGL,CAAAC,EAAA,IAAyBE,SAAAsC,KAAsC,GAAxD,CAAAC,QAAAC,IAAA,IAAA1C;AAAcG,MAAAA;AAAAL,WAAAyC,eAEUpC,KAAAP,CAAAA,eAA+B2C,YAAY3C,UAAU,GAACE,OAAAyC,aAAAzC,OAAAK,MAAAA,KAAAL,EAAA,CAAA;AAArF6C,QAAAA,UAAgBC,eAAezC,EAAsD;AAAC,MAAAW,IAAAc;AAAA9B,SAAAA,EAAA2C,CAAAA,MAAAA,UAAA3C,SAAA6C,WAE5E7B,KAAAA,MAAA;AACRiB,UAAAA,eAAqBU,OAAMlC,KAAAsC,CAAAA,iBAAsBF,QAAQ/C,YAAU,CAAC,EAAC2B,UAAW;AAAC,WAAA,MACpEQ,aAAYP,YAAa;AAAA,EACrCI,GAAAA,KAAA,CAACa,QAAQE,OAAO,GAAC7C,OAAA2C,QAAA3C,OAAA6C,SAAA7C,OAAAgB,IAAAhB,OAAA8B,OAAAd,KAAAhB,EAAA,CAAA,GAAA8B,KAAA9B,EAAA,CAAA,IAHpBgD,UAAUhC,IAGPc,EAAiB,GAEbc;AAAI;AAZN,SAAAF,QAAA;AAAA,SAGmCO,mBAAsB;AAAC;"}
|
package/package.json
CHANGED
package/src/useObservable.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {useMemo, useState, useSyncExternalStore} from 'react'
|
|
2
2
|
import {
|
|
3
3
|
asapScheduler,
|
|
4
4
|
catchError,
|
|
@@ -91,13 +91,11 @@ export function useObservable<ObservableType extends Observable<any>, InitialVal
|
|
|
91
91
|
}
|
|
92
92
|
}, [observable, getInitialValue])
|
|
93
93
|
|
|
94
|
-
useEffect(() => {
|
|
95
|
-
console.log('stor.subscribe changed', store.subscribe)
|
|
96
|
-
}, [store.subscribe])
|
|
97
|
-
|
|
98
94
|
return useSyncExternalStore<ObservedValueOf<ObservableType>>(
|
|
99
95
|
store.subscribe,
|
|
100
96
|
store.getSnapshot,
|
|
101
|
-
typeof initialValue === 'undefined'
|
|
97
|
+
typeof initialValue === 'undefined'
|
|
98
|
+
? undefined
|
|
99
|
+
: () => getValue(initialValue) as ObservedValueOf<ObservableType>,
|
|
102
100
|
)
|
|
103
101
|
}
|