react-rx 4.1.0-canary.3 → 4.1.0-canary.4

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
@@ -23,9 +23,7 @@ function useObservable(observable, initialValue) {
23
23
  }
24
24
  let t0;
25
25
  $[0] !== observable ? (t0 = (onStoreChange) => {
26
- const instance = cache.get(observable);
27
- console.count("subscribe");
28
- const subscription_0 = instance.observable.subscribe(onStoreChange);
26
+ const subscription_0 = cache.get(observable).observable.subscribe(onStoreChange);
29
27
  return () => {
30
28
  subscription_0.unsubscribe();
31
29
  };
@@ -34,7 +32,7 @@ function useObservable(observable, initialValue) {
34
32
  let t1;
35
33
  $[2] !== observable ? (t1 = () => {
36
34
  const instance_0 = cache.get(observable);
37
- if (console.count("getSnapshot"), instance_0.error)
35
+ if (instance_0.error)
38
36
  throw instance_0.error;
39
37
  return instance_0.snapshot;
40
38
  }, $[2] = observable, $[3] = t1) : t1 = $[3];
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../src/useObservable.ts","../src/useObservableEvent.ts"],"sourcesContent":["import {useCallback, useSyncExternalStore} from 'react'\nimport {\n asapScheduler,\n catchError,\n finalize,\n type Observable,\n type ObservedValueOf,\n of,\n share,\n timer,\n} from 'rxjs'\nimport {map, tap} from 'rxjs/operators'\n\nfunction getValue<T>(value: T): T extends () => infer U ? U : T {\n return typeof value === 'function' ? value() : value\n}\n\ninterface CacheRecord<T> {\n observable: Observable<void>\n snapshot: T\n error?: unknown\n}\n\nconst cache = new WeakMap<Observable<any>, CacheRecord<any>>()\n\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n initialValue: ObservedValueOf<ObservableType> | (() => ObservedValueOf<ObservableType>),\n): ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n): undefined | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue: InitialValue | (() => InitialValue),\n): InitialValue | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue?: InitialValue | (() => InitialValue),\n): InitialValue | ObservedValueOf<ObservableType> {\n if (!cache.has(observable)) {\n const entry: Partial<CacheRecord<ObservedValueOf<ObservableType>>> = {\n snapshot: getValue(initialValue) as ObservedValueOf<ObservableType>,\n }\n entry.observable = observable.pipe(\n map((value) => ({snapshot: value, error: undefined})),\n catchError((error) => of({snapshot: undefined, error})),\n tap(({snapshot, error}) => {\n entry.snapshot = snapshot\n entry.error = error\n }),\n // Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state\n // and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.\n map((value) => void value),\n // Ensure that the cache entry is deleted when the observable completes or errors.\n finalize(() => cache.delete(observable)),\n share({resetOnRefCountZero: () => timer(0, asapScheduler)}),\n )\n\n // Eagerly subscribe to sync set `entry.currentValue` to what the observable returns, and keep the observable alive until the component unmounts.\n const subscription = entry.observable.subscribe()\n subscription.unsubscribe()\n\n cache.set(observable, entry as CacheRecord<ObservedValueOf<ObservableType>>)\n }\n\n const subscribe = useCallback(\n (onStoreChange: () => void) => {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const instance = cache.get(observable)!\n console.count('subscribe')\n const subscription = instance.observable.subscribe(onStoreChange)\n return () => {\n subscription.unsubscribe()\n }\n },\n [observable],\n )\n\n return useSyncExternalStore<ObservedValueOf<ObservableType>>(\n subscribe,\n () => {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const instance = cache.get(observable)!\n console.count('getSnapshot')\n if (instance.error) {\n throw instance.error\n }\n return instance.snapshot\n },\n typeof initialValue === 'undefined'\n ? undefined\n : () => getValue(initialValue) as ObservedValueOf<ObservableType>,\n )\n}\n","import {observableCallback} from 'observable-callback'\nimport {useEffect, useState} from 'react'\nimport {type Observable} from 'rxjs'\nimport {useEffectEvent} from 'use-effect-event'\n\n/** @public */\nexport function useObservableEvent<T, U>(\n handleEvent: (arg: Observable<T>) => Observable<U>,\n): (arg: T) => void {\n const [[calls$, call]] = useState(() => observableCallback<T>())\n\n const onEvent = useEffectEvent((observable: Observable<T>) => handleEvent(observable))\n\n useEffect(() => {\n const subscription = calls$.pipe((observable) => onEvent(observable)).subscribe()\n return () => subscription.unsubscribe()\n }, [calls$, onEvent])\n\n return call\n}\n"],"names":["getValue","value","cache","WeakMap","useObservable","observable","initialValue","$","_c","has","entry","snapshot","pipe","map","_temp","catchError","_temp2","tap","t0","error","error_0","_temp3","finalize","delete","share","resetOnRefCountZero","_temp4","subscribe","unsubscribe","set","onStoreChange","instance","get","console","count","subscription_0","subscription","t1","instance_0","t2","undefined","useSyncExternalStore","timer","asapScheduler","value_0","of","useObservableEvent","handleEvent","useState","calls$","call","onEvent","useEffectEvent","t3","observable_0","useEffect","observableCallback"],"mappings":";;;AAaA,SAASA,SAAYC,OAA2C;AAC9D,SAAO,OAAOA,SAAU,aAAaA,MAAUA,IAAAA;AACjD;AAQA,MAAMC,4BAAYC,QAA2C;AAiBtDC,SAAAA,cAAAC,YAAAC,cAAA;AAAAC,QAAAA,IAAAC,uBAAA,CAAA;AAAA,MAAA,CAIAN,MAAAO,IAAUJ,UAAU,GAAC;AACxB,UAAAK,QAAA;AAAA,MAAAC,UACYX,SAASM,YAAY;AAAA,IAAC;AAE7BD,UAAAA,aAAcA,WAAUO,KAC3BC,UAAAA,IAAAC,OAAoD,GACpDC,KAAAA,WAAAC,MAAsD,GACtDC,UAAAC,IAAAA,CAAAA,QAAA;AAAK,YAAA;AAAA,QAAAP;AAAAA,QAAAQ,OAAAC;AAAAA,MAAAA,IAAAF;AACEP,YAAAA,WAAYA,UACjBD,MAAKS,QAASA;AAAAA,IAAAA,CACf,GAGDN,UAAAA,IAAAQ,MAAyB,GAEzBC,KAAAA,SAAepB,MAAAA,MAA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SAAAL,QAAAb,OAAA;AAAA,SAAA;AAAA,IAAAU,UAS0BV;AAAAA,IAAKkB,OAAAqB;AAAAA,EAAA;AAAA;AC3C/B,SAAAM,mBAAAC,aAAA;AAAA,QAAAxC,IAAAC,qBAAAA,EAAA,CAAA,GAGL,CAAAU,EAAA,IAAyB8B,MAAAA,SAAAlC,KAAsC,GAAxD,CAAAmC,QAAAC,IAAA,IAAAhC;AAAcmB,MAAAA;AAAA9B,WAAAwC,eAEUV,KAAAhC,CAAAA,eAA+B0C,YAAY1C,UAAU,GAACE,OAAAwC,aAAAxC,OAAA8B,MAAAA,KAAA9B,EAAA,CAAA;AAArF4C,QAAAA,UAAgBC,8BAAef,EAAsD;AAAC,MAAAE,IAAAc;AAAA9C,SAAAA,EAAA0C,CAAAA,MAAAA,UAAA1C,SAAA4C,WAE5EZ,KAAAA,MAAA;AACRH,UAAAA,eAAqBa,OAAMrC,KAAA0C,CAAAA,iBAAsBH,QAAQ9C,YAAU,CAAC,EAACsB,UAAW;AAAC,WAAA,MACpES,aAAYR,YAAa;AAAA,EACrCyB,GAAAA,KAAA,CAACJ,QAAQE,OAAO,GAAC5C,OAAA0C,QAAA1C,OAAA4C,SAAA5C,OAAAgC,IAAAhC,OAAA8C,OAAAd,KAAAhC,EAAA,CAAA,GAAA8C,KAAA9C,EAAA,CAAA,IAHpBgD,MAAAA,UAAUhB,IAGPc,EAAiB,GAEbH;AAAI;AAZN,SAAApC,QAAA;AAAA,SAGmC0C,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 snapshot: T\n error?: unknown\n}\n\nconst cache = new WeakMap<Observable<any>, CacheRecord<any>>()\n\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n initialValue: ObservedValueOf<ObservableType> | (() => ObservedValueOf<ObservableType>),\n): ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n): undefined | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue: InitialValue | (() => InitialValue),\n): InitialValue | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue?: InitialValue | (() => InitialValue),\n): InitialValue | ObservedValueOf<ObservableType> {\n if (!cache.has(observable)) {\n const entry: Partial<CacheRecord<ObservedValueOf<ObservableType>>> = {\n snapshot: getValue(initialValue) as ObservedValueOf<ObservableType>,\n }\n entry.observable = observable.pipe(\n map((value) => ({snapshot: value, error: undefined})),\n catchError((error) => of({snapshot: undefined, error})),\n tap(({snapshot, error}) => {\n entry.snapshot = snapshot\n entry.error = error\n }),\n // Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state\n // and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.\n map((value) => void value),\n // Ensure that the cache entry is deleted when the observable completes or errors.\n finalize(() => cache.delete(observable)),\n share({resetOnRefCountZero: () => timer(0, asapScheduler)}),\n )\n\n // Eagerly subscribe to sync set `entry.currentValue` to what the observable returns, and keep the observable alive until the component unmounts.\n const subscription = entry.observable.subscribe()\n subscription.unsubscribe()\n\n cache.set(observable, entry as CacheRecord<ObservedValueOf<ObservableType>>)\n }\n\n const subscribe = useCallback(\n (onStoreChange: () => void) => {\n const instance = cache.get(observable)!\n const subscription = instance.observable.subscribe(onStoreChange)\n return () => {\n subscription.unsubscribe()\n }\n },\n [observable],\n )\n\n return useSyncExternalStore<ObservedValueOf<ObservableType>>(\n subscribe,\n () => {\n const instance = cache.get(observable)!\n if (instance.error) {\n throw instance.error\n }\n return instance.snapshot\n },\n typeof initialValue === 'undefined'\n ? undefined\n : () => getValue(initialValue) as ObservedValueOf<ObservableType>,\n )\n}\n","import {observableCallback} from 'observable-callback'\nimport {useEffect, useState} from 'react'\nimport {type Observable} from 'rxjs'\nimport {useEffectEvent} from 'use-effect-event'\n\n/** @public */\nexport function useObservableEvent<T, U>(\n handleEvent: (arg: Observable<T>) => Observable<U>,\n): (arg: T) => void {\n const [[calls$, call]] = useState(() => observableCallback<T>())\n\n const onEvent = useEffectEvent((observable: Observable<T>) => handleEvent(observable))\n\n useEffect(() => {\n const subscription = calls$.pipe((observable) => onEvent(observable)).subscribe()\n return () => subscription.unsubscribe()\n }, [calls$, onEvent])\n\n return call\n}\n"],"names":["getValue","value","cache","WeakMap","useObservable","observable","initialValue","$","_c","has","entry","snapshot","pipe","map","_temp","catchError","_temp2","tap","t0","error","error_0","_temp3","finalize","delete","share","resetOnRefCountZero","_temp4","subscribe","unsubscribe","set","onStoreChange","subscription_0","get","subscription","t1","instance_0","instance","t2","undefined","useSyncExternalStore","timer","asapScheduler","value_0","of","useObservableEvent","handleEvent","useState","calls$","call","onEvent","useEffectEvent","t3","observable_0","useEffect","observableCallback"],"mappings":";;;AAaA,SAASA,SAAYC,OAA2C;AAC9D,SAAO,OAAOA,SAAU,aAAaA,MAAUA,IAAAA;AACjD;AAQA,MAAMC,4BAAYC,QAA2C;AAiBtDC,SAAAA,cAAAC,YAAAC,cAAA;AAAAC,QAAAA,IAAAC,uBAAA,CAAA;AAAA,MAAA,CAIAN,MAAAO,IAAUJ,UAAU,GAAC;AACxB,UAAAK,QAAA;AAAA,MAAAC,UACYX,SAASM,YAAY;AAAA,IAAC;AAE7BD,UAAAA,aAAcA,WAAUO,KAC3BC,UAAAA,IAAAC,OAAoD,GACpDC,KAAAA,WAAAC,MAAsD,GACtDC,UAAAC,IAAAA,CAAAA,QAAA;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A1B,OAAAG,OAAA;AAAA,SAUqBwB,QAAA;AAAA,IAAAhC,UAAA2B;AAAAA,IAAAnB;AAAAA,EAAAA,CAA+B;AAAC;AAVrD,SAAAL,QAAAb,OAAA;AAAA,SAAA;AAAA,IAAAU,UAS0BV;AAAAA,IAAKkB,OAAAmB;AAAAA,EAAA;AAAA;AC3C/B,SAAAM,mBAAAC,aAAA;AAAA,QAAAtC,IAAAC,qBAAAA,EAAA,CAAA,GAGL,CAAAU,EAAA,IAAyB4B,MAAAA,SAAAhC,KAAsC,GAAxD,CAAAiC,QAAAC,IAAA,IAAA9B;AAAcgB,MAAAA;AAAA3B,WAAAsC,eAEUX,KAAA7B,CAAAA,eAA+BwC,YAAYxC,UAAU,GAACE,OAAAsC,aAAAtC,OAAA2B,MAAAA,KAAA3B,EAAA,CAAA;AAArF0C,QAAAA,UAAgBC,8BAAehB,EAAsD;AAAC,MAAAG,IAAAc;AAAA5C,SAAAA,EAAAwC,CAAAA,MAAAA,UAAAxC,SAAA0C,WAE5EZ,KAAAA,MAAA;AACRJ,UAAAA,eAAqBc,OAAMnC,KAAAwC,CAAAA,iBAAsBH,QAAQ5C,YAAU,CAAC,EAACsB,UAAW;AAAC,WAAA,MACpEM,aAAYL,YAAa;AAAA,EACrCuB,GAAAA,KAAA,CAACJ,QAAQE,OAAO,GAAC1C,OAAAwC,QAAAxC,OAAA0C,SAAA1C,OAAA8B,IAAA9B,OAAA4C,OAAAd,KAAA9B,EAAA,CAAA,GAAA4C,KAAA5C,EAAA,CAAA,IAHpB8C,MAAAA,UAAUhB,IAGPc,EAAiB,GAEbH;AAAI;AAZN,SAAAlC,QAAA;AAAA,SAGmCwC,sCAAsB;AAAC;;;"}
package/dist/index.js CHANGED
@@ -26,9 +26,7 @@ function useObservable(observable, initialValue) {
26
26
  }
27
27
  let t0;
28
28
  $[0] !== observable ? (t0 = (onStoreChange) => {
29
- const instance = cache.get(observable);
30
- console.count("subscribe");
31
- const subscription_0 = instance.observable.subscribe(onStoreChange);
29
+ const subscription_0 = cache.get(observable).observable.subscribe(onStoreChange);
32
30
  return () => {
33
31
  subscription_0.unsubscribe();
34
32
  };
@@ -37,7 +35,7 @@ function useObservable(observable, initialValue) {
37
35
  let t1;
38
36
  $[2] !== observable ? (t1 = () => {
39
37
  const instance_0 = cache.get(observable);
40
- if (console.count("getSnapshot"), instance_0.error)
38
+ if (instance_0.error)
41
39
  throw instance_0.error;
42
40
  return instance_0.snapshot;
43
41
  }, $[2] = observable, $[3] = t1) : t1 = $[3];
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/useObservable.ts","../src/useObservableEvent.ts"],"sourcesContent":["import {useCallback, useSyncExternalStore} from 'react'\nimport {\n asapScheduler,\n catchError,\n finalize,\n type Observable,\n type ObservedValueOf,\n of,\n share,\n timer,\n} from 'rxjs'\nimport {map, tap} from 'rxjs/operators'\n\nfunction getValue<T>(value: T): T extends () => infer U ? U : T {\n return typeof value === 'function' ? value() : value\n}\n\ninterface CacheRecord<T> {\n observable: Observable<void>\n snapshot: T\n error?: unknown\n}\n\nconst cache = new WeakMap<Observable<any>, CacheRecord<any>>()\n\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n initialValue: ObservedValueOf<ObservableType> | (() => ObservedValueOf<ObservableType>),\n): ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n): undefined | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue: InitialValue | (() => InitialValue),\n): InitialValue | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue?: InitialValue | (() => InitialValue),\n): InitialValue | ObservedValueOf<ObservableType> {\n if (!cache.has(observable)) {\n const entry: Partial<CacheRecord<ObservedValueOf<ObservableType>>> = {\n snapshot: getValue(initialValue) as ObservedValueOf<ObservableType>,\n }\n entry.observable = observable.pipe(\n map((value) => ({snapshot: value, error: undefined})),\n catchError((error) => of({snapshot: undefined, error})),\n tap(({snapshot, error}) => {\n entry.snapshot = snapshot\n entry.error = error\n }),\n // Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state\n // and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.\n map((value) => void value),\n // Ensure that the cache entry is deleted when the observable completes or errors.\n finalize(() => cache.delete(observable)),\n share({resetOnRefCountZero: () => timer(0, asapScheduler)}),\n )\n\n // Eagerly subscribe to sync set `entry.currentValue` to what the observable returns, and keep the observable alive until the component unmounts.\n const subscription = entry.observable.subscribe()\n subscription.unsubscribe()\n\n cache.set(observable, entry as CacheRecord<ObservedValueOf<ObservableType>>)\n }\n\n const subscribe = useCallback(\n (onStoreChange: () => void) => {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const instance = cache.get(observable)!\n console.count('subscribe')\n const subscription = instance.observable.subscribe(onStoreChange)\n return () => {\n subscription.unsubscribe()\n }\n },\n [observable],\n )\n\n return useSyncExternalStore<ObservedValueOf<ObservableType>>(\n subscribe,\n () => {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const instance = cache.get(observable)!\n console.count('getSnapshot')\n if (instance.error) {\n throw instance.error\n }\n return instance.snapshot\n },\n typeof initialValue === 'undefined'\n ? undefined\n : () => getValue(initialValue) as ObservedValueOf<ObservableType>,\n )\n}\n","import {observableCallback} from 'observable-callback'\nimport {useEffect, useState} from 'react'\nimport {type Observable} from 'rxjs'\nimport {useEffectEvent} from 'use-effect-event'\n\n/** @public */\nexport function useObservableEvent<T, U>(\n handleEvent: (arg: Observable<T>) => Observable<U>,\n): (arg: T) => void {\n const [[calls$, call]] = useState(() => observableCallback<T>())\n\n const onEvent = useEffectEvent((observable: Observable<T>) => handleEvent(observable))\n\n useEffect(() => {\n const subscription = calls$.pipe((observable) => onEvent(observable)).subscribe()\n return () => subscription.unsubscribe()\n }, [calls$, onEvent])\n\n return call\n}\n"],"names":["getValue","value","cache","WeakMap","useObservable","observable","initialValue","$","_c","has","entry","snapshot","pipe","map","_temp","catchError","_temp2","tap","t0","error","error_0","_temp3","finalize","delete","share","resetOnRefCountZero","_temp4","subscribe","unsubscribe","set","onStoreChange","instance","get","console","count","subscription_0","subscription","t1","instance_0","t2","undefined","useSyncExternalStore","timer","asapScheduler","value_0","of","useObservableEvent","handleEvent","useState","calls$","call","onEvent","useEffectEvent","t3","observable_0","useEffect","observableCallback"],"mappings":";;;;;;AAaA,SAASA,SAAYC,OAA2C;AAC9D,SAAO,OAAOA,SAAU,aAAaA,MAAUA,IAAAA;AACjD;AAQA,MAAMC,4BAAYC,QAA2C;AAiBtDC,SAAAA,cAAAC,YAAAC,cAAA;AAAAC,QAAAA,IAAAC,EAAA,CAAA;AAAA,MAAA,CAIAN,MAAAO,IAAUJ,UAAU,GAAC;AACxB,UAAAK,QAAA;AAAA,MAAAC,UACYX,SAASM,YAAY;AAAA,IAAC;AAE7BD,UAAAA,aAAcA,WAAUO,KAC3BC,IAAAC,OAAoD,GACpDC,WAAAC,MAAsD,GACtDC,IAAAC,CAAAA,QAAA;AAAK,YAAA;AAAA,QAAAP;AAAAA,QAAAQ,OAAAC;AAAAA,MAAAA,IAAAF;AACEP,YAAAA,WAAYA,UACjBD,MAAKS,QAASA;AAAAA,IAAAA,CACf,GAGDN,IAAAQ,MAAyB,GAEzBC,SAAepB,MAAAA,MAAAqB,OAAalB,UAAU,CAAC,GACvCmB,MAAA;AAAA,MAAAC,qBAAAC;AAAAA,IAA0D,CAAA,CAC5D,GAGqBhB,MAAKL,WAAAsB,UACdC,EAAAA,YAEZ1B,GAAAA,MAAA2B,IAAUxB,YAAYK,KAAqD;AAAA,EAAA;AAACQ,MAAAA;AAAAX,WAAAF,cAI5Ea,KAAAY,CAAA,kBAAA;AAEEC,UAAAA,WAAiB7B,MAAA8B,IAAU3B,UAAU;AACrC4B,YAAAC,MAAc,W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AAwC,aAAAxC,OAAA8B,MAAAA,KAAA9B,EAAA,CAAA;AAArF4C,QAAAA,UAAgBC,eAAef,EAAsD;AAAC,MAAAE,IAAAc;AAAA9C,SAAAA,EAAA0C,CAAAA,MAAAA,UAAA1C,SAAA4C,WAE5EZ,KAAAA,MAAA;AACRH,UAAAA,eAAqBa,OAAMrC,KAAA0C,CAAAA,iBAAsBH,QAAQ9C,YAAU,CAAC,EAACsB,UAAW;AAAC,WAAA,MACpES,aAAYR,YAAa;AAAA,EACrCyB,GAAAA,KAAA,CAACJ,QAAQE,OAAO,GAAC5C,OAAA0C,QAAA1C,OAAA4C,SAAA5C,OAAAgC,IAAAhC,OAAA8C,OAAAd,KAAAhC,EAAA,CAAA,GAAA8C,KAAA9C,EAAA,CAAA,IAHpBgD,UAAUhB,IAGPc,EAAiB,GAEbH;AAAI;AAZN,SAAApC,QAAA;AAAA,SAGmC0C,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 snapshot: T\n error?: unknown\n}\n\nconst cache = new WeakMap<Observable<any>, CacheRecord<any>>()\n\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n initialValue: ObservedValueOf<ObservableType> | (() => ObservedValueOf<ObservableType>),\n): ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n): undefined | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue: InitialValue | (() => InitialValue),\n): InitialValue | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue?: InitialValue | (() => InitialValue),\n): InitialValue | ObservedValueOf<ObservableType> {\n if (!cache.has(observable)) {\n const entry: Partial<CacheRecord<ObservedValueOf<ObservableType>>> = {\n snapshot: getValue(initialValue) as ObservedValueOf<ObservableType>,\n }\n entry.observable = observable.pipe(\n map((value) => ({snapshot: value, error: undefined})),\n catchError((error) => of({snapshot: undefined, error})),\n tap(({snapshot, error}) => {\n entry.snapshot = snapshot\n entry.error = error\n }),\n // Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state\n // and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.\n map((value) => void value),\n // Ensure that the cache entry is deleted when the observable completes or errors.\n finalize(() => cache.delete(observable)),\n share({resetOnRefCountZero: () => timer(0, asapScheduler)}),\n )\n\n // Eagerly subscribe to sync set `entry.currentValue` to what the observable returns, and keep the observable alive until the component unmounts.\n const subscription = entry.observable.subscribe()\n subscription.unsubscribe()\n\n cache.set(observable, entry as CacheRecord<ObservedValueOf<ObservableType>>)\n }\n\n const subscribe = useCallback(\n (onStoreChange: () => void) => {\n const instance = cache.get(observable)!\n const subscription = instance.observable.subscribe(onStoreChange)\n return () => {\n subscription.unsubscribe()\n }\n },\n [observable],\n )\n\n return useSyncExternalStore<ObservedValueOf<ObservableType>>(\n subscribe,\n () => {\n const instance = cache.get(observable)!\n if (instance.error) {\n throw instance.error\n }\n return instance.snapshot\n },\n typeof initialValue === 'undefined'\n ? undefined\n : () => getValue(initialValue) as ObservedValueOf<ObservableType>,\n )\n}\n","import {observableCallback} from 'observable-callback'\nimport {useEffect, useState} from 'react'\nimport {type Observable} from 'rxjs'\nimport {useEffectEvent} from 'use-effect-event'\n\n/** @public */\nexport function useObservableEvent<T, U>(\n handleEvent: (arg: Observable<T>) => Observable<U>,\n): (arg: T) => void {\n const [[calls$, call]] = useState(() => observableCallback<T>())\n\n const onEvent = useEffectEvent((observable: Observable<T>) => handleEvent(observable))\n\n useEffect(() => {\n const subscription = calls$.pipe((observable) => onEvent(observable)).subscribe()\n return () => subscription.unsubscribe()\n }, [calls$, onEvent])\n\n return call\n}\n"],"names":["getValue","value","cache","WeakMap","useObservable","observable","initialValue","$","_c","has","entry","snapshot","pipe","map","_temp","catchError","_temp2","tap","t0","error","error_0","_temp3","finalize","delete","share","resetOnRefCountZero","_temp4","subscribe","unsubscribe","set","onStoreChange","subscription_0","get","subscription","t1","instance_0","instance","t2","undefined","useSyncExternalStore","timer","asapScheduler","value_0","of","useObservableEvent","handleEvent","useState","calls$","call","onEvent","useEffectEvent","t3","observable_0","useEffect","observableCallback"],"mappings":";;;;;;AAaA,SAASA,SAAYC,OAA2C;AAC9D,SAAO,OAAOA,SAAU,aAAaA,MAAUA,IAAAA;AACjD;AAQA,MAAMC,4BAAYC,QAA2C;AAiBtDC,SAAAA,cAAAC,YAAAC,cAAA;AAAAC,QAAAA,IAAAC,EAAA,CAAA;AAAA,MAAA,CAIAN,MAAAO,IAAUJ,UAAU,GAAC;AACxB,UAAAK,QAAA;AAAA,MAAAC,UACYX,SAASM,YAAY;AAAA,IAAC;AAE7BD,UAAAA,aAAcA,WAAUO,KAC3BC,IAAAC,OAAoD,GACpDC,WAAAC,MAAsD,GACtDC,IAAAC,CAAAA,QAAA;AAAK,YAAA;AAAA,QAAAP;AAAAA,QAAAQ,OAAAC;AAAAA,MAAAA,IAAAF;AACEP,YAAAA,WAAYA,UACjBD,MAAKS,QAASA;AAAAA,IAAAA,CACf,GAGDN,IAAAQ,MAAyB,GAEzBC,SAAepB,MAAAA,MAAAqB,OAAalB,UAAU,CAAC,GACvCmB,MAAA;AAAA,MAAAC,qBAAAC;AAAAA,IAA0D,CAAA,CAC5D,GAGqBhB,MAAKL,WAAAsB,UACdC,EAAAA,YAEZ1B,GAAAA,MAAA2B,IAAUxB,YAAYK,KAAqD;AAAA,EAAA;AAACQ,MAAAA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A,eAA+BwC,YAAYxC,UAAU,GAACE,OAAAsC,aAAAtC,OAAA2B,MAAAA,KAAA3B,EAAA,CAAA;AAArF0C,QAAAA,UAAgBC,eAAehB,EAAsD;AAAC,MAAAG,IAAAc;AAAA5C,SAAAA,EAAAwC,CAAAA,MAAAA,UAAAxC,SAAA0C,WAE5EZ,KAAAA,MAAA;AACRJ,UAAAA,eAAqBc,OAAMnC,KAAAwC,CAAAA,iBAAsBH,QAAQ5C,YAAU,CAAC,EAACsB,UAAW;AAAC,WAAA,MACpEM,aAAYL,YAAa;AAAA,EACrCuB,GAAAA,KAAA,CAACJ,QAAQE,OAAO,GAAC1C,OAAAwC,QAAAxC,OAAA0C,SAAA1C,OAAA8B,IAAA9B,OAAA4C,OAAAd,KAAA9B,EAAA,CAAA,GAAA4C,KAAA5C,EAAA,CAAA,IAHpB8C,UAAUhB,IAGPc,EAAiB,GAEbH;AAAI;AAZN,SAAAlC,QAAA;AAAA,SAGmCwC,mBAAsB;AAAC;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-rx",
3
- "version": "4.1.0-canary.3",
3
+ "version": "4.1.0-canary.4",
4
4
  "description": "React + RxJS = <3",
5
5
  "keywords": [
6
6
  "action",
@@ -70,9 +70,7 @@ export function useObservable<ObservableType extends Observable<any>, InitialVal
70
70
 
71
71
  const subscribe = useCallback(
72
72
  (onStoreChange: () => void) => {
73
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
74
73
  const instance = cache.get(observable)!
75
- console.count('subscribe')
76
74
  const subscription = instance.observable.subscribe(onStoreChange)
77
75
  return () => {
78
76
  subscription.unsubscribe()
@@ -84,9 +82,7 @@ export function useObservable<ObservableType extends Observable<any>, InitialVal
84
82
  return useSyncExternalStore<ObservedValueOf<ObservableType>>(
85
83
  subscribe,
86
84
  () => {
87
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
88
85
  const instance = cache.get(observable)!
89
- console.count('getSnapshot')
90
86
  if (instance.error) {
91
87
  throw instance.error
92
88
  }