react-rx 4.2.0 → 4.2.1

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
@@ -5,24 +5,21 @@ var reactCompilerRuntime = require("react-compiler-runtime"), react = require("r
5
5
  function getValue(value) {
6
6
  return typeof value == "function" ? value() : value;
7
7
  }
8
- const cache = /* @__PURE__ */ new WeakMap();
8
+ const cache = /* @__PURE__ */ new WeakMap(), EMPTY_OBJECT = {};
9
9
  function useObservable(observable, initialValue, t0) {
10
- const $ = reactCompilerRuntime.c(12);
11
- let t1;
12
- $[0] !== t0 ? (t1 = t0 === void 0 ? {} : t0, $[0] = t0, $[1] = t1) : t1 = $[1];
13
- const options = t1, {
14
- disabled: t2
15
- } = options, disabled = t2 === void 0 ? !1 : t2;
10
+ const $ = reactCompilerRuntime.c(10), options = t0 === void 0 ? EMPTY_OBJECT : t0, {
11
+ disabled: t1
12
+ } = options, disabled = t1 === void 0 ? !1 : t1;
16
13
  if (!cache.has(observable)) {
17
14
  const state = {
18
15
  didEmit: !1
19
16
  }, entry = {
20
17
  state,
21
- observable: observable.pipe(operators.map(_temp$1), rxjs.catchError(_temp2), operators.tap((t32) => {
18
+ observable: observable.pipe(operators.map(_temp$1), rxjs.catchError(_temp2), operators.tap((t22) => {
22
19
  const {
23
20
  snapshot,
24
21
  error: error_0
25
- } = t32;
22
+ } = t22;
26
23
  state.didEmit = !0, state.snapshot = snapshot, state.error = error_0;
27
24
  }), operators.map(_temp3), rxjs.finalize(() => cache.delete(observable)), rxjs.share({
28
25
  resetOnRefCountZero: _temp4
@@ -35,23 +32,23 @@ function useObservable(observable, initialValue, t0) {
35
32
  };
36
33
  entry.observable.subscribe().unsubscribe(), cache.set(observable, entry);
37
34
  }
35
+ let t2;
36
+ $[0] !== observable ? (t2 = cache.get(observable), $[0] = observable, $[1] = t2) : t2 = $[1];
37
+ const instance = t2;
38
38
  let t3;
39
- $[2] !== observable ? (t3 = cache.get(observable), $[2] = observable, $[3] = t3) : t3 = $[3];
40
- const instance = t3;
41
- let t4;
42
- $[4] !== disabled || $[5] !== instance.observable ? (t4 = (onStoreChange) => {
39
+ $[2] !== disabled || $[3] !== instance.observable ? (t3 = (onStoreChange) => {
43
40
  if (disabled)
44
41
  return _temp5;
45
42
  const subscription_0 = instance.observable.subscribe(onStoreChange);
46
43
  return () => {
47
44
  subscription_0.unsubscribe();
48
45
  };
49
- }, $[4] = disabled, $[5] = instance.observable, $[6] = t4) : t4 = $[6];
50
- const subscribe = t4;
46
+ }, $[2] = disabled, $[3] = instance.observable, $[4] = t3) : t3 = $[4];
47
+ const subscribe = t3;
48
+ let t4;
49
+ $[5] !== initialValue || $[6] !== instance ? (t4 = () => instance.getSnapshot(initialValue), $[5] = initialValue, $[6] = instance, $[7] = t4) : t4 = $[7];
51
50
  let t5;
52
- $[7] !== initialValue || $[8] !== instance ? (t5 = () => instance.getSnapshot(initialValue), $[7] = initialValue, $[8] = instance, $[9] = t5) : t5 = $[9];
53
- let t6;
54
- return $[10] !== initialValue ? (t6 = typeof initialValue > "u" ? void 0 : () => getValue(initialValue), $[10] = initialValue, $[11] = t6) : t6 = $[11], react.useSyncExternalStore(subscribe, t5, t6);
51
+ return $[8] !== initialValue ? (t5 = typeof initialValue > "u" ? void 0 : () => getValue(initialValue), $[8] = initialValue, $[9] = t5) : t5 = $[9], react.useSyncExternalStore(subscribe, t4, t5);
55
52
  }
56
53
  function _temp5() {
57
54
  }
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../src/useObservable.ts","../src/useObservableEvent.ts"],"sourcesContent":["import {useCallback, useMemo, 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 as () => any)() : value) as T extends () => infer U\n ? U\n : T\n}\n\ninterface ObservableState<T> {\n didEmit: boolean\n snapshot?: T\n error?: unknown\n}\n\ninterface CacheRecord<T> {\n observable: Observable<void>\n state: {\n didEmit: boolean\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 interface UseObservableOptions {\n disabled?: boolean\n}\n\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n initialValue: ObservedValueOf<ObservableType> | (() => ObservedValueOf<ObservableType>),\n options?: UseObservableOptions,\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 options?: UseObservableOptions,\n): InitialValue | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue?: InitialValue | (() => InitialValue),\n options: UseObservableOptions = {},\n): InitialValue | ObservedValueOf<ObservableType> {\n const {disabled = false} = options\n\n const instance = useMemo(() => {\n if (!cache.has(observable)) {\n // This separate object is used as a stable reference to the cache entry's snapshot and error.\n // It's used by the `getSnapshot` closure.\n const state: ObservableState<ObservedValueOf<ObservableType>> = {\n didEmit: false,\n }\n const entry: CacheRecord<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.didEmit = true\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 (\n state.didEmit ? state.snapshot : getValue(initialValue)\n ) 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)\n }\n return cache.get(observable)!\n }, [observable])\n\n const subscribe = useCallback(\n (onStoreChange: () => void) => {\n if (disabled) {\n return () => {}\n }\n\n const subscription = instance.observable.subscribe(onStoreChange)\n return () => {\n subscription.unsubscribe()\n }\n },\n [instance.observable, disabled],\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$])\n\n return call\n}\n"],"names":["getValue","value","cache","WeakMap","useObservable","observable","initialValue","t0","$","_c","t1","undefined","options","disabled","t2","has","state","didEmit","entry","pipe","map","_temp","catchError","_temp2","tap","t3","snapshot","error","error_0","_temp3","finalize","delete","share","resetOnRefCountZero","_temp4","getSnapshot","initialValue_0","subscribe","unsubscribe","set","get","instance","t4","onStoreChange","_temp5","subscription_0","subscription","t5","t6","useSyncExternalStore","timer","asapScheduler","value_0","of","useObservableEvent","handleEvent","useState","calls$","call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AAAAA,IAAK0B,OAAAhB;AAAAA,EAAAA;AAAA;ACtEnC,SAAA2C,mBAAAC,aAAA;AAAA,QAAA/C,IAAAC,qBAAAA,EAAA,CAAA,GAGL,CAAAF,EAAA,IAAyBiD,MAAAA,SAAAnC,KAAsC,GAAxD,CAAAoC,QAAAC,IAAA,IAAAnD;AAAc,MAAAG;AAAAF,WAAA+C,eAEU7C,KAAAL,CAAAA,eAA+BkD,YAAYlD,UAAU,GAACG,OAAA+C,aAAA/C,OAAAE,MAAAA,KAAAF,EAAA,CAAA;AAArF,QAAAmD,UAAgBC,eAAAA,eAAelD,EAAsD;AAAC,MAAAI;AAAAN,IAAA,CAAA,MAAAiD,UAAAjD,SAAAmD,WAE5E7C,KAAAA,MAAA;AACR,UAAAgC,eAAqBW,OAAMtC,KAAA0C,CAAAA,iBAAsBF,QAAQtD,YAAU,CAAC,EAACgC,UAAAA;AAAY,WAAA,MACpES,aAAYR,YAAAA;AAAAA,EAAc,GACxC9B,OAAAiD,QAAAjD,OAAAmD,SAAAnD,OAAAM,MAAAA,KAAAN,EAAA,CAAA;AAAA,MAAAiB;AAAA,SAAAjB,SAAAiD,UAAEhC,MAACgC,MAAM,GAACjD,OAAAiD,QAAAjD,OAAAiB,MAAAA,KAAAjB,EAAA,CAAA,GAHXsD,gBAAUhD,IAGPW,EAAQ,GAEJiC;AAAI;AAZN,SAAArC,QAAA;AAAA,SAGmC0C,sCAAAA;AAAuB;;;"}
1
+ {"version":3,"file":"index.cjs","sources":["../src/useObservable.ts","../src/useObservableEvent.ts"],"sourcesContent":["import {useCallback, useMemo, 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 as () => any)() : value) as T extends () => infer U\n ? U\n : T\n}\n\ninterface ObservableState<T> {\n didEmit: boolean\n snapshot?: T\n error?: unknown\n}\n\ninterface CacheRecord<T> {\n observable: Observable<void>\n state: {\n didEmit: boolean\n snapshot?: T\n error?: unknown\n }\n getSnapshot: (initialValue: unknown) => T\n}\n\nconst cache = new WeakMap<Observable<any>, CacheRecord<any>>()\n\nconst EMPTY_OBJECT = {}\n\n/** @public */\nexport interface UseObservableOptions {\n disabled?: boolean\n}\n\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n initialValue: ObservedValueOf<ObservableType> | (() => ObservedValueOf<ObservableType>),\n options?: UseObservableOptions,\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 options?: UseObservableOptions,\n): InitialValue | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue?: InitialValue | (() => InitialValue),\n options: UseObservableOptions = EMPTY_OBJECT,\n): InitialValue | ObservedValueOf<ObservableType> {\n const {disabled = false} = options\n\n const instance = useMemo(() => {\n if (!cache.has(observable)) {\n // This separate object is used as a stable reference to the cache entry's snapshot and error.\n // It's used by the `getSnapshot` closure.\n const state: ObservableState<ObservedValueOf<ObservableType>> = {\n didEmit: false,\n }\n const entry: CacheRecord<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.didEmit = true\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 (\n state.didEmit ? state.snapshot : getValue(initialValue)\n ) 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)\n }\n return cache.get(observable)!\n }, [observable])\n\n const subscribe = useCallback(\n (onStoreChange: () => void) => {\n if (disabled) {\n return () => {}\n }\n\n const subscription = instance.observable.subscribe(onStoreChange)\n return () => {\n subscription.unsubscribe()\n }\n },\n [instance.observable, disabled],\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$])\n\n return call\n}\n"],"names":["getValue","value","cache","WeakMap","EMPTY_OBJECT","useObservable","observable","initialValue","t0","$","_c","options","undefined","disabled","t1","has","state","didEmit","entry","pipe","map","_temp","catchError","_temp2","tap","t2","snapshot","error","error_0","_temp3","finalize","delete","share","resetOnRefCountZero","_temp4","getSnapshot","initialValue_0","subscribe","unsubscribe","set","get","instance","t3","onStoreChange","_temp5","subscription_0","subscription","t4","t5","useSyncExternalStore","timer","asapScheduler","value_0","of","useObservableEvent","handleEv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AAC,aAAA;AAAA,QAAA9C,IAAAC,qBAAAA,EAAA,CAAA,GAGL,CAAAF,EAAA,IAAyBgD,MAAAA,SAAAnC,KAAsC,GAAxD,CAAAoC,QAAAC,IAAA,IAAAlD;AAAc,MAAAM;AAAAL,WAAA8C,eAEUzC,KAAAR,CAAAA,eAA+BiD,YAAYjD,UAAU,GAACG,OAAA8C,aAAA9C,OAAAK,MAAAA,KAAAL,EAAA,CAAA;AAArF,QAAAkD,UAAgBC,eAAAA,eAAe9C,EAAsD;AAAC,MAAAW;AAAAhB,IAAA,CAAA,MAAAgD,UAAAhD,SAAAkD,WAE5ElC,KAAAA,MAAA;AACR,UAAAqB,eAAqBW,OAAMtC,KAAA0C,CAAAA,iBAAsBF,QAAQrD,YAAU,CAAC,EAAC+B,UAAAA;AAAY,WAAA,MACpES,aAAYR,YAAAA;AAAAA,EAAc,GACxC7B,OAAAgD,QAAAhD,OAAAkD,SAAAlD,OAAAgB,MAAAA,KAAAhB,EAAA,CAAA;AAAA,MAAAiC;AAAA,SAAAjC,SAAAgD,UAAEf,MAACe,MAAM,GAAChD,OAAAgD,QAAAhD,OAAAiC,MAAAA,KAAAjC,EAAA,CAAA,GAHXqD,gBAAUrC,IAGPiB,EAAQ,GAEJgB;AAAI;AAZN,SAAArC,QAAA;AAAA,SAGmC0C,sCAAAA;AAAuB;;;"}
package/dist/index.js CHANGED
@@ -8,24 +8,21 @@ import { useEffectEvent } from "use-effect-event";
8
8
  function getValue(value) {
9
9
  return typeof value == "function" ? value() : value;
10
10
  }
11
- const cache = /* @__PURE__ */ new WeakMap();
11
+ const cache = /* @__PURE__ */ new WeakMap(), EMPTY_OBJECT = {};
12
12
  function useObservable(observable, initialValue, t0) {
13
- const $ = c(12);
14
- let t1;
15
- $[0] !== t0 ? (t1 = t0 === void 0 ? {} : t0, $[0] = t0, $[1] = t1) : t1 = $[1];
16
- const options = t1, {
17
- disabled: t2
18
- } = options, disabled = t2 === void 0 ? !1 : t2;
13
+ const $ = c(10), options = t0 === void 0 ? EMPTY_OBJECT : t0, {
14
+ disabled: t1
15
+ } = options, disabled = t1 === void 0 ? !1 : t1;
19
16
  if (!cache.has(observable)) {
20
17
  const state = {
21
18
  didEmit: !1
22
19
  }, entry = {
23
20
  state,
24
- observable: observable.pipe(map(_temp$1), catchError(_temp2), tap((t32) => {
21
+ observable: observable.pipe(map(_temp$1), catchError(_temp2), tap((t22) => {
25
22
  const {
26
23
  snapshot,
27
24
  error: error_0
28
- } = t32;
25
+ } = t22;
29
26
  state.didEmit = !0, state.snapshot = snapshot, state.error = error_0;
30
27
  }), map(_temp3), finalize(() => cache.delete(observable)), share({
31
28
  resetOnRefCountZero: _temp4
@@ -38,23 +35,23 @@ function useObservable(observable, initialValue, t0) {
38
35
  };
39
36
  entry.observable.subscribe().unsubscribe(), cache.set(observable, entry);
40
37
  }
38
+ let t2;
39
+ $[0] !== observable ? (t2 = cache.get(observable), $[0] = observable, $[1] = t2) : t2 = $[1];
40
+ const instance = t2;
41
41
  let t3;
42
- $[2] !== observable ? (t3 = cache.get(observable), $[2] = observable, $[3] = t3) : t3 = $[3];
43
- const instance = t3;
44
- let t4;
45
- $[4] !== disabled || $[5] !== instance.observable ? (t4 = (onStoreChange) => {
42
+ $[2] !== disabled || $[3] !== instance.observable ? (t3 = (onStoreChange) => {
46
43
  if (disabled)
47
44
  return _temp5;
48
45
  const subscription_0 = instance.observable.subscribe(onStoreChange);
49
46
  return () => {
50
47
  subscription_0.unsubscribe();
51
48
  };
52
- }, $[4] = disabled, $[5] = instance.observable, $[6] = t4) : t4 = $[6];
53
- const subscribe = t4;
49
+ }, $[2] = disabled, $[3] = instance.observable, $[4] = t3) : t3 = $[4];
50
+ const subscribe = t3;
51
+ let t4;
52
+ $[5] !== initialValue || $[6] !== instance ? (t4 = () => instance.getSnapshot(initialValue), $[5] = initialValue, $[6] = instance, $[7] = t4) : t4 = $[7];
54
53
  let t5;
55
- $[7] !== initialValue || $[8] !== instance ? (t5 = () => instance.getSnapshot(initialValue), $[7] = initialValue, $[8] = instance, $[9] = t5) : t5 = $[9];
56
- let t6;
57
- return $[10] !== initialValue ? (t6 = typeof initialValue > "u" ? void 0 : () => getValue(initialValue), $[10] = initialValue, $[11] = t6) : t6 = $[11], useSyncExternalStore(subscribe, t5, t6);
54
+ return $[8] !== initialValue ? (t5 = typeof initialValue > "u" ? void 0 : () => getValue(initialValue), $[8] = initialValue, $[9] = t5) : t5 = $[9], useSyncExternalStore(subscribe, t4, t5);
58
55
  }
59
56
  function _temp5() {
60
57
  }
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/useObservable.ts","../src/useObservableEvent.ts"],"sourcesContent":["import {useCallback, useMemo, 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 as () => any)() : value) as T extends () => infer U\n ? U\n : T\n}\n\ninterface ObservableState<T> {\n didEmit: boolean\n snapshot?: T\n error?: unknown\n}\n\ninterface CacheRecord<T> {\n observable: Observable<void>\n state: {\n didEmit: boolean\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 interface UseObservableOptions {\n disabled?: boolean\n}\n\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n initialValue: ObservedValueOf<ObservableType> | (() => ObservedValueOf<ObservableType>),\n options?: UseObservableOptions,\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 options?: UseObservableOptions,\n): InitialValue | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue?: InitialValue | (() => InitialValue),\n options: UseObservableOptions = {},\n): InitialValue | ObservedValueOf<ObservableType> {\n const {disabled = false} = options\n\n const instance = useMemo(() => {\n if (!cache.has(observable)) {\n // This separate object is used as a stable reference to the cache entry's snapshot and error.\n // It's used by the `getSnapshot` closure.\n const state: ObservableState<ObservedValueOf<ObservableType>> = {\n didEmit: false,\n }\n const entry: CacheRecord<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.didEmit = true\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 (\n state.didEmit ? state.snapshot : getValue(initialValue)\n ) 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)\n }\n return cache.get(observable)!\n }, [observable])\n\n const subscribe = useCallback(\n (onStoreChange: () => void) => {\n if (disabled) {\n return () => {}\n }\n\n const subscription = instance.observable.subscribe(onStoreChange)\n return () => {\n subscription.unsubscribe()\n }\n },\n [instance.observable, disabled],\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$])\n\n return call\n}\n"],"names":["getValue","value","cache","WeakMap","useObservable","observable","initialValue","t0","$","_c","t1","undefined","options","disabled","t2","has","state","didEmit","entry","pipe","map","_temp","catchError","_temp2","tap","t3","snapshot","error","error_0","_temp3","finalize","delete","share","resetOnRefCountZero","_temp4","getSnapshot","initialValue_0","subscribe","unsubscribe","set","get","instance","t4","onStoreChange","_temp5","subscription_0","subscription","t5","t6","useSyncExternalStore","timer","asapScheduler","value_0","of","useObservableEvent","handleEvent","useState","calls$","call","onEvent","useEffectEvent","observable_0","useEffect","observableCallback"],"mappings":";;;;;;;AAaA,SAASA,SAAYC,OAA2C;AAC9D,SAAQ,OAAOA,SAAU,aAAcA,MAAAA,IAAwBA;AAGjE;AAkBA,MAAMC,4BAAYC,QAAAA;AAwBX,SAAAC,cAAAC,YAAAC,cAAAC,IAAA;AAAA,QAAAC,IAAAC,EAAA,EAAA;AAAA,MAAAC;AAAAF,WAAAD,MAGLG,KAAAH,OAAkCI,cAAlCJ,IAAkCC,OAAAD,IAAAC,OAAAE,MAAAA,KAAAF,EAAA,CAAA;AAAlC,QAAAI,UAAAF,IAEA;AAAA,IAAAG,UAAAC;AAAAA,EAAAA,IAA2BF,SAApBC,WAAAC,OAAgBH,cAAhBG;AAAgB,MAAA,CAGhBZ,MAAAa,IAAUV,UAAU,GAAC;AAGxB,UAAAW,QAAA;AAAA,MAAAC,SAAA;AAAA,IAAA,GAGAC,QAA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AAAM,MAAAA,KAAAN,EAAA,CAAA;AAAA,MAAAiB;AAAA,SAAAjB,SAAAiD,UAAEhC,MAACgC,MAAM,GAACjD,OAAAiD,QAAAjD,OAAAiB,MAAAA,KAAAjB,EAAA,CAAA,GAHXsD,UAAUhD,IAGPW,EAAQ,GAEJiC;AAAI;AAZN,SAAArC,QAAA;AAAA,SAGmC0C,mBAAAA;AAAuB;"}
1
+ {"version":3,"file":"index.js","sources":["../src/useObservable.ts","../src/useObservableEvent.ts"],"sourcesContent":["import {useCallback, useMemo, 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 as () => any)() : value) as T extends () => infer U\n ? U\n : T\n}\n\ninterface ObservableState<T> {\n didEmit: boolean\n snapshot?: T\n error?: unknown\n}\n\ninterface CacheRecord<T> {\n observable: Observable<void>\n state: {\n didEmit: boolean\n snapshot?: T\n error?: unknown\n }\n getSnapshot: (initialValue: unknown) => T\n}\n\nconst cache = new WeakMap<Observable<any>, CacheRecord<any>>()\n\nconst EMPTY_OBJECT = {}\n\n/** @public */\nexport interface UseObservableOptions {\n disabled?: boolean\n}\n\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n initialValue: ObservedValueOf<ObservableType> | (() => ObservedValueOf<ObservableType>),\n options?: UseObservableOptions,\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 options?: UseObservableOptions,\n): InitialValue | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue?: InitialValue | (() => InitialValue),\n options: UseObservableOptions = EMPTY_OBJECT,\n): InitialValue | ObservedValueOf<ObservableType> {\n const {disabled = false} = options\n\n const instance = useMemo(() => {\n if (!cache.has(observable)) {\n // This separate object is used as a stable reference to the cache entry's snapshot and error.\n // It's used by the `getSnapshot` closure.\n const state: ObservableState<ObservedValueOf<ObservableType>> = {\n didEmit: false,\n }\n const entry: CacheRecord<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.didEmit = true\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 (\n state.didEmit ? state.snapshot : getValue(initialValue)\n ) 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)\n }\n return cache.get(observable)!\n }, [observable])\n\n const subscribe = useCallback(\n (onStoreChange: () => void) => {\n if (disabled) {\n return () => {}\n }\n\n const subscription = instance.observable.subscribe(onStoreChange)\n return () => {\n subscription.unsubscribe()\n }\n },\n [instance.observable, disabled],\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$])\n\n return call\n}\n"],"names":["getValue","value","cache","WeakMap","EMPTY_OBJECT","useObservable","observable","initialValue","t0","$","_c","options","undefined","disabled","t1","has","state","didEmit","entry","pipe","map","_temp","catchError","_temp2","tap","t2","snapshot","error","error_0","_temp3","finalize","delete","share","resetOnRefCountZero","_temp4","getSnapshot","initialValue_0","subscribe","unsubscribe","set","get","instance","t3","onStoreChange","_temp5","subscription_0","subscription","t4","t5","useSyncExternalStore","timer","asapScheduler","value_0","of","useObservableEvent","handleEvent","useState","calls$","call","onEvent","useEffectEvent","observable_0","useEffect","observableCallback"],"mappings":";;;;;;;AAaA,SAASA,SAAYC,OAA2C;AAC9D,SAAQ,OAAOA,SAAU,aAAcA,MAAAA,IAAwBA;AAGjE;AAkBA,MAAMC,QAAQ,oBAAIC,WAEZC,eAAe,CAAA;AAwBd,SAAAC,cAAAC,YAAAC,cAAAC,IAAA;AAAA,QAAAC,IAAAC,EAAA,EAAA,GAGLC,UAAAH,OAA4CI,SAAAR,eAA5CI,IAEA;AAAA,IAAAK,UAAAC;AAAAA,EAAAA,IAA2BH,SAApBE,WAAAC,OAAgBF,cAAhBE;AAAgB,MAAA,CAGhBZ,MAAAa,IAAUT,UAAU,GAAC;AAGxB,UAAAU,QAAA;AAAA,MAAAC,SAAA;AAAA,IAAA,GAGAC,QAAA;AAAA,MAAAF;AAAAA,MAAAV,YAEcA,WAAUa,KACpBC,IAAAC,OAAoD,GACpDC,WAAAC,MAAsD,GACtDC,IAAAC,CAAAA,QAAA;AAAK,cAAA;AAAA,UAAAC;AAAAA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EAAA,CAAA,GAHXqD,UAAUrC,IAGPiB,EAAQ,GAEJgB;AAAI;AAZN,SAAArC,QAAA;AAAA,SAGmC0C,mBAAAA;AAAuB;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-rx",
3
- "version": "4.2.0",
3
+ "version": "4.2.1",
4
4
  "description": "React + RxJS = <3",
5
5
  "keywords": [
6
6
  "action",
@@ -80,7 +80,7 @@
80
80
  },
81
81
  "devDependencies": {
82
82
  "@sanity/browserslist-config": "^1.0.5",
83
- "@sanity/pkg-utils": "^8.1.4",
83
+ "@sanity/pkg-utils": "^8.1.14",
84
84
  "@sanity/prettier-config": "^1.0.6",
85
85
  "@sanity/semantic-release-preset": "^5.0.0",
86
86
  "@testing-library/dom": "^10.4.1",
@@ -90,7 +90,7 @@
90
90
  "@types/react-dom": "^19.1.9",
91
91
  "@typescript-eslint/eslint-plugin": "^8.41.0",
92
92
  "@typescript-eslint/parser": "^8.41.0",
93
- "@vitejs/plugin-react": "^5.0.2",
93
+ "@vitejs/plugin-react": "^5.0.3",
94
94
  "babel-plugin-react-compiler": "19.1.0-rc.3",
95
95
  "eslint": "^9.34.0",
96
96
  "eslint-config-prettier": "^10.1.8",
@@ -15,13 +15,13 @@ test('useObservable with no initial value can be undefined', () => {
15
15
  test('return type of useObservable with initial value is not undefined', () => {
16
16
  const observable = of('foo')
17
17
  //@ts-expect-error - because initial value is given, the return type can never be undefined
18
- expectTypeOf(useObservable(observable, {initialValue: 'bar'})).toEqualTypeOf<string | undefined>()
18
+ expectTypeOf(useObservable(observable, 'bar')).toEqualTypeOf<string | undefined>()
19
19
  })
20
20
 
21
21
  test('useObservable with initial value if a different type returns a union of the observed type and the initial value type', () => {
22
22
  const observable = of('foo')
23
23
 
24
- expectTypeOf(useObservable(observable, {initialValue: 1})).toEqualTypeOf<string | number>()
25
- expectTypeOf(useObservable(observable, {initialValue: () => 1})).toEqualTypeOf<string | number>()
26
- expectTypeOf(useObservable(observable, {initialValue: 'foo'})).toEqualTypeOf<string>()
24
+ expectTypeOf(useObservable(observable, 1)).toEqualTypeOf<string | number>()
25
+ expectTypeOf(useObservable(observable, () => 1)).toEqualTypeOf<string | number>()
26
+ expectTypeOf(useObservable(observable, 'foo')).toEqualTypeOf<string>()
27
27
  })
@@ -35,6 +35,8 @@ interface CacheRecord<T> {
35
35
 
36
36
  const cache = new WeakMap<Observable<any>, CacheRecord<any>>()
37
37
 
38
+ const EMPTY_OBJECT = {}
39
+
38
40
  /** @public */
39
41
  export interface UseObservableOptions {
40
42
  disabled?: boolean
@@ -60,7 +62,7 @@ export function useObservable<ObservableType extends Observable<any>, InitialVal
60
62
  export function useObservable<ObservableType extends Observable<any>, InitialValue>(
61
63
  observable: ObservableType,
62
64
  initialValue?: InitialValue | (() => InitialValue),
63
- options: UseObservableOptions = {},
65
+ options: UseObservableOptions = EMPTY_OBJECT,
64
66
  ): InitialValue | ObservedValueOf<ObservableType> {
65
67
  const {disabled = false} = options
66
68