react-rx 4.1.0-canary.3 → 4.1.0-canary.5
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 +14 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +14 -14
- package/dist/index.js.map +1 -1
- package/package.json +2 -1
- package/src/useObservable.ts +2 -7
package/dist/index.cjs
CHANGED
|
@@ -6,7 +6,7 @@ function getValue(value) {
|
|
|
6
6
|
}
|
|
7
7
|
const cache = /* @__PURE__ */ new WeakMap();
|
|
8
8
|
function useObservable(observable, initialValue) {
|
|
9
|
-
const $ = reactCompilerRuntime.c(
|
|
9
|
+
const $ = reactCompilerRuntime.c(9);
|
|
10
10
|
if (!cache.has(observable)) {
|
|
11
11
|
const entry = {
|
|
12
12
|
snapshot: getValue(initialValue)
|
|
@@ -22,24 +22,24 @@ function useObservable(observable, initialValue) {
|
|
|
22
22
|
})), entry.observable.subscribe().unsubscribe(), cache.set(observable, entry);
|
|
23
23
|
}
|
|
24
24
|
let t0;
|
|
25
|
-
$[0] !== observable ? (t0 = (
|
|
26
|
-
|
|
27
|
-
|
|
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) => {
|
|
28
29
|
const subscription_0 = instance.observable.subscribe(onStoreChange);
|
|
29
30
|
return () => {
|
|
30
31
|
subscription_0.unsubscribe();
|
|
31
32
|
};
|
|
32
|
-
}, $[
|
|
33
|
-
const subscribe =
|
|
34
|
-
let t1;
|
|
35
|
-
$[2] !== observable ? (t1 = () => {
|
|
36
|
-
const instance_0 = cache.get(observable);
|
|
37
|
-
if (console.count("getSnapshot"), instance_0.error)
|
|
38
|
-
throw instance_0.error;
|
|
39
|
-
return instance_0.snapshot;
|
|
40
|
-
}, $[2] = observable, $[3] = t1) : t1 = $[3];
|
|
33
|
+
}, $[2] = instance.observable, $[3] = t1) : t1 = $[3];
|
|
34
|
+
const subscribe = t1;
|
|
41
35
|
let t2;
|
|
42
|
-
|
|
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
43
|
}
|
|
44
44
|
function _temp4() {
|
|
45
45
|
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, 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 if (!cache.has(observable)) {\n const entry: Partial<CacheRecord<ObservedValueOf<ObservableType>>> = {\n snapshot: getValue(initialValue) as ObservedValueOf<ObservableType>,\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\n const subscribe = useCallback(\n (onStoreChange: () => void) => {\n
|
|
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 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 if (!cache.has(observable)) {\n const entry: Partial<CacheRecord<ObservedValueOf<ObservableType>>> = {\n snapshot: getValue(initialValue) as ObservedValueOf<ObservableType>,\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 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 if (instance.error) {\n throw instance.error\n }\n return instance.snapshot\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","entry","snapshot","pipe","map","_temp","catchError","_temp2","tap","t0","error","error_0","_temp3","finalize","delete","share","resetOnRefCountZero","_temp4","subscribe","unsubscribe","set","get","instance","t1","onStoreChange","subscription_0","subscription","t2","t3","undefined","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;AAQA,MAAMC,4BAAYC,QAA2C;AAiBtDC,SAAAA,cAAAC,YAAAC,cAAA;AAAAC,QAAAA,IAAAC,uBAAA,CAAA;AAAA,MAAA,CAIAN,MAAAO,IAAUJ,UAAU,GAAC;AACxB,UAAAK,QAAA;AAAA,MAAAC,UACYX,SAASM,YAAY;AAAA,IAAC;AAE7BD,UAAAA,aAAcA,WAAUO,KAC3BC,UAAAA,IAAAC,OAAoD,GACpDC,KAAAA,WAAAC,MAAsD,GACtDC,UAAAC,IAAAA,CAAAA,QAAA;AAAK,YAAA;AAAA,QAAAP;AAAAA,QAAAQ,OAAAC;AAAAA,MAAAA,IAAAF;AACEP,YAAAA,WAAYA,UACjBD,MAAKS,QAASA;AAAAA,IAAAA,CACf,GAGDN,UAAAA,IAAAQ,MAAyB,GAEzBC,KAAAA,SAAepB,MAAAA,MAAAqB,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{
|
|
12
|
-
const $ = c(
|
|
12
|
+
const $ = c(9);
|
|
13
13
|
if (!cache.has(observable)) {
|
|
14
14
|
const entry = {
|
|
15
15
|
snapshot: getValue(initialValue)
|
|
@@ -25,24 +25,24 @@ function useObservable(observable, initialValue) {
|
|
|
25
25
|
})), entry.observable.subscribe().unsubscribe(), cache.set(observable, entry);
|
|
26
26
|
}
|
|
27
27
|
let t0;
|
|
28
|
-
$[0] !== observable ? (t0 = (
|
|
29
|
-
|
|
30
|
-
|
|
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) => {
|
|
31
32
|
const subscription_0 = instance.observable.subscribe(onStoreChange);
|
|
32
33
|
return () => {
|
|
33
34
|
subscription_0.unsubscribe();
|
|
34
35
|
};
|
|
35
|
-
}, $[
|
|
36
|
-
const subscribe =
|
|
37
|
-
let t1;
|
|
38
|
-
$[2] !== observable ? (t1 = () => {
|
|
39
|
-
const instance_0 = cache.get(observable);
|
|
40
|
-
if (console.count("getSnapshot"), instance_0.error)
|
|
41
|
-
throw instance_0.error;
|
|
42
|
-
return instance_0.snapshot;
|
|
43
|
-
}, $[2] = observable, $[3] = t1) : t1 = $[3];
|
|
36
|
+
}, $[2] = instance.observable, $[3] = t1) : t1 = $[3];
|
|
37
|
+
const subscribe = t1;
|
|
44
38
|
let t2;
|
|
45
|
-
|
|
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
46
|
}
|
|
47
47
|
function _temp4() {
|
|
48
48
|
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, 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 if (!cache.has(observable)) {\n const entry: Partial<CacheRecord<ObservedValueOf<ObservableType>>> = {\n snapshot: getValue(initialValue) as ObservedValueOf<ObservableType>,\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\n const subscribe = useCallback(\n (onStoreChange: () => void) => {\n
|
|
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 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 if (!cache.has(observable)) {\n const entry: Partial<CacheRecord<ObservedValueOf<ObservableType>>> = {\n snapshot: getValue(initialValue) as ObservedValueOf<ObservableType>,\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 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 if (instance.error) {\n throw instance.error\n }\n return instance.snapshot\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","entry","snapshot","pipe","map","_temp","catchError","_temp2","tap","t0","error","error_0","_temp3","finalize","delete","share","resetOnRefCountZero","_temp4","subscribe","unsubscribe","set","get","instance","t1","onStoreChange","subscription_0","subscription","t2","t3","undefined","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;AAQA,MAAMC,4BAAYC,QAA2C;AAiBtDC,SAAAA,cAAAC,YAAAC,cAAA;AAAAC,QAAAA,IAAAC,EAAA,CAAA;AAAA,MAAA,CAIAN,MAAAO,IAAUJ,UAAU,GAAC;AACxB,UAAAK,QAAA;AAAA,MAAAC,UACYX,SAASM,YAAY;AAAA,IAAC;AAE7BD,UAAAA,aAAcA,WAAUO,KAC3BC,IAAAC,OAAoD,GACpDC,WAAAC,MAAsD,GACtDC,IAAAC,CAAAA,QAAA;AAAK,YAAA;AAAA,QAAAP;AAAAA,QAAAQ,OAAAC;AAAAA,MAAAA,IAAAF;AACEP,YAAAA,WAAYA,UACjBD,MAAKS,QAASA;AAAAA,IAAAA,CACf,GAGDN,IAAAQ,MAAyB,GAEzBC,SAAepB,MAAAA,MAAAqB,OAAalB,UAAU,CAAC,GACvCmB,MAAA;AAAA,MAAAC,qBAAAC;AAAAA,IAA0D,CAAA,CAC5D,GAGqBhB,MAAKL,WAAAsB,UACdC,EAAAA,YAEZ1B,GAAAA,MAAA2B,IAAUxB,YAAYK,KAAqD;AAAA,EAAA;AAACQ,MAAAA;AAAAX,WAAAF,cAE7Da,KAAAhB,MAAA4B,IAAUzB,UAAU,GAACE,OAAAF,YAAAE,OAAAW,MAAAA,KAAAX,EAAA,CAAA;AAAtC,QAAAwB,WAAiBb;AAAsBc,MAAAA;AAAAzB,IAAA,CAAA,MAAAwB,SAAA1B,cAGrC2B,KAAAC,CAAA,kBAAA;AACE,UAAAC,iBAAqBH,SAAQ1B,WAAAsB,UAAs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UAAU,GAACE,OAAAsC,aAAAtC,OAAAyB,MAAAA,KAAAzB,EAAA,CAAA;AAArF0C,QAAAA,UAAgBC,eAAelB,EAAsD;AAAC,MAAAI,IAAAC;AAAA9B,SAAAA,EAAAwC,CAAAA,MAAAA,UAAAxC,SAAA0C,WAE5Eb,KAAAA,MAAA;AACRD,UAAAA,eAAqBY,OAAMnC,KAAAuC,CAAAA,iBAAsBF,QAAQ5C,YAAU,CAAC,EAACsB,UAAW;AAAC,WAAA,MACpEQ,aAAYP,YAAa;AAAA,EACrCS,GAAAA,KAAA,CAACU,QAAQE,OAAO,GAAC1C,OAAAwC,QAAAxC,OAAA0C,SAAA1C,OAAA6B,IAAA7B,OAAA8B,OAAAD,KAAA7B,EAAA,CAAA,GAAA8B,KAAA9B,EAAA,CAAA,IAHpB6C,UAAUhB,IAGPC,EAAiB,GAEbW;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.0-canary.
|
|
3
|
+
"version": "4.1.0-canary.5",
|
|
4
4
|
"description": "React + RxJS = <3",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"action",
|
|
@@ -80,6 +80,7 @@
|
|
|
80
80
|
"@types/react-dom": "^18.3.1",
|
|
81
81
|
"@typescript-eslint/eslint-plugin": "^8.12.2",
|
|
82
82
|
"@typescript-eslint/parser": "^8.12.2",
|
|
83
|
+
"@vitejs/plugin-react": "^4.3.3",
|
|
83
84
|
"babel-plugin-react-compiler": "beta",
|
|
84
85
|
"eslint": "^8.57.1",
|
|
85
86
|
"eslint-config-prettier": "^9.1.0",
|
package/src/useObservable.ts
CHANGED
|
@@ -67,26 +67,21 @@ export function useObservable<ObservableType extends Observable<any>, InitialVal
|
|
|
67
67
|
|
|
68
68
|
cache.set(observable, entry as CacheRecord<ObservedValueOf<ObservableType>>)
|
|
69
69
|
}
|
|
70
|
+
const instance = cache.get(observable)!
|
|
70
71
|
|
|
71
72
|
const subscribe = useCallback(
|
|
72
73
|
(onStoreChange: () => void) => {
|
|
73
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
74
|
-
const instance = cache.get(observable)!
|
|
75
|
-
console.count('subscribe')
|
|
76
74
|
const subscription = instance.observable.subscribe(onStoreChange)
|
|
77
75
|
return () => {
|
|
78
76
|
subscription.unsubscribe()
|
|
79
77
|
}
|
|
80
78
|
},
|
|
81
|
-
[observable],
|
|
79
|
+
[instance.observable],
|
|
82
80
|
)
|
|
83
81
|
|
|
84
82
|
return useSyncExternalStore<ObservedValueOf<ObservableType>>(
|
|
85
83
|
subscribe,
|
|
86
84
|
() => {
|
|
87
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
88
|
-
const instance = cache.get(observable)!
|
|
89
|
-
console.count('getSnapshot')
|
|
90
85
|
if (instance.error) {
|
|
91
86
|
throw instance.error
|
|
92
87
|
}
|