react-rx 4.1.2-canary.10 → 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 CHANGED
@@ -1,72 +1,61 @@
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(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;
6
14
  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
16
- })),
17
- operators.tap(({
15
+ const entry = {
16
+ snapshot: getInitialValue()
17
+ };
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 {
18
26
  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);
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);
33
34
  }
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
35
  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
- });
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);
50
41
  return () => {
51
- debug && console.log("unsubscribe", observable), subscription.unsubscribe();
42
+ subscription_0.unsubscribe();
52
43
  };
53
- }, $[3] = debug, $[4] = observable, $[5] = instance.observable, $[6] = t2) : t2 = $[6];
54
- let t3;
55
- $[7] !== debug || $[8] !== instance.snapshot || $[9] !== instance.error ? (t3 = () => {
56
- if (debug && console.log("getSnapshot", instance.snapshot, instance.error), instance.error)
44
+ }, $[4] = instance.observable, $[5] = t3) : t3 = $[5];
45
+ let t4;
46
+ $[6] !== instance.error || $[7] !== instance.snapshot ? (t4 = () => {
47
+ if (instance.error)
57
48
  throw instance.error;
58
49
  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;
50
+ }, $[6] = instance.error, $[7] = instance.snapshot, $[8] = t4) : t4 = $[8];
66
51
  let t5;
67
- $[14] !== store || $[15] !== initialValue ? (t5 = () => store.getSnapshot() ?? getValue(initialValue), $[14] = store, $[15] = initialValue, $[16] = t5) : t5 = $[16];
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;
68
57
  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);
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);
70
59
  }
71
60
  function useObservableEvent(handleEvent) {
72
61
  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","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,cAAKC,CAAW,WAAA;AAAA,QAACC,UAAUD;AAAAA,QAAOE,OAAOC;AAAAA,MAAAA,EAAW;AAAA,MACpDC,gBAAYF,WAAUG,QAAG;AAAA,QAACJ,UAAUE;AAAAA,QAAWD;AAAAA,MAAAA,CAAM,CAAC;AAAA,MACtDI,UAAAA,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,cAAKC,CAAU,UAAA;AAAA,MAAA,CAAU;AAAA;AAAA,MAEzBS,KAAAA,SAAS,MAAMlB,MAAMmB,OAAOhB,UAAU,CAAC;AAAA,MACvCiB,WAAM;AAAA,QAACC,qBAAqBA,MAAMC,WAAM,GAAGC,KAAa,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,uBAAA,EAAA;AAAA,MAAAC,IAAAC;AAAAH,IAAA5B,CA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wB,MAAAA,UAAUnB,IAGPG,EAAiB,GAEbY;AAAI;AAZN,SAAAF,QAAA;AAAA,SAGmCO,sCAAsB;AAAC;;;"}
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,UAAsBG,KAAAA,GAAA;AAAA,MAAAN,UAAAI;AAAAA,MAAAD;AAAAA,IAAAA,CAA+B,CAAC,GACtDI,cAAAC,CAAAA,QAAA;AAAK,YAAA;AAAA,QAAAR;AAAAA,QAAAG,OAAAM;AAAAA,MAAAA,IAAAD;AACER,YAAAA,WAAYA,UACjBD,MAAKI,QAASA;AAAAA,IAAAA,CACf,GAGDD,UAAAA,IAAAQ,CAAA,YAAA;AAAA,IAAA,CAAyB,GAEzBC,cAAexB,MAAAA,MAAAyB,OAAatB,UAAU,CAAC,GACvCuB,WAAA;AAAA,MAAAC,qBAAAA,MAAkCC,cAAAC,KAAsB,aAAA;AAAA,IAAE,CAAA,CAC5D,GAGqBjB,MAAKT,WAAA2B,UACdC,EAAAA,YAEZ/B,GAAAA,MAAA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KD,aACLE,EAGF;AAAC;AC7FI,SAAAE,mBAAAC,aAAA;AAAA,QAAAzC,IAAAC,qBAAAA,EAAA,CAAA,GAGL,CAAAC,EAAA,IAAyBE,MAAAA,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,8BAAezC,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,MAAAA,UAAUhC,IAGPc,EAAiB,GAEbc;AAAI;AAZN,SAAAF,QAAA;AAAA,SAGmCO,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,64 @@
1
1
  import { c } from "react-compiler-runtime";
2
- import { useSyncExternalStore, useState, useEffect } from "react";
2
+ import { useState, useSyncExternalStore, useEffect } from "react";
3
3
  import { catchError, of, finalize, share, timer, asapScheduler } 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(14);
13
+ let t0;
14
+ $[0] !== initialValue ? (t0 = () => () => getValue(initialValue), $[0] = initialValue, $[1] = t0) : t0 = $[1];
15
+ const [getInitialValue] = useState(t0);
16
+ let t1;
9
17
  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
19
- })),
20
- tap(({
18
+ const entry = {
19
+ snapshot: getInitialValue()
20
+ };
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 {
21
29
  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);
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);
36
37
  }
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
38
  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
- });
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);
53
44
  return () => {
54
- debug && console.log("unsubscribe", observable), subscription.unsubscribe();
45
+ subscription_0.unsubscribe();
55
46
  };
