react-rx 4.1.2-canary.10 → 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 CHANGED
@@ -1,72 +1,67 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: !0 });
3
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
+ function getValue(value) {
5
+ return typeof value == "function" ? value() : value;
6
+ }
4
7
  const cache = /* @__PURE__ */ new WeakMap();
5
- function getOrCreateObservable(observable, debug) {
8
+ function useObservable(observable, initialValue) {
9
+ const $ = reactCompilerRuntime.c(9);
6
10
  if (!cache.has(observable)) {
7
- const entry = {};
8
- entry.observable = observable.pipe(
9
- operators.map((value) => ({
10
- snapshot: value,
11
- error: void 0
12
- })),
13
- rxjs.catchError((error) => rxjs.of({
14
- snapshot: void 0,
15
- error
11
+ const state = {
12
+ snapshot: void 0,
13
+ error: void 0
14
+ }, entry = {
15
+ state,
16
+ observable: observable.pipe(operators.map(_temp$1), rxjs.catchError(_temp2), operators.tap((t02) => {
17
+ const {
18
+ snapshot,
19
+ error: error_0
20
+ } = t02;
21
+ state.snapshot = snapshot, state.error = error_0;
22
+ }), operators.map(_temp3), rxjs.finalize(() => cache.delete(observable)), rxjs.share({
23
+ resetOnRefCountZero: _temp4
16
24
  })),
17
- operators.tap(({
18
- snapshot,
19
- error
20
- }) => {
21
- debug && console.log("tap", snapshot, error), entry.snapshot = snapshot, entry.error = error;
22
- }),
23
- // Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state
24
- // and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.
25
- operators.map((value) => {
26
- }),
27
- // Ensure that the cache entry is deleted when the observable completes or errors.
28
- rxjs.finalize(() => cache.delete(observable)),
29
- rxjs.share({
30
- resetOnRefCountZero: () => rxjs.timer(0, rxjs.asapScheduler)
31
- })
32
- ), entry.observable.subscribe().unsubscribe(), cache.set(observable, entry);
25
+ getSnapshot: (initialValue_0) => {
26
+ if (state.error)
27
+ throw state.error;
28
+ return state.snapshot ?? getValue(initialValue_0);
29
+ }
30
+ };
31
+ entry.observable.subscribe().unsubscribe(), cache.set(observable, entry);
33
32
  }
34
- return cache.get(observable);
35
- }
36
- function getValue(value) {
37
- return typeof value == "function" ? value() : value;
38
- }
39
- function useObservable(observable, initialValue, debug) {
40
- const $ = reactCompilerRuntime.c(19);
41
- let t0, t1;
42
- $[0] !== observable || $[1] !== debug ? (t1 = getOrCreateObservable(observable, debug), $[0] = observable, $[1] = debug, $[2] = t1) : t1 = $[2];
43
- const instance = t1;
44
- let t2;
45
- $[3] !== debug || $[4] !== observable || $[5] !== instance.observable ? (t2 = (onStoreChange) => {
46
- debug && console.log("subscribe", observable);
47
- const subscription = instance.observable.subscribe(() => {
48
- debug && console.log("onStoreChange", observable), onStoreChange();
49
- });
33
+ let t0;
34
+ $[0] !== observable ? (t0 = cache.get(observable), $[0] = observable, $[1] = t0) : t0 = $[1];
35
+ const instance = t0;
36
+ let t1;
37
+ $[2] !== instance.observable ? (t1 = (onStoreChange) => {
38
+ const subscription_0 = instance.observable.subscribe(onStoreChange);
50
39
  return () => {
51
- debug && console.log("unsubscribe", observable), subscription.unsubscribe();
40
+ subscription_0.unsubscribe();
52
41
  };
53
- }, $[3] = debug, $[4] = observable, $[5] = instance.observable, $[6] = t2) : t2 = $[6];
42
+ }, $[2] = instance.observable, $[3] = t1) : t1 = $[3];
43
+ const subscribe = t1;
44
+ let t2;
45
+ $[4] !== instance || $[5] !== initialValue ? (t2 = () => instance.getSnapshot(initialValue), $[4] = instance, $[5] = initialValue, $[6] = t2) : t2 = $[6];
54
46
  let t3;
55
- $[7] !== debug || $[8] !== instance.snapshot || $[9] !== instance.error ? (t3 = () => {
56
- if (debug && console.log("getSnapshot", instance.snapshot, instance.error), instance.error)
57
- throw instance.error;
58
- return instance.snapshot;
59
- }, $[7] = debug, $[8] = instance.snapshot, $[9] = instance.error, $[10] = t3) : t3 = $[10];
60
- let t4;
61
- $[11] !== t2 || $[12] !== t3 ? (t4 = {
62
- subscribe: t2,
63
- getSnapshot: t3
64
- }, $[11] = t2, $[12] = t3, $[13] = t4) : t4 = $[13], t0 = t4;
65
- const store = t0;
66
- let t5;
67
- $[14] !== store || $[15] !== initialValue ? (t5 = () => store.getSnapshot() ?? getValue(initialValue), $[14] = store, $[15] = initialValue, $[16] = t5) : t5 = $[16];
68
- let t6;
69
- return $[17] !== initialValue ? (t6 = typeof initialValue > "u" ? void 0 : () => getValue(initialValue), $[17] = initialValue, $[18] = t6) : t6 = $[18], react.useSyncExternalStore(store.subscribe, t5, t6);
47
+ return $[7] !== initialValue ? (t3 = typeof initialValue > "u" ? void 0 : () => getValue(initialValue), $[7] = initialValue, $[8] = t3) : t3 = $[8], react.useSyncExternalStore(subscribe, t2, t3);
48
+ }
49
+ function _temp4() {
50
+ return rxjs.timer(0, rxjs.asapScheduler);
51
+ }
52
+ function _temp3(value_0) {
53
+ }
54
+ function _temp2(error) {
55
+ return rxjs.of({
56
+ snapshot: void 0,
57
+ error
58
+ });
59
+ }
60
+ function _temp$1(value) {
61
+ return {
62
+ snapshot: value,
63
+ error: void 0
64
+ };
70
65
  }
71
66
  function useObservableEvent(handleEvent) {
72
67
  const $ = reactCompilerRuntime.c(6), [t0] = react.useState(_temp), [calls$, call] = t0;
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../src/utils.ts","../src/useObservable.ts","../src/useObservableEvent.ts"],"sourcesContent":["import {\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\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/** @internal */\nexport function getOrCreateObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n debug?: boolean,\n): CacheRecord<ObservedValueOf<ObservableType>> {\n if (!cache.has(observable)) {\n const entry: Partial<CacheRecord<ObservedValueOf<ObservableType>>> = {}\n entry.observable = observable.pipe(\n map((value) => ({snapshot: value, error: undefined})),\n catchError((error) => of({snapshot: undefined, error})),\n tap(({snapshot, error}) => {\n if (debug) {\n console.log('tap', snapshot, error)\n }\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 return cache.get(observable)!\n}\n","import {useMemo, useSyncExternalStore} from 'react'\nimport {type Observable, type ObservedValueOf} from 'rxjs'\n\nimport {getOrCreateObservable} from './utils'\n\nfunction getValue<T>(value: T): T extends () => infer U ? U : T {\n return typeof value === 'function' ? value() : value\n}\n\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n initialValue: ObservedValueOf<ObservableType> | (() => ObservedValueOf<ObservableType>),\n debug?: boolean,\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 debug?: boolean,\n): InitialValue | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue?: InitialValue | (() => InitialValue),\n debug?: boolean,\n): InitialValue | ObservedValueOf<ObservableType> {\n const store = useMemo(() => {\n const instance = getOrCreateObservable(observable, debug)\n\n return {\n subscribe: (onStoreChange: () => void) => {\n if (debug) {\n console.log('subscribe', observable)\n }\n const subscription = instance.observable.subscribe(() => {\n if (debug) {\n console.log('onStoreChange', observable)\n }\n onStoreChange()\n })\n return () => {\n if (debug) {\n console.log('unsubscribe', observable)\n }\n subscription.unsubscribe()\n }\n },\n getSnapshot: () => {\n if (debug) {\n console.log('getSnapshot', instance.snapshot, instance.error)\n }\n if (instance.error) {\n throw instance.error\n }\n return instance.snapshot\n },\n }\n }, [debug, observable])\n\n return useSyncExternalStore<ObservedValueOf<ObservableType>>(\n store.subscribe,\n () => store.getSnapshot() ?? (getValue(initialValue) as ObservedValueOf<ObservableType>),\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":["cache","WeakMap","getOrCreateObservable","observable","debug","has","entry","pipe","map","value","snapshot","error","undefined","catchError","of","tap","console","log","finalize","delete","share","resetOnRefCountZero","timer","asapScheduler","subscribe","unsubscribe","set","get","getValue","useObservable","initialValue","$","_c","t0","t1","instance","t2","onStoreChange","subscription","t3","t4","getSnapshot","store","t5","t6","useSyncExternalStore","useObservableEvent","handleEvent","use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EbY;AAAI;AAZN,SAAAF,QAAA;AAAA,SAGmCO,sCAAsB;AAAC;;;"}
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,MAAAA;AAAA7B,IAAA,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AAU,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.d.cts CHANGED
@@ -5,7 +5,6 @@ import {ObservedValueOf} from 'rxjs'
5
5
  export declare function useObservable<ObservableType extends Observable<any>>(
6
6
  observable: ObservableType,
7
7
  initialValue: ObservedValueOf<ObservableType> | (() => ObservedValueOf<ObservableType>),
8
- debug?: boolean,
9
8
  ): ObservedValueOf<ObservableType>
10
9
 
11
10
  /** @public */
@@ -17,7 +16,6 @@ export declare function useObservable<ObservableType extends Observable<any>>(
17
16
  export declare function useObservable<ObservableType extends Observable<any>, InitialValue>(
18
17
  observable: ObservableType,
19
18
  initialValue: InitialValue | (() => InitialValue),
20
- debug?: boolean,
21
19
  ): InitialValue | ObservedValueOf<ObservableType>
22
20
 
23
21
  /** @public */
package/dist/index.d.ts CHANGED
@@ -5,7 +5,6 @@ import {ObservedValueOf} from 'rxjs'
5
5
  export declare function useObservable<ObservableType extends Observable<any>>(
6
6
  observable: ObservableType,
7
7
  initialValue: ObservedValueOf<ObservableType> | (() => ObservedValueOf<ObservableType>),
8
- debug?: boolean,
9
8
  ): ObservedValueOf<ObservableType>
10
9
 
11
10
  /** @public */
@@ -17,7 +16,6 @@ export declare function useObservable<ObservableType extends Observable<any>>(
17
16
  export declare function useObservable<ObservableType extends Observable<any>, InitialValue>(
18
17
  observable: ObservableType,
19
18
  initialValue: InitialValue | (() => InitialValue),
20
- debug?: boolean,
21
19
  ): InitialValue | ObservedValueOf<ObservableType>
22
20
 
23
21
  /** @public */
package/dist/index.js CHANGED
@@ -1,75 +1,70 @@
1
1
  import { c } from "react-compiler-runtime";
2
2
  import { useSyncExternalStore, useState, useEffect } from "react";
3
- import { catchError, of, finalize, share, timer, asapScheduler } from "rxjs";
3
+ import { catchError, finalize, share, timer, asapScheduler, of } from "rxjs";
4
4
  import { map, tap } from "rxjs/operators";
5
5
  import { observableCallback } from "observable-callback";
6
6
  import { useEffectEvent } from "use-effect-event";
7
+ function getValue(value) {
8
+ return typeof value == "function" ? value() : value;
9
+ }
7
10
  const cache = /* @__PURE__ */ new WeakMap();
8
- function getOrCreateObservable(observable, debug) {
11
+ function useObservable(observable, initialValue) {
12
+ const $ = c(9);
9
13
  if (!cache.has(observable)) {
10
- const entry = {};
11
- entry.observable = observable.pipe(
12
- map((value) => ({
13
- snapshot: value,
14
- error: void 0
15
- })),
16
- catchError((error) => of({
17
- snapshot: void 0,
18
- error
14
+ const state = {
15
+ snapshot: void 0,
16
+ error: void 0
17
+ }, entry = {
18
+ state,
19
+ observable: observable.pipe(map(_temp$1), catchError(_temp2), tap((t02) => {
20
+ const {
21
+ snapshot,
22
+ error: error_0
23
+ } = t02;
24
+ state.snapshot = snapshot, state.error = error_0;
25
+ }), map(_temp3), finalize(() => cache.delete(observable)), share({
26
+ resetOnRefCountZero: _temp4
19
27
  })),
20
- tap(({
21
- snapshot,
22
- error
23
- }) => {
24
- debug && console.log("tap", snapshot, error), entry.snapshot = snapshot, entry.error = error;
25
- }),
26
- // Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state
27
- // and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.
28
- map((value) => {
29
- }),
30
- // Ensure that the cache entry is deleted when the observable completes or errors.
31
- finalize(() => cache.delete(observable)),
32
- share({
33
- resetOnRefCountZero: () => timer(0, asapScheduler)
34
- })
35
- ), entry.observable.subscribe().unsubscribe(), cache.set(observable, entry);
28
+ getSnapshot: (initialValue_0) => {
29
+ if (state.error)
30
+ throw state.error;
31
+ return state.snapshot ?? getValue(initialValue_0);
32
+ }
33
+ };
34
+ entry.observable.subscribe().unsubscribe(), cache.set(observable, entry);
36
35
  }
37
- return cache.get(observable);
38
- }
39
- function getValue(value) {
40
- return typeof value == "function" ? value() : value;
41
- }
42
- function useObservable(observable, initialValue, debug) {
43
- const $ = c(19);
44
- let t0, t1;
45
- $[0] !== observable || $[1] !== debug ? (t1 = getOrCreateObservable(observable, debug), $[0] = observable, $[1] = debug, $[2] = t1) : t1 = $[2];
46
- const instance = t1;
47
- let t2;
48
- $[3] !== debug || $[4] !== observable || $[5] !== instance.observable ? (t2 = (onStoreChange) => {
49
- debug && console.log("subscribe", observable);
50
- const subscription = instance.observable.subscribe(() => {
51
- debug && console.log("onStoreChange", observable), onStoreChange();
52
- });
36
+ let t0;
37
+ $[0] !== observable ? (t0 = cache.get(observable), $[0] = observable, $[1] = t0) : t0 = $[1];
38
+ const instance = t0;
39
+ let t1;
40
+ $[2] !== instance.observable ? (t1 = (onStoreChange) => {
41
+ const subscription_0 = instance.observable.subscribe(onStoreChange);
53
42
  return () => {
54
- debug && console.log("unsubscribe", observable), subscription.unsubscribe();
43
+ subscription_0.unsubscribe();
55
44
  };
56
- }, $[3] = debug, $[4] = observable, $[5] = instance.observable, $[6] = t2) : t2 = $[6];
45
+ }, $[2] = instance.observable, $[3] = t1) : t1 = $[3];
46
+ const subscribe = t1;
47
+ let t2;
48
+ $[4] !== instance || $[5] !== initialValue ? (t2 = () => instance.getSnapshot(initialValue), $[4] = instance, $[5] = initialValue, $[6] = t2) : t2 = $[6];
57
49
  let t3;
58
- $[7] !== debug || $[8] !== instance.snapshot || $[9] !== instance.error ? (t3 = () => {
59
- if (debug && console.log("getSnapshot", instance.snapshot, instance.error), instance.error)
60
- throw instance.error;
61
- return instance.snapshot;
62
- }, $[7] = debug, $[8] = instance.snapshot, $[9] = instance.error, $[10] = t3) : t3 = $[10];
63
- let t4;
64
- $[11] !== t2 || $[12] !== t3 ? (t4 = {
65
- subscribe: t2,
66
- getSnapshot: t3
67
- }, $[11] = t2, $[12] = t3, $[13] = t4) : t4 = $[13], t0 = t4;
68
- const store = t0;
69
- let t5;
70
- $[14] !== store || $[15] !== initialValue ? (t5 = () => store.getSnapshot() ?? getValue(initialValue), $[14] = store, $[15] = initialValue, $[16] = t5) : t5 = $[16];
71
- let t6;
72
- return $[17] !== initialValue ? (t6 = typeof initialValue > "u" ? void 0 : () => getValue(initialValue), $[17] = initialValue, $[18] = t6) : t6 = $[18], useSyncExternalStore(store.subscribe, t5, t6);
50
+ return $[7] !== initialValue ? (t3 = typeof initialValue > "u" ? void 0 : () => getValue(initialValue), $[7] = initialValue, $[8] = t3) : t3 = $[8], useSyncExternalStore(subscribe, t2, t3);
51
+ }
52
+ function _temp4() {
53
+ return timer(0, asapScheduler);
54
+ }
55
+ function _temp3(value_0) {
56
+ }
57
+ function _temp2(error) {
58
+ return of({
59
+ snapshot: void 0,
60
+ error
61
+ });
62
+ }
63
+ function _temp$1(value) {
64
+ return {
65
+ snapshot: value,
66
+ error: void 0
67
+ };
73
68
  }
74
69
  function useObservableEvent(handleEvent) {
75
70
  const $ = c(6), [t0] = useState(_temp), [calls$, call] = t0;
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/utils.ts","../src/useObservable.ts","../src/useObservableEvent.ts"],"sourcesContent":["import {\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\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/** @internal */\nexport function getOrCreateObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n debug?: boolean,\n): CacheRecord<ObservedValueOf<ObservableType>> {\n if (!cache.has(observable)) {\n const entry: Partial<CacheRecord<ObservedValueOf<ObservableType>>> = {}\n entry.observable = observable.pipe(\n map((value) => ({snapshot: value, error: undefined})),\n catchError((error) => of({snapshot: undefined, error})),\n tap(({snapshot, error}) => {\n if (debug) {\n console.log('tap', snapshot, error)\n }\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 return cache.get(observable)!\n}\n","import {useMemo, useSyncExternalStore} from 'react'\nimport {type Observable, type ObservedValueOf} from 'rxjs'\n\nimport {getOrCreateObservable} from './utils'\n\nfunction getValue<T>(value: T): T extends () => infer U ? U : T {\n return typeof value === 'function' ? value() : value\n}\n\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n initialValue: ObservedValueOf<ObservableType> | (() => ObservedValueOf<ObservableType>),\n debug?: boolean,\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 debug?: boolean,\n): InitialValue | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue?: InitialValue | (() => InitialValue),\n debug?: boolean,\n): InitialValue | ObservedValueOf<ObservableType> {\n const store = useMemo(() => {\n const instance = getOrCreateObservable(observable, debug)\n\n return {\n subscribe: (onStoreChange: () => void) => {\n if (debug) {\n console.log('subscribe', observable)\n }\n const subscription = instance.observable.subscribe(() => {\n if (debug) {\n console.log('onStoreChange', observable)\n }\n onStoreChange()\n })\n return () => {\n if (debug) {\n console.log('unsubscribe', observable)\n }\n subscription.unsubscribe()\n }\n },\n getSnapshot: () => {\n if (debug) {\n console.log('getSnapshot', instance.snapshot, instance.error)\n }\n if (instance.error) {\n throw instance.error\n }\n return instance.snapshot\n },\n }\n }, [debug, observable])\n\n return useSyncExternalStore<ObservedValueOf<ObservableType>>(\n store.subscribe,\n () => store.getSnapshot() ?? (getValue(initialValue) as ObservedValueOf<ObservableType>),\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":["cache","WeakMap","getOrCreateObservable","observable","debug","has","entry","pipe","map","value","snapshot","error","undefined","catchError","of","tap","console","log","finalize","delete","share","resetOnRefCountZero","timer","asapScheduler","subscribe","unsubscribe","set","get","getValue","useObservable","initialValue","$","_c","t0","t1","instance","t2","onStoreChange","subscription","t3","t4","getSnapshot","store","t5","t6","useSyncExternalStore","useObservableEvent","handleEvent","useState","_temp","calls$","call","onEvent","useEffectEvent","observable_0","useEffect","observableCallback"],"mappings":";;;;;;AAkBA,MAAMA,4BAAYC,QAA2C;AAG7CC,SAAAA,sBACdC,YACAC,OAC8C;AAC9C,MAAI,CAACJ,MAAMK,IAAIF,UAAU,GAAG;AAC1B,UAAMG,QAA+D,CAAC;AACtEA,UAAMH,aAAaA,WAAWI;AAAAA,MAC5BC,IAAKC,CAAW,WAAA;AAAA,QAACC,UAAUD;AAAAA,QAAOE,OAAOC;AAAAA,MAAAA,EAAW;AAAA,MACpDC,WAAYF,WAAUG,GAAG;AAAA,QAACJ,UAAUE;AAAAA,QAAWD;AAAAA,MAAAA,CAAM,CAAC;AAAA,MACtDI,IAAI,CAAC;AAAA,QAACL;AAAAA,QAAUC;AAAAA,MAAAA,MAAW;AACrBP,iBACFY,QAAQC,IAAI,OAAOP,UAAUC,KAAK,GAEpCL,MAAMI,WAAWA,UACjBJ,MAAMK,QAAQA;AAAAA,MAAAA,CACf;AAAA;AAAA;AAAA,MAGDH,IAAKC,CAAU,UAAA;AAAA,MAAA,CAAU;AAAA;AAAA,MAEzBS,SAAS,MAAMlB,MAAMmB,OAAOhB,UAAU,CAAC;AAAA,MACvCiB,MAAM;AAAA,QAACC,qBAAqBA,MAAMC,MAAM,GAAGC,aAAa;AAAA,MAAE,CAAA;AAAA,IAAA,GAIvCjB,MAAMH,WAAWqB,YACzBC,YAEbzB,GAAAA,MAAM0B,IAAIvB,YAAYG,KAAqD;AAAA,EAAA;AAGtEN,SAAAA,MAAM2B,IAAIxB,UAAU;AAC7B;AChDA,SAASyB,SAAYnB,OAA2C;AAC9D,SAAO,OAAOA,SAAU,aAAaA,MAAUA,IAAAA;AACjD;AAmBOoB,SAAAA,cAAA1B,YAAA2B,cAAA1B,OAAA;AAAA2B,QAAAA,IAAAC,EAAA,EAAA;AAAA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AA,IAHpBwB,UAAUnB,IAGPG,EAAiB,GAEbY;AAAI;AAZN,SAAAF,QAAA;AAAA,SAGmCO,mBAAsB;AAAC;"}
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;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,MAAAA;AAAA7B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zC,OAAA6B,MAAAA,KAAA7B,EAAA,CAAA;AAArF6C,QAAAA,UAAgBC,eAAejB,EAAsD;AAAC,MAAAI,IAAAC;AAAAlC,SAAAA,EAAA2C,CAAAA,MAAAA,UAAA3C,SAAA6C,WAE5EZ,KAAAA,MAAA;AACRD,UAAAA,eAAqBW,OAAMnC,KAAAuC,CAAAA,iBAAsBF,QAAQ/C,YAAU,CAAC,EAAC0B,UAAW;AAAC,WAAA,MACpEQ,aAAYP,YAAa;AAAA,EACrCS,GAAAA,KAAA,CAACS,QAAQE,OAAO,GAAC7C,OAAA2C,QAAA3C,OAAA6C,SAAA7C,OAAAiC,IAAAjC,OAAAkC,OAAAD,KAAAjC,EAAA,CAAA,GAAAkC,KAAAlC,EAAA,CAAA,IAHpBgD,UAAUf,IAGPC,EAAiB,GAEbU;AAAI;AAZN,SAAAlC,QAAA;AAAA,SAGmCuC,mBAAsB;AAAC;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-rx",
3
- "version": "4.1.2-canary.10",
3
+ "version": "4.1.2-canary.12",
4
4
  "description": "React + RxJS = <3",
5
5
  "keywords": [
6
6
  "action",
@@ -1,17 +1,35 @@
1
- import {useMemo, useSyncExternalStore} from 'react'
2
- import {type Observable, type ObservedValueOf} from 'rxjs'
3
-
4
- import {getOrCreateObservable} from './utils'
1
+ import {useCallback, useSyncExternalStore} from 'react'
2
+ import {
3
+ asapScheduler,
4
+ catchError,
5
+ finalize,
6
+ type Observable,
7
+ type ObservedValueOf,
8
+ of,
9
+ share,
10
+ timer,
11
+ } from 'rxjs'
12
+ import {map, tap} from 'rxjs/operators'
5
13
 
6
14
  function getValue<T>(value: T): T extends () => infer U ? U : T {
7
15
  return typeof value === 'function' ? value() : value
8
16
  }
9
17
 
18
+ interface CacheRecord<T> {
19
+ observable: Observable<void>
20
+ state: {
21
+ snapshot: T
22
+ error?: unknown
23
+ }
24
+ getSnapshot: (initialValue: unknown) => T
25
+ }
26
+
27
+ const cache = new WeakMap<Observable<any>, CacheRecord<any>>()
28
+
10
29
  /** @public */
11
30
  export function useObservable<ObservableType extends Observable<any>>(
12
31
  observable: ObservableType,
13
32
  initialValue: ObservedValueOf<ObservableType> | (() => ObservedValueOf<ObservableType>),
14
- debug?: boolean,
15
33
  ): ObservedValueOf<ObservableType>
16
34
  /** @public */
17
35
  export function useObservable<ObservableType extends Observable<any>>(
@@ -21,50 +39,65 @@ export function useObservable<ObservableType extends Observable<any>>(
21
39
  export function useObservable<ObservableType extends Observable<any>, InitialValue>(
22
40
  observable: ObservableType,
23
41
  initialValue: InitialValue | (() => InitialValue),
24
- debug?: boolean,
25
42
  ): InitialValue | ObservedValueOf<ObservableType>
26
43
  /** @public */
27
44
  export function useObservable<ObservableType extends Observable<any>, InitialValue>(
28
45
  observable: ObservableType,
29
46
  initialValue?: InitialValue | (() => InitialValue),
30
- debug?: boolean,
31
47
  ): InitialValue | ObservedValueOf<ObservableType> {
32
- const store = useMemo(() => {
33
- const instance = getOrCreateObservable(observable, debug)
34
-
35
- return {
36
- subscribe: (onStoreChange: () => void) => {
37
- if (debug) {
38
- console.log('subscribe', observable)
39
- }
40
- const subscription = instance.observable.subscribe(() => {
41
- if (debug) {
42
- console.log('onStoreChange', observable)
43
- }
44
- onStoreChange()
45
- })
46
- return () => {
47
- if (debug) {
48
- console.log('unsubscribe', observable)
49
- }
50
- subscription.unsubscribe()
51
- }
52
- },
53
- getSnapshot: () => {
54
- if (debug) {
55
- console.log('getSnapshot', instance.snapshot, instance.error)
56
- }
57
- if (instance.error) {
58
- throw instance.error
48
+ if (!cache.has(observable)) {
49
+ const state = {
50
+ snapshot: undefined as ObservedValueOf<ObservableType>,
51
+ error: undefined,
52
+ }
53
+ const entry: CacheRecord<ObservedValueOf<ObservableType>> = {
54
+ // snapshot: getValue(initialValue) as ObservedValueOf<ObservableType>,
55
+ state,
56
+ observable: observable.pipe(
57
+ map((value) => ({snapshot: value, error: undefined})),
58
+ catchError((error) => of({snapshot: undefined, error})),
59
+ tap(({snapshot, error}) => {
60
+ state.snapshot = snapshot
61
+ state.error = error
62
+ }),
63
+ // Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state
64
+ // and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.
65
+ map((value) => void value),
66
+ // Ensure that the cache entry is deleted when the observable completes or errors.
67
+ finalize(() => cache.delete(observable)),
68
+ share({resetOnRefCountZero: () => timer(0, asapScheduler)}),
69
+ ),
70
+ getSnapshot: (initialValue) => {
71
+ if (state.error) {
72
+ throw state.error
59
73
  }
60
- return instance.snapshot
74
+ return state.snapshot ?? (getValue(initialValue) as ObservedValueOf<ObservableType>)
61
75
  },
62
76
  }
63
- }, [debug, observable])
77
+
78
+ // Eagerly subscribe to sync set `state.snapshot` to what the observable returns, and keep the observable alive until the component unmounts.
79
+ const subscription = entry.observable.subscribe()
80
+ subscription.unsubscribe()
81
+
82
+ cache.set(observable, entry as CacheRecord<ObservedValueOf<ObservableType>>)
83
+ }
84
+ const instance = cache.get(observable)!
85
+
86
+ const subscribe = useCallback(
87
+ (onStoreChange: () => void) => {
88
+ const subscription = instance.observable.subscribe(onStoreChange)
89
+ return () => {
90
+ subscription.unsubscribe()
91
+ }
92
+ },
93
+ [instance.observable],
94
+ )
64
95
 
65
96
  return useSyncExternalStore<ObservedValueOf<ObservableType>>(
66
- store.subscribe,
67
- () => store.getSnapshot() ?? (getValue(initialValue) as ObservedValueOf<ObservableType>),
97
+ subscribe,
98
+ () => {
99
+ return instance.getSnapshot(initialValue)
100
+ },
68
101
  typeof initialValue === 'undefined'
69
102
  ? undefined
70
103
  : () => getValue(initialValue) as ObservedValueOf<ObservableType>,
package/src/utils.ts DELETED
@@ -1,54 +0,0 @@
1
- import {
2
- asapScheduler,
3
- catchError,
4
- finalize,
5
- type Observable,
6
- type ObservedValueOf,
7
- of,
8
- share,
9
- timer,
10
- } from 'rxjs'
11
- import {map, tap} from 'rxjs/operators'
12
-
13
- interface CacheRecord<T> {
14
- observable: Observable<void>
15
- snapshot: T
16
- error?: unknown
17
- }
18
-
19
- const cache = new WeakMap<Observable<any>, CacheRecord<any>>()
20
-
21
- /** @internal */
22
- export function getOrCreateObservable<ObservableType extends Observable<any>>(
23
- observable: ObservableType,
24
- debug?: boolean,
25
- ): CacheRecord<ObservedValueOf<ObservableType>> {
26
- if (!cache.has(observable)) {
27
- const entry: Partial<CacheRecord<ObservedValueOf<ObservableType>>> = {}
28
- entry.observable = observable.pipe(
29
- map((value) => ({snapshot: value, error: undefined})),
30
- catchError((error) => of({snapshot: undefined, error})),
31
- tap(({snapshot, error}) => {
32
- if (debug) {
33
- console.log('tap', snapshot, error)
34
- }
35
- entry.snapshot = snapshot
36
- entry.error = error
37
- }),
38
- // Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state
39
- // and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.
40
- map((value) => void value),
41
- // Ensure that the cache entry is deleted when the observable completes or errors.
42
- finalize(() => cache.delete(observable)),
43
- share({resetOnRefCountZero: () => timer(0, asapScheduler)}),
44
- )
45
-
46
- // Eagerly subscribe to sync set `entry.currentValue` to what the observable returns, and keep the observable alive until the component unmounts.
47
- const subscription = entry.observable.subscribe()
48
- subscription.unsubscribe()
49
-
50
- cache.set(observable, entry as CacheRecord<ObservedValueOf<ObservableType>>)
51
- }
52
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
53
- return cache.get(observable)!
54
- }