react-rx 4.1.2-canary.11 → 4.1.2-canary.12
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 +3 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/useObservable.ts +5 -5
package/dist/index.cjs
CHANGED
|
@@ -22,10 +22,10 @@ function useObservable(observable, initialValue) {
|
|
|
22
22
|
}), operators.map(_temp3), rxjs.finalize(() => cache.delete(observable)), rxjs.share({
|
|
23
23
|
resetOnRefCountZero: _temp4
|
|
24
24
|
})),
|
|
25
|
-
getSnapshot: () => {
|
|
25
|
+
getSnapshot: (initialValue_0) => {
|
|
26
26
|
if (state.error)
|
|
27
27
|
throw state.error;
|
|
28
|
-
return state.snapshot;
|
|
28
|
+
return state.snapshot ?? getValue(initialValue_0);
|
|
29
29
|
}
|
|
30
30
|
};
|
|
31
31
|
entry.observable.subscribe().unsubscribe(), cache.set(observable, entry);
|
|
@@ -42,7 +42,7 @@ function useObservable(observable, initialValue) {
|
|
|
42
42
|
}, $[2] = instance.observable, $[3] = t1) : t1 = $[3];
|
|
43
43
|
const subscribe = t1;
|
|
44
44
|
let t2;
|
|
45
|
-
$[4] !== instance || $[5] !== initialValue ? (t2 = () => instance.getSnapshot(
|
|
45
|
+
$[4] !== instance || $[5] !== initialValue ? (t2 = () => instance.getSnapshot(initialValue), $[4] = instance, $[5] = initialValue, $[6] = t2) : t2 = $[6];
|
|
46
46
|
let t3;
|
|
47
47
|
return $[7] !== initialValue ? (t3 = typeof initialValue > "u" ? void 0 : () => getValue(initialValue), $[7] = initialValue, $[8] = t3) : t3 = $[8], react.useSyncExternalStore(subscribe, t2, t3);
|
|
48
48
|
}
|
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 state: {\n snapshot: T\n error?: unknown\n }\n getSnapshot: () => 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 const state = {\n snapshot: undefined as ObservedValueOf<ObservableType>,\n error: undefined,\n }\n const entry: CacheRecord<ObservedValueOf<ObservableType>> = {\n // snapshot: getValue(initialValue) as 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: () => {\n if (state.error) {\n throw state.error\n }\n return state.snapshot\n },\n }\n\n // Eagerly subscribe to sync set `
|
|
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 const state = {\n snapshot: undefined as ObservedValueOf<ObservableType>,\n error: undefined,\n }\n const entry: CacheRecord<ObservedValueOf<ObservableType>> = {\n // snapshot: getValue(initialValue) as 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;AACxB,UAAAK,QAAA;AAAA,MAAAC,UAAAC;AAAAA,MAAAC,OAAAD;AAAAA,OAIAE,QAAA;AAAA,MAAAJ;AAAAA,MAAAL,YAGcA,WAAUU,KACpBC,UAAAA,IAAAC,OAAoD,GACpDC,KAAAA,WAAAC,MAAsD,GACtDC,cAAAC,CAAAA,QAAA;AAAK,cAAA;AAAA,UAAAV;AAAAA,UAAAE,OAAAS;AAAAA,QAAAA,IAAAD;AACEV,cAAAA,WAAYA,UACjBD,MAAKG,QAASA;AAAAA,MAAAA,CACf,GAGDG,UAAAA,IAAAO,MAAyB,GAEzBC,KAAAA,SAAetB,MAAAA,MAAAuB,OAAapB,UAAU,CAAC,GACvCqB,WAAA;AAAA,QAAAC,qBAAAC;AAAAA,MAAAA,CAA0D,CAC5D;AAAA,MAACC,aAAAC,CAAA,mBAAA;AAAA,YAEKpB,MAAKG;AAAA,gBACDH,MAAKG;AAENH,eAAAA,MAAKC,YAAcX,SAASM,cAAY;AAAA,MAAA;AAAA,IAAqC;AAKnEQ,UAAKT,WAAA0B,YACdC,YAEZ9B,GAAAA,MAAA+B,IAAU5B,YAAYS,KAAqD;AAAA,EAAA;AAACO,MAAAA;AAAAd,WAAAF,cAE7DgB,KAAAnB,MAAAgC,IAAU7B,UAAU,GAACE,OAAAF,YAAAE,OAAAc,MAAAA,KAAAd,EAAA,CAAA;AAAtC,QAAA4B,WAAiBd;AAAsBe,MAA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AA+B2C,YAAY3C,UAAU,GAACE,OAAAyC,aAAAzC,OAAA6B,MAAAA,KAAA7B,EAAA,CAAA;AAArF6C,QAAAA,UAAgBC,8BAAejB,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,MAAAA,UAAUf,IAGPC,EAAiB,GAEbU;AAAI;AAZN,SAAAlC,QAAA;AAAA,SAGmCuC,sCAAsB;AAAC;;;"}
|
package/dist/index.js
CHANGED
|
@@ -25,10 +25,10 @@ function useObservable(observable, initialValue) {
|
|
|
25
25
|
}), map(_temp3), finalize(() => cache.delete(observable)), share({
|
|
26
26
|
resetOnRefCountZero: _temp4
|
|
27
27
|
})),
|
|
28
|
-
getSnapshot: () => {
|
|
28
|
+
getSnapshot: (initialValue_0) => {
|
|
29
29
|
if (state.error)
|
|
30
30
|
throw state.error;
|
|
31
|
-
return state.snapshot;
|
|
31
|
+
return state.snapshot ?? getValue(initialValue_0);
|
|
32
32
|
}
|
|
33
33
|
};
|
|
34
34
|
entry.observable.subscribe().unsubscribe(), cache.set(observable, entry);
|
|
@@ -45,7 +45,7 @@ function useObservable(observable, initialValue) {
|
|
|
45
45
|
}, $[2] = instance.observable, $[3] = t1) : t1 = $[3];
|
|
46
46
|
const subscribe = t1;
|
|
47
47
|
let t2;
|
|
48
|
-
$[4] !== instance || $[5] !== initialValue ? (t2 = () => instance.getSnapshot(
|
|
48
|
+
$[4] !== instance || $[5] !== initialValue ? (t2 = () => instance.getSnapshot(initialValue), $[4] = instance, $[5] = initialValue, $[6] = t2) : t2 = $[6];
|
|
49
49
|
let t3;
|
|
50
50
|
return $[7] !== initialValue ? (t3 = typeof initialValue > "u" ? void 0 : () => getValue(initialValue), $[7] = initialValue, $[8] = t3) : t3 = $[8], useSyncExternalStore(subscribe, t2, t3);
|
|
51
51
|
}
|
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 state: {\n snapshot: T\n error?: unknown\n }\n getSnapshot: () => 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 const state = {\n snapshot: undefined as ObservedValueOf<ObservableType>,\n error: undefined,\n }\n const entry: CacheRecord<ObservedValueOf<ObservableType>> = {\n // snapshot: getValue(initialValue) as 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: () => {\n if (state.error) {\n throw state.error\n }\n return state.snapshot\n },\n }\n\n // Eagerly subscribe to sync set `
|
|
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 const state = {\n snapshot: undefined as ObservedValueOf<ObservableType>,\n error: undefined,\n }\n const entry: CacheRecord<ObservedValueOf<ObservableType>> = {\n // snapshot: getValue(initialValue) as 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;AACxB,UAAAK,QAAA;AAAA,MAAAC,UAAAC;AAAAA,MAAAC,OAAAD;AAAAA,OAIAE,QAAA;AAAA,MAAAJ;AAAAA,MAAAL,YAGcA,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;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AAdvD,SAAAI,QAAAhB,OAAA;AAAA,SAAA;AAAA,IAAAU,UAa4BV;AAAAA,IAAKY,OAAAD;AAAAA,EAAA;AAAA;AClDjC,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
package/src/useObservable.ts
CHANGED
|
@@ -21,7 +21,7 @@ interface CacheRecord<T> {
|
|
|
21
21
|
snapshot: T
|
|
22
22
|
error?: unknown
|
|
23
23
|
}
|
|
24
|
-
getSnapshot: () => T
|
|
24
|
+
getSnapshot: (initialValue: unknown) => T
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
const cache = new WeakMap<Observable<any>, CacheRecord<any>>()
|
|
@@ -67,15 +67,15 @@ export function useObservable<ObservableType extends Observable<any>, InitialVal
|
|
|
67
67
|
finalize(() => cache.delete(observable)),
|
|
68
68
|
share({resetOnRefCountZero: () => timer(0, asapScheduler)}),
|
|
69
69
|
),
|
|
70
|
-
getSnapshot: () => {
|
|
70
|
+
getSnapshot: (initialValue) => {
|
|
71
71
|
if (state.error) {
|
|
72
72
|
throw state.error
|
|
73
73
|
}
|
|
74
|
-
return state.snapshot
|
|
74
|
+
return state.snapshot ?? (getValue(initialValue) as ObservedValueOf<ObservableType>)
|
|
75
75
|
},
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
-
// Eagerly subscribe to sync set `
|
|
78
|
+
// Eagerly subscribe to sync set `state.snapshot` to what the observable returns, and keep the observable alive until the component unmounts.
|
|
79
79
|
const subscription = entry.observable.subscribe()
|
|
80
80
|
subscription.unsubscribe()
|
|
81
81
|
|
|
@@ -96,7 +96,7 @@ export function useObservable<ObservableType extends Observable<any>, InitialVal
|
|
|
96
96
|
return useSyncExternalStore<ObservedValueOf<ObservableType>>(
|
|
97
97
|
subscribe,
|
|
98
98
|
() => {
|
|
99
|
-
return instance.getSnapshot(
|
|
99
|
+
return instance.getSnapshot(initialValue)
|
|
100
100
|
},
|
|
101
101
|
typeof initialValue === 'undefined'
|
|
102
102
|
? undefined
|