56
- }, $[3] = debug, $[4] = observable, $[5] = instance.observable, $[6] = t2) : t2 = $[6];
57
- let t3;
58
- $[7] !== debug || $[8] !== instance.snapshot || $[9] !== instance.error ? (t3 = () => {
59
- if (debug && console.log("getSnapshot", instance.snapshot, instance.error), instance.error)
47
+ }, $[4] = instance.observable, $[5] = t3) : t3 = $[5];
48
+ let t4;
49
+ $[6] !== instance.error || $[7] !== instance.snapshot ? (t4 = () => {
50
+ if (instance.error)
60
51
  throw instance.error;
61
52
  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;
53
+ }, $[6] = instance.error, $[7] = instance.snapshot, $[8] = t4) : t4 = $[8];
69
54
  let t5;
70
- $[14] !== store || $[15] !== initialValue ? (t5 = () => store.getSnapshot() ?? getValue(initialValue), $[14] = store, $[15] = initialValue, $[16] = t5) : t5 = $[16];
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;
71
60
  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);
61
+ return $[12] !== initialValue ? (t6 = typeof initialValue > "u" ? void 0 : () => getValue(initialValue), $[12] = initialValue, $[13] = t6) : t6 = $[13], useSyncExternalStore(store.subscribe, store.getSnapshot, t6);
73
62
  }
74
63
  function useObservableEvent(handleEvent) {
75
64
  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,Y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UAAAA,eAAqBY,OAAM3C,KAAA+C,CAAAA,iBAAsBF,QAAQjD,YAAU,CAAC,EAACqB,UAAW;AAAC,WAAA,MACpEc,aAAYb,YAAa;AAAA,EACrCc,GAAAA,KAAA,CAACW,QAAQE,OAAO,GAACrB,OAAAmB,QAAAnB,OAAAqB,SAAArB,OAAAK,IAAAL,OAAAQ,OAAAH,KAAAL,EAAA,CAAA,GAAAQ,KAAAR,EAAA,CAAA,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 {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,IA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;AA7BT,QAAAE,QAAchC;AA2CmBiC,MAAAA;AAAA,SAAAtC,UAAAD,gBAK/BuC,KAAA,OAAOvC,eAAiB,MAAWa,SAEzBnB,MAAAA,SAASM,YAAY,GAAoCC,QAAAD,cAAAC,QAAAsC,MAAAA,KAAAtC,EAAA,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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-rx",
3
- "version": "4.1.2-canary.10",
3
+ "version": "4.1.2-canary.2",
4
4
  "description": "React + RxJS = <3",
5
5
  "keywords": [
6
6
  "action",
@@ -1,17 +1,32 @@
1
- import {useMemo, useSyncExternalStore} from 'react'
2
- import {type Observable, type ObservedValueOf} from 'rxjs'
3
-
4
- import {getOrCreateObservable} from './utils'
1
+ import {useMemo, useState, 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
+ snapshot: T
21
+ error?: unknown
22
+ }
23
+
24
+ const cache = new WeakMap<Observable<any>, CacheRecord<any>>()
25
+
10
26
  /** @public */
11
27
  export function useObservable<ObservableType extends Observable<any>>(
12
28
  observable: ObservableType,
13
29
  initialValue: ObservedValueOf<ObservableType> | (() => ObservedValueOf<ObservableType>),
14
- debug?: boolean,
15
30
  ): ObservedValueOf<ObservableType>
16
31
  /** @public */
17
32
  export function useObservable<ObservableType extends Observable<any>>(
@@ -21,50 +36,64 @@ export function useObservable<ObservableType extends Observable<any>>(
21
36
  export function useObservable<ObservableType extends Observable<any>, InitialValue>(
22
37
  observable: ObservableType,
23
38
  initialValue: InitialValue | (() => InitialValue),
24
- debug?: boolean,
25
39
  ): InitialValue | ObservedValueOf<ObservableType>
26
40
  /** @public */
27
41
  export function useObservable<ObservableType extends Observable<any>, InitialValue>(
28
42
  observable: ObservableType,
29
43
  initialValue?: InitialValue | (() => InitialValue),
30
- debug?: boolean,
31
44
  ): InitialValue | ObservedValueOf<ObservableType> {
45
+ const [getInitialValue] = useState(
46
+ () => () => getValue(initialValue) as ObservedValueOf<ObservableType>,
47
+ )
48
+
32
49
  const store = useMemo(() => {
33
- const instance = getOrCreateObservable(observable, debug)
50
+ if (!cache.has(observable)) {
51
+ const entry: Partial<CacheRecord<ObservedValueOf<ObservableType>>> = {
52
+ snapshot: getInitialValue(),
53
+ }
54
+ entry.observable = observable.pipe(
55
+ map((value) => ({snapshot: value, error: undefined})),
56
+ catchError((error) => of({snapshot: undefined, error})),
57
+ tap(({snapshot, error}) => {
58
+ entry.snapshot = snapshot
59
+ entry.error = error
60
+ }),
61
+ // Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state
62
+ // and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.
63
+ map((value) => void value),
64
+ // Ensure that the cache entry is deleted when the observable completes or errors.
65
+ finalize(() => cache.delete(observable)),
66
+ share({resetOnRefCountZero: () => timer(0, asapScheduler)}),
67
+ )
68
+
69
+ // Eagerly subscribe to sync set `entry.currentValue` to what the observable returns, and keep the observable alive until the component unmounts.
70
+ const subscription = entry.observable.subscribe()
71
+ subscription.unsubscribe()
72
+
73
+ cache.set(observable, entry as CacheRecord<ObservedValueOf<ObservableType>>)
74
+ }
75
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
76
+ const instance = cache.get(observable)!
34
77
 
35
78
  return {
36
79
  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
- })
80
+ const subscription = instance.observable.subscribe(onStoreChange)
46
81
  return () => {
47
- if (debug) {
48
- console.log('unsubscribe', observable)
49
- }
50
82
  subscription.unsubscribe()
51
83
  }
52
84
  },
53
85
  getSnapshot: () => {
54
- if (debug) {
55
- console.log('getSnapshot', instance.snapshot, instance.error)
56
- }
57
86
  if (instance.error) {
58
87
  throw instance.error
59
88
  }
60
89
  return instance.snapshot
61
90
  },
62
91
  }
63
- }, [debug, observable])
92
+ }, [observable, getInitialValue])
64
93
 
65
94
  return useSyncExternalStore<ObservedValueOf<ObservableType>>(
66
95
  store.subscribe,
67
- () => store.getSnapshot() ?? (getValue(initialValue) as ObservedValueOf<ObservableType>),
96
+ store.getSnapshot,
68
97
  typeof initialValue === 'undefined'
69
98
  ? undefined
70
99
  : () => 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
- }