react-rx 4.1.2 → 4.1.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
@@ -7,61 +7,51 @@ function getValue(value) {
7
7
  const cache = /* @__PURE__ */ new WeakMap();
8
8
  function useObservable(observable, initialValue) {
9
9
  const $ = reactCompilerRuntime.c(9);
10
+ let t0;
10
11
  if (!cache.has(observable)) {
11
12
  const state = {
12
- snapshot: void 0,
13
- error: void 0
13
+ didEmit: !1
14
14
  }, entry = {
15
15
  state,
16
- observable: observable.pipe(operators.map(_temp$1), rxjs.catchError(_temp2), operators.tap((t02) => {
16
+ observable: observable.pipe(operators.map((value) => ({
17
+ snapshot: value,
18
+ error: void 0
19
+ })), rxjs.catchError((error) => rxjs.of({
20
+ snapshot: void 0,
21
+ error
22
+ })), operators.tap((t12) => {
17
23
  const {
18
24
  snapshot,
19
25
  error: error_0
20
- } = t02;
21
- state.snapshot = snapshot, state.error = error_0;
22
- }), operators.map(_temp3), rxjs.finalize(() => cache.delete(observable)), rxjs.share({
23
- resetOnRefCountZero: _temp4
26
+ } = t12;
27
+ state.didEmit = !0, state.snapshot = snapshot, state.error = error_0;
28
+ }), operators.map((value_0) => {
29
+ }), rxjs.finalize(() => cache.delete(observable)), rxjs.share({
30
+ resetOnRefCountZero: () => rxjs.timer(0, rxjs.asapScheduler)
24
31
  })),
25
32
  getSnapshot: (initialValue_0) => {
26
33
  if (state.error)
27
34
  throw state.error;
28
- return state.snapshot ?? getValue(initialValue_0);
35
+ return state.didEmit ? state.snapshot : getValue(initialValue_0);
29
36
  }
30
37
  };
31
38
  entry.observable.subscribe().unsubscribe(), cache.set(observable, entry);
32
39
  }
33
- let t0;
34
- $[0] !== observable ? (t0 = cache.get(observable), $[0] = observable, $[1] = t0) : t0 = $[1];
35
- const instance = t0;
36
40
  let t1;
37
- $[2] !== instance.observable ? (t1 = (onStoreChange) => {
41
+ $[0] !== observable ? (t1 = cache.get(observable), $[0] = observable, $[1] = t1) : t1 = $[1], t0 = t1;
42
+ const instance = t0;
43
+ let t2;
44
+ $[2] !== instance.observable ? (t2 = (onStoreChange) => {
38
45
  const subscription_0 = instance.observable.subscribe(onStoreChange);
39
46
  return () => {
40
47
  subscription_0.unsubscribe();
41
48
  };
42
- }, $[2] = instance.observable, $[3] = t1) : t1 = $[3];
43
- const subscribe = t1;
44
- let t2;
45
- $[4] !== instance || $[5] !== initialValue ? (t2 = () => instance.getSnapshot(initialValue), $[4] = instance, $[5] = initialValue, $[6] = t2) : t2 = $[6];
49
+ }, $[2] = instance.observable, $[3] = t2) : t2 = $[3];
50
+ const subscribe = t2;
46
51
  let t3;
47
- return $[7] !== initialValue ? (t3 = typeof initialValue > "u" ? void 0 : () => getValue(initialValue), $[7] = initialValue, $[8] = t3) : t3 = $[8], react.useSyncExternalStore(subscribe, t2, t3);
48
- }
49
- function _temp4() {
50
- return rxjs.timer(0, rxjs.asapScheduler);
51
- }
52
- function _temp3(value_0) {
53
- }
54
- function _temp2(error) {
55
- return rxjs.of({
56
- snapshot: void 0,
57
- error
58
- });
59
- }
60
- function _temp$1(value) {
61
- return {
62
- snapshot: value,
63
- error: void 0
64
- };
52
+ $[4] !== instance || $[5] !== initialValue ? (t3 = () => instance.getSnapshot(initialValue), $[4] = instance, $[5] = initialValue, $[6] = t3) : t3 = $[6];
53
+ let t4;
54
+ return $[7] !== initialValue ? (t4 = typeof initialValue > "u" ? void 0 : () => getValue(initialValue), $[7] = initialValue, $[8] = t4) : t4 = $[8], react.useSyncExternalStore(subscribe, t3, t4);
65
55
  }
66
56
  function useObservableEvent(handleEvent) {
67
57
  const $ = reactCompilerRuntime.c(6), [t0] = react.useState(_temp), [calls$, call] = t0;
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../src/useObservable.ts","../src/useObservableEvent.ts"],"sourcesContent":["import {useCallback, useSyncExternalStore} from 'react'\nimport {\n asapScheduler,\n catchError,\n finalize,\n type Observable,\n type ObservedValueOf,\n of,\n share,\n timer,\n} from 'rxjs'\nimport {map, tap} from 'rxjs/operators'\n\nfunction getValue<T>(value: T): T extends () => infer U ? U : T {\n return typeof value === 'function' ? value() : value\n}\n\ninterface CacheRecord<T> {\n observable: Observable<void>\n state: {\n snapshot: T\n error?: unknown\n }\n getSnapshot: (initialValue: unknown) => T\n}\n\nconst cache = new WeakMap<Observable<any>, CacheRecord<any>>()\n\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n initialValue: ObservedValueOf<ObservableType> | (() => ObservedValueOf<ObservableType>),\n): ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n): undefined | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue: InitialValue | (() => InitialValue),\n): InitialValue | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue?: InitialValue | (() => InitialValue),\n): InitialValue | ObservedValueOf<ObservableType> {\n if (!cache.has(observable)) {\n // 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 = {\n snapshot: undefined as ObservedValueOf<ObservableType>,\n error: undefined,\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.snapshot = snapshot\n state.error = error\n }),\n // Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state\n // and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.\n map((value) => void value),\n // Ensure that the cache entry is deleted when the observable completes or errors.\n finalize(() => cache.delete(observable)),\n share({resetOnRefCountZero: () => timer(0, asapScheduler)}),\n ),\n getSnapshot: (initialValue) => {\n if (state.error) {\n throw state.error\n }\n return state.snapshot ?? (getValue(initialValue) as ObservedValueOf<ObservableType>)\n },\n }\n\n // Eagerly subscribe to sync set `state.snapshot` to what the observable returns, and keep the observable alive until the component unmounts.\n const subscription = entry.observable.subscribe()\n subscription.unsubscribe()\n\n cache.set(observable, entry as CacheRecord<ObservedValueOf<ObservableType>>)\n }\n const instance = cache.get(observable)!\n\n const subscribe = useCallback(\n (onStoreChange: () => void) => {\n const subscription = instance.observable.subscribe(onStoreChange)\n return () => {\n subscription.unsubscribe()\n }\n },\n [instance.observable],\n )\n\n return useSyncExternalStore<ObservedValueOf<ObservableType>>(\n subscribe,\n () => {\n return instance.getSnapshot(initialValue)\n },\n typeof initialValue === 'undefined'\n ? undefined\n : () => getValue(initialValue) as ObservedValueOf<ObservableType>,\n )\n}\n","import {observableCallback} from 'observable-callback'\nimport {useEffect, useState} from 'react'\nimport {type Observable} from 'rxjs'\nimport {useEffectEvent} from 'use-effect-event'\n\n/** @public */\nexport function useObservableEvent<T, U>(\n handleEvent: (arg: Observable<T>) => Observable<U>,\n): (arg: T) => void {\n const [[calls$, call]] = useState(() => observableCallback<T>())\n\n const onEvent = useEffectEvent((observable: Observable<T>) => handleEvent(observable))\n\n useEffect(() => {\n const subscription = calls$.pipe((observable) => onEvent(observable)).subscribe()\n return () => subscription.unsubscribe()\n }, [calls$, onEvent])\n\n return call\n}\n"],"names":["getValue","value","cache","WeakMap","useObservable","observable","initialValue","$","_c","has","state","snapshot","undefined","error","entry","pipe","map","_temp","catchError","_temp2","tap","t0","error_0","_temp3","finalize","delete","share","resetOnRefCountZero","_temp4","getSnapshot","initialValue_0","subscribe","unsubscribe","set","get","instance","t1","onStoreChange","subscription_0","subscription","t2","t3","useSyncExternalStore","timer","asapScheduler","value_0","of","useObservableEvent","handleEvent","useState","calls$","call","onEvent","useEffectEvent","observable_0","useEffect","observableCallback"],"mappings":";;;AAaA,SAASA,SAAYC,OAA2C;AAC9D,SAAO,OAAOA,SAAU,aAAaA,MAAUA,IAAAA;AACjD;AAW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Z,KAAAA,MAAA;AACRD,UAAAA,eAAqBW,OAAMnC,KAAAuC,CAAAA,iBAAsBF,QAAQ/C,YAAU,CAAC,EAAC0B,UAAW;AAAC,WAAA,MACpEQ,aAAYP,YAAa;AAAA,EACrCS,GAAAA,KAAA,CAACS,QAAQE,OAAO,GAAC7C,OAAA2C,QAAA3C,OAAA6C,SAAA7C,OAAAiC,IAAAjC,OAAAkC,OAAAD,KAAAjC,EAAA,CAAA,GAAAkC,KAAAlC,EAAA,CAAA,IAHpBgD,MAAAA,UAAUf,IAGPC,EAAiB,GAEbU;AAAI;AAZN,SAAAlC,QAAA;AAAA,SAGmCuC,sCAAsB;AAAC;;;"}
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() : value\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 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 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 const subscription = instance.observable.subscribe(onStoreChange)\n return () => {\n subscription.unsubscribe()\n }\n },\n [instance.observable],\n )\n\n return useSyncExternalStore<ObservedValueOf<ObservableType>>(\n subscribe,\n () => {\n return instance.getSnapshot(initialValue)\n },\n typeof initialValue === 'undefined'\n ? undefined\n : () => getValue(initialValue) as ObservedValueOf<ObservableType>,\n )\n}\n","import {observableCallback} from 'observable-callback'\nimport {useEffect, useState} from 'react'\nimport {type Observable} from 'rxjs'\nimport {useEffectEvent} from 'use-effect-event'\n\n/** @public */\nexport function useObservableEvent<T, U>(\n handleEvent: (arg: Observable<T>) => Observable<U>,\n): (arg: T) => void {\n const [[calls$, call]] = useState(() => observableCallback<T>())\n\n const onEvent = useEffectEvent((observable: Observable<T>) => handleEvent(observable))\n\n useEffect(() => {\n const subscription = calls$.pipe((observable) => onEvent(observable)).subscribe()\n return () => subscription.unsubscribe()\n }, [calls$, onEvent])\n\n return call\n}\n"],"names":["getValue","value","cache","WeakMap","useObservable","observable","initialValue","$","_c","t0","has","state","didEmit","entry","pipe","map","snapshot","error","undefined","catchError","of","tap","t1","error_0","value_0","finalize","delete","share","resetOnRefCountZero","timer","asapScheduler","getSnapshot","initialValue_0","subscribe","unsubscribe","set","get","instance","t2","onStoreChange","subscription_0","subscription","t3","t4","useSyncExternalStore","useObservableEvent","handleEvent","useState","_temp","calls$","call","onEvent","useEffectEvent","observable_0","useEffect","observableCallback"],"mappings":";;;AAaA,SAASA,SAAYC,OAA2C;AAC9D,SAAO,OAAOA,SAAU,aAAaA,MAAUA,IAAAA;AACjD;AAkBA,MAAMC,4BAAYC,QAA2C;AAiBtDC,SAAAA,cAAAC,YAAAC,cAAA;AAAAC,QAAAA,IAAAC,uBAAA,CAAA;AAAAC,MAAAA;AAAA,MAAA,CAKEP,MAAAQ,IAAUL,UAAU,GAAC;AAGxB,UAAAM,QAAA;AAAA,MAAAC,SAAA;AAAA,OAGAC,QAAA;AAAA,MAAAF;AAAAA,MAAAN,YAEcA,WAAUS,KACpBC,UAAAA,IAAAd,CAAA,WAAA;AAAA,QAAAe,UAA2Bf;AAAAA,QAAKgB,OAAAC;AAAAA,MAAoB,EAAA,GACpDC,KAAAA,WAAAF,CAAAA,UAAsBG,KAAAA,GAAA;AAAA,QAAAJ,UAAAE;AAAAA,QAAAD;AAAAA,MAAAA,CAA+B,CAAC,GACtDI,cAAAC,CAAAA,QAAA;AAAK,cAAA;AAAA,UAAAN;AAAAA,UAAAC,OAAAM;AAAAA,QAAAA,IAAAD;AACHX,cAAKC,UAAA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A,SAASM,YAAY,GAAoCC,OAAAD,cAAAC,OAAAoC,MAAAA,KAAApC,EAAA,CAAA,GAP9DqC,MAAAA,qBACLX,WACAS,IAGAC,EAGF;AAAC;AC7GI,SAAAE,mBAAAC,aAAA;AAAA,QAAAvC,IAAAC,qBAAAA,EAAA,CAAA,GAGL,CAAAC,EAAA,IAAyBsC,MAAAA,SAAAC,KAAsC,GAAxD,CAAAC,QAAAC,IAAA,IAAAzC;AAAca,MAAAA;AAAAf,WAAAuC,eAEUxB,KAAAjB,CAAAA,eAA+ByC,YAAYzC,UAAU,GAACE,OAAAuC,aAAAvC,OAAAe,MAAAA,KAAAf,EAAA,CAAA;AAArF4C,QAAAA,UAAgBC,8BAAe9B,EAAsD;AAAC,MAAAgB,IAAAI;AAAAnC,SAAAA,EAAA0C,CAAAA,MAAAA,UAAA1C,SAAA4C,WAE5Eb,KAAAA,MAAA;AACRG,UAAAA,eAAqBQ,OAAMnC,KAAAuC,CAAAA,iBAAsBF,QAAQ9C,YAAU,CAAC,EAAC4B,UAAW;AAAC,WAAA,MACpEQ,aAAYP,YAAa;AAAA,EACrCQ,GAAAA,KAAA,CAACO,QAAQE,OAAO,GAAC5C,OAAA0C,QAAA1C,OAAA4C,SAAA5C,OAAA+B,IAAA/B,OAAAmC,OAAAJ,KAAA/B,EAAA,CAAA,GAAAmC,KAAAnC,EAAA,CAAA,IAHpB+C,MAAAA,UAAUhB,IAGPI,EAAiB,GAEbQ;AAAI;AAZN,SAAAF,QAAA;AAAA,SAGmCO,sCAAsB;AAAC;;;"}
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { c } from "react-compiler-runtime";
2
2
  import { useSyncExternalStore, useState, useEffect } from "react";
3
- import { catchError, finalize, share, timer, asapScheduler, of } from "rxjs";
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";
@@ -10,61 +10,51 @@ function getValue(value) {
10
10
  const cache = /* @__PURE__ */ new WeakMap();
11
11
  function useObservable(observable, initialValue) {
12
12
  const $ = c(9);
13
+ let t0;
13
14
  if (!cache.has(observable)) {
14
15
  const state = {
15
- snapshot: void 0,
16
- error: void 0
16
+ didEmit: !1
17
17
  }, entry = {
18
18
  state,
19
- observable: observable.pipe(map(_temp$1), catchError(_temp2), tap((t02) => {
19
+ observable: observable.pipe(map((value) => ({
20
+ snapshot: value,
21
+ error: void 0
22
+ })), catchError((error) => of({
23
+ snapshot: void 0,
24
+ error
25
+ })), tap((t12) => {
20
26
  const {
21
27
  snapshot,
22
28
  error: error_0
23
- } = t02;
24
- state.snapshot = snapshot, state.error = error_0;
25
- }), map(_temp3), finalize(() => cache.delete(observable)), share({
26
- resetOnRefCountZero: _temp4
29
+ } = t12;
30
+ state.didEmit = !0, state.snapshot = snapshot, state.error = error_0;
31
+ }), map((value_0) => {
32
+ }), finalize(() => cache.delete(observable)), share({
33
+ resetOnRefCountZero: () => timer(0, asapScheduler)
27
34
  })),
28
35
  getSnapshot: (initialValue_0) => {
29
36
  if (state.error)
30
37
  throw state.error;
31
- return state.snapshot ?? getValue(initialValue_0);
38
+ return state.didEmit ? state.snapshot : getValue(initialValue_0);
32
39
  }
33
40
  };
34
41
  entry.observable.subscribe().unsubscribe(), cache.set(observable, entry);
35
42
  }
36
- let t0;
37
- $[0] !== observable ? (t0 = cache.get(observable), $[0] = observable, $[1] = t0) : t0 = $[1];
38
- const instance = t0;
39
43
  let t1;
40
- $[2] !== instance.observable ? (t1 = (onStoreChange) => {
44
+ $[0] !== observable ? (t1 = cache.get(observable), $[0] = observable, $[1] = t1) : t1 = $[1], t0 = t1;
45
+ const instance = t0;
46
+ let t2;
47
+ $[2] !== instance.observable ? (t2 = (onStoreChange) => {
41
48
  const subscription_0 = instance.observable.subscribe(onStoreChange);
42
49
  return () => {
43
50
  subscription_0.unsubscribe();
44
51
  };
45
- }, $[2] = instance.observable, $[3] = t1) : t1 = $[3];
46
- const subscribe = t1;
47
- let t2;
48
- $[4] !== instance || $[5] !== initialValue ? (t2 = () => instance.getSnapshot(initialValue), $[4] = instance, $[5] = initialValue, $[6] = t2) : t2 = $[6];
52
+ }, $[2] = instance.observable, $[3] = t2) : t2 = $[3];
53
+ const subscribe = t2;
49
54
  let t3;
50
- return $[7] !== initialValue ? (t3 = typeof initialValue > "u" ? void 0 : () => getValue(initialValue), $[7] = initialValue, $[8] = t3) : t3 = $[8], useSyncExternalStore(subscribe, t2, t3);
51
- }
52
- function _temp4() {
53
- return timer(0, asapScheduler);
54
- }
55
- function _temp3(value_0) {
56
- }
57
- function _temp2(error) {
58
- return of({
59
- snapshot: void 0,
60
- error
61
- });
62
- }
63
- function _temp$1(value) {
64
- return {
65
- snapshot: value,
66
- error: void 0
67
- };
55
+ $[4] !== instance || $[5] !== initialValue ? (t3 = () => instance.getSnapshot(initialValue), $[4] = instance, $[5] = initialValue, $[6] = t3) : t3 = $[6];
56
+ let t4;
57
+ return $[7] !== initialValue ? (t4 = typeof initialValue > "u" ? void 0 : () => getValue(initialValue), $[7] = initialValue, $[8] = t4) : t4 = $[8], useSyncExternalStore(subscribe, t3, t4);
68
58
  }
69
59
  function useObservableEvent(handleEvent) {
70
60
  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/useObservable.ts","../src/useObservableEvent.ts"],"sourcesContent":["import {useCallback, useSyncExternalStore} from 'react'\nimport {\n asapScheduler,\n catchError,\n finalize,\n type Observable,\n type ObservedValueOf,\n of,\n share,\n timer,\n} from 'rxjs'\nimport {map, tap} from 'rxjs/operators'\n\nfunction getValue<T>(value: T): T extends () => infer U ? U : T {\n return typeof value === 'function' ? value() : value\n}\n\ninterface CacheRecord<T> {\n observable: Observable<void>\n state: {\n snapshot: T\n error?: unknown\n }\n getSnapshot: (initialValue: unknown) => T\n}\n\nconst cache = new WeakMap<Observable<any>, CacheRecord<any>>()\n\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n initialValue: ObservedValueOf<ObservableType> | (() => ObservedValueOf<ObservableType>),\n): ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n): undefined | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue: InitialValue | (() => InitialValue),\n): InitialValue | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue?: InitialValue | (() => InitialValue),\n): InitialValue | ObservedValueOf<ObservableType> {\n if (!cache.has(observable)) {\n // 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 = {\n snapshot: undefined as ObservedValueOf<ObservableType>,\n error: undefined,\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.snapshot = snapshot\n state.error = error\n }),\n // Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state\n // and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.\n map((value) => void value),\n // Ensure that the cache entry is deleted when the observable completes or errors.\n finalize(() => cache.delete(observable)),\n share({resetOnRefCountZero: () => timer(0, asapScheduler)}),\n ),\n getSnapshot: (initialValue) => {\n if (state.error) {\n throw state.error\n }\n return state.snapshot ?? (getValue(initialValue) as ObservedValueOf<ObservableType>)\n },\n }\n\n // Eagerly subscribe to sync set `state.snapshot` to what the observable returns, and keep the observable alive until the component unmounts.\n const subscription = entry.observable.subscribe()\n subscription.unsubscribe()\n\n cache.set(observable, entry as CacheRecord<ObservedValueOf<ObservableType>>)\n }\n const instance = cache.get(observable)!\n\n const subscribe = useCallback(\n (onStoreChange: () => void) => {\n const subscription = instance.observable.subscribe(onStoreChange)\n return () => {\n subscription.unsubscribe()\n }\n },\n [instance.observable],\n )\n\n return useSyncExternalStore<ObservedValueOf<ObservableType>>(\n subscribe,\n () => {\n return instance.getSnapshot(initialValue)\n },\n typeof initialValue === 'undefined'\n ? undefined\n : () => getValue(initialValue) as ObservedValueOf<ObservableType>,\n )\n}\n","import {observableCallback} from 'observable-callback'\nimport {useEffect, useState} from 'react'\nimport {type Observable} from 'rxjs'\nimport {useEffectEvent} from 'use-effect-event'\n\n/** @public */\nexport function useObservableEvent<T, U>(\n handleEvent: (arg: Observable<T>) => Observable<U>,\n): (arg: T) => void {\n const [[calls$, call]] = useState(() => observableCallback<T>())\n\n const onEvent = useEffectEvent((observable: Observable<T>) => handleEvent(observable))\n\n useEffect(() => {\n const subscription = calls$.pipe((observable) => onEvent(observable)).subscribe()\n return () => subscription.unsubscribe()\n }, [calls$, onEvent])\n\n return call\n}\n"],"names":["getValue","value","cache","WeakMap","useObservable","observable","initialValue","$","_c","has","state","snapshot","undefined","error","entry","pipe","map","_temp","catchError","_temp2","tap","t0","error_0","_temp3","finalize","delete","share","resetOnRefCountZero","_temp4","getSnapshot","initialValue_0","subscribe","unsubscribe","set","get","instance","t1","onStoreChange","subscription_0","subscription","t2","t3","useSyncExternalStore","timer","asapScheduler","value_0","of","useObservableEvent","handleEvent","useState","calls$","call","onEvent","useEffectEvent","observable_0","useEffect","observableCallback"],"mappings":";;;;;;AAaA,SAASA,SAAYC,OAA2C;AAC9D,SAAO,OAAOA,SAAU,aAAaA,MAAUA,IAAAA;AACjD;AAWA,MAAMC,4BAAYC,QAA2C;AAiBtDC,SAAAA,cAAAC,YAAAC,cAAA;AAAAC,QAAAA,IAAAC,EAAA,CAAA;AAAA,MAAA,CAIAN,MAAAO,IAAUJ,UAAU,GAAC;AAGxB,UAAAK,QAAA;AAAA,MAAAC,UAAAC;AAAAA,MAAAC,OAAAD;AAAAA,OAIAE,QAAA;AAAA,MAA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,EAAA,CAAA,IAHpBgD,UAAUf,IAGPC,EAAiB,GAEbU;AAAI;AAZN,SAAAlC,QAAA;AAAA,SAGmCuC,mBAAsB;AAAC;"}
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() : value\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 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 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 const subscription = instance.observable.subscribe(onStoreChange)\n return () => {\n subscription.unsubscribe()\n }\n },\n [instance.observable],\n )\n\n return useSyncExternalStore<ObservedValueOf<ObservableType>>(\n subscribe,\n () => {\n return instance.getSnapshot(initialValue)\n },\n typeof initialValue === 'undefined'\n ? undefined\n : () => getValue(initialValue) as ObservedValueOf<ObservableType>,\n )\n}\n","import {observableCallback} from 'observable-callback'\nimport {useEffect, useState} from 'react'\nimport {type Observable} from 'rxjs'\nimport {useEffectEvent} from 'use-effect-event'\n\n/** @public */\nexport function useObservableEvent<T, U>(\n handleEvent: (arg: Observable<T>) => Observable<U>,\n): (arg: T) => void {\n const [[calls$, call]] = useState(() => observableCallback<T>())\n\n const onEvent = useEffectEvent((observable: Observable<T>) => handleEvent(observable))\n\n useEffect(() => {\n const subscription = calls$.pipe((observable) => onEvent(observable)).subscribe()\n return () => subscription.unsubscribe()\n }, [calls$, onEvent])\n\n return call\n}\n"],"names":["getValue","value","cache","WeakMap","useObservable","observable","initialValue","$","_c","t0","has","state","didEmit","entry","pipe","map","snapshot","error","undefined","catchError","of","tap","t1","error_0","value_0","finalize","delete","share","resetOnRefCountZero","timer","asapScheduler","getSnapshot","initialValue_0","subscribe","unsubscribe","set","get","instance","t2","onStoreChange","subscription_0","subscription","t3","t4","useSyncExternalStore","useObservableEvent","handleEvent","useState","_temp","calls$","call","onEvent","useEffectEvent","observable_0","useEffect","observableCallback"],"mappings":";;;;;;AAaA,SAASA,SAAYC,OAA2C;AAC9D,SAAO,OAAOA,SAAU,aAAaA,MAAUA,IAAAA;AACjD;AAkBA,MAAMC,4BAAYC,QAA2C;AAiBtDC,SAAAA,cAAAC,YAAAC,cAAA;AAAAC,QAAAA,IAAAC,EAAA,CAAA;AAAAC,MAAAA;AAAA,MAAA,CAKEP,MAAAQ,IAAUL,UAAU,GAAC;AAGxB,UAAAM,QAAA;AAAA,MAAAC,SAAA;AAAA,OAGAC,QAAA;AAAA,MAAAF;AAAAA,MAAAN,YAEcA,WAAUS,KACpBC,IAAAd,CAAA,WAAA;AAAA,QAAAe,UAA2Bf;AAAAA,QAAKgB,OAAAC;AAAAA,MAAoB,EAAA,GACpDC,WAAAF,CAAAA,UAAsBG,GAAA;AAAA,QAAAJ,UAAAE;AAAAA,QAAAD;AAAAA,MAAAA,CAA+B,CAAC,GACtDI,IAAAC,CAAAA,QAAA;AAAK,cAAA;AAAA,UAAAN;AAAAA,UAAAC,OAAAM;AAAAA,QAAAA,IAAAD;AACHX,cAAKC,UAAA,IACLD,MAAKK,WAAYA,UACjBL,MAAKM,QAASA;AAAAA,MAAAA,CACf,GAGDF,IAAAS,CAAA,YAAA;AAAA,MAAA,CAAyB,GAEzBC,SAAevB,MAAAA,MAAAwB,OAAarB,UAAU,CAAC,GACvCsB,MAAA;AAAA,QAAAC,qBAAAA,MAAkCC,SAAAC,aAAsB;AAAA,MAAA,CAAE,CAC5D;AAAA,MAACC,aAAAC,CAAA,mBAAA;AAAA,YAEKrB,MAAKM;AAAA,gBACDN,MAAKM;AAAA,eAGXN,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AuC,aAAAvC,OAAAe,MAAAA,KAAAf,EAAA,CAAA;AAArF4C,QAAAA,UAAgBC,eAAe9B,EAAsD;AAAC,MAAAgB,IAAAI;AAAAnC,SAAAA,EAAA0C,CAAAA,MAAAA,UAAA1C,SAAA4C,WAE5Eb,KAAAA,MAAA;AACRG,UAAAA,eAAqBQ,OAAMnC,KAAAuC,CAAAA,iBAAsBF,QAAQ9C,YAAU,CAAC,EAAC4B,UAAW;AAAC,WAAA,MACpEQ,aAAYP,YAAa;AAAA,EACrCQ,GAAAA,KAAA,CAACO,QAAQE,OAAO,GAAC5C,OAAA0C,QAAA1C,OAAA4C,SAAA5C,OAAA+B,IAAA/B,OAAAmC,OAAAJ,KAAA/B,EAAA,CAAA,GAAAmC,KAAAnC,EAAA,CAAA,IAHpB+C,UAAUhB,IAGPI,EAAiB,GAEbQ;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",
3
+ "version": "4.1.4",
4
4
  "description": "React + RxJS = <3",
5
5
  "keywords": [
6
6
  "action",
@@ -75,11 +75,11 @@
75
75
  "prettier": "@sanity/prettier-config",
76
76
  "dependencies": {
77
77
  "observable-callback": "^1.0.3",
78
- "react-compiler-runtime": "19.0.0-beta-9ee70a1-20241017",
78
+ "react-compiler-runtime": "19.0.0-beta-63b359f-20241101",
79
79
  "use-effect-event": "^1.0.2"
80
80
  },
81
81
  "devDependencies": {
82
- "@sanity/pkg-utils": "^6.11.8",
82
+ "@sanity/pkg-utils": "^6.11.9",
83
83
  "@sanity/prettier-config": "^1.0.3",
84
84
  "@sanity/semantic-release-preset": "^5.0.0",
85
85
  "@testing-library/dom": "^10.4.0",
@@ -87,15 +87,15 @@
87
87
  "@types/node": "^18.17.5",
88
88
  "@types/react": "^18.3.12",
89
89
  "@types/react-dom": "^18.3.1",
90
- "@typescript-eslint/eslint-plugin": "^8.12.2",
91
- "@typescript-eslint/parser": "^8.12.2",
90
+ "@typescript-eslint/eslint-plugin": "^8.13.0",
91
+ "@typescript-eslint/parser": "^8.13.0",
92
92
  "@vitejs/plugin-react": "^4.3.3",
93
- "babel-plugin-react-compiler": "beta",
93
+ "babel-plugin-react-compiler": "19.0.0-beta-63b359f-20241101",
94
94
  "eslint": "^8.57.1",
95
95
  "eslint-config-prettier": "^9.1.0",
96
96
  "eslint-plugin-prettier": "^5.2.1",
97
97
  "eslint-plugin-react": "^7.37.2",
98
- "eslint-plugin-react-compiler": "beta",
98
+ "eslint-plugin-react-compiler": "19.0.0-beta-63b359f-20241101",
99
99
  "eslint-plugin-react-hooks": "^5.0.0",
100
100
  "eslint-plugin-simple-import-sort": "^12.1.1",
101
101
  "jsdom": "^24.1.0",
@@ -1,5 +1,5 @@
1
1
  import {act, render, renderHook} from '@testing-library/react'
2
- import {createElement, Fragment} from 'react'
2
+ import {createElement, Fragment, useMemo} from 'react'
3
3
  import {asyncScheduler, Observable, of, ReplaySubject, scheduled, share, Subject, timer} from 'rxjs'
4
4
  import {map} from 'rxjs/operators'
5
5
  import {expect, test} from 'vitest'
@@ -236,3 +236,53 @@ test('should re-subscribe when receiving a new observable', () => {
236
236
 
237
237
  unmount()
238
238
  })
239
+
240
+ test('should return undefined if observable emits undefined, also when given initial value', () => {
241
+ const values$ = new Subject<string | undefined>()
242
+ const {result, unmount} = renderHook(() => useObservable(values$, 'initial'))
243
+
244
+ expect(result.current).toBe('initial')
245
+
246
+ act(() => values$.next(undefined))
247
+
248
+ expect(result.current).toBe(undefined)
249
+
250
+ unmount()
251
+ })
252
+
253
+ test('should return undefined if observable emits undefined, also when given initial value, and also when unsubscribe + resubscribe', () => {
254
+ const snapshots: (string | undefined)[] = []
255
+ const subject = new Subject<string | undefined>()
256
+
257
+ function ObservableComponent(props: {prefix: string}) {
258
+ // will create a new observable every time prefix changes
259
+ const observable = useMemo(
260
+ () => subject.pipe(map((v) => (typeof v === 'string' ? `${props.prefix}-${v}` : v))),
261
+ [props.prefix],
262
+ )
263
+ snapshots.push(useObservable(observable, 'initial'))
264
+ return createElement(Fragment, null)
265
+ }
266
+
267
+ const {unmount, rerender} = render(createElement(ObservableComponent, {prefix: 'first'}))
268
+ act(() => subject.next('foo'))
269
+ act(() => subject.next(undefined))
270
+ act(() => subject.next('bar'))
271
+
272
+ // now change the prefix
273
+ rerender(createElement(ObservableComponent, {prefix: 'second'}))
274
+ act(() => subject.next('foo again'))
275
+ act(() => subject.next(undefined))
276
+ act(() => subject.next('bar again'))
277
+ expect(snapshots).toEqual([
278
+ 'initial',
279
+ 'first-foo',
280
+ undefined,
281
+ 'first-bar',
282
+ 'initial',
283
+ 'second-foo again',
284
+ undefined,
285
+ 'second-bar again',
286
+ ])
287
+ unmount()
288
+ })
@@ -1,4 +1,4 @@
1
- import {useCallback, useSyncExternalStore} from 'react'
1
+ import {useCallback, useMemo, useSyncExternalStore} from 'react'
2
2
  import {
3
3
  asapScheduler,
4
4
  catchError,
@@ -15,10 +15,17 @@ function getValue<T>(value: T): T extends () => infer U ? U : T {
15
15
  return typeof value === 'function' ? value() : value
16
16
  }
17
17
 
18
+ interface ObservableState<T> {
19
+ didEmit: boolean
20
+ snapshot?: T
21
+ error?: unknown
22
+ }
23
+
18
24
  interface CacheRecord<T> {
19
25
  observable: Observable<void>
20
26
  state: {
21
- snapshot: T
27
+ didEmit: boolean
28
+ snapshot?: T
22
29
  error?: unknown
23
30
  }
24
31
  getSnapshot: (initialValue: unknown) => T
@@ -45,44 +52,48 @@ export function useObservable<ObservableType extends Observable<any>, InitialVal
45
52
  observable: ObservableType,
46
53
  initialValue?: InitialValue | (() => InitialValue),
47
54
  ): InitialValue | ObservedValueOf<ObservableType> {
48
- if (!cache.has(observable)) {
49
- // This separate object is used as a stable reference to the cache entry's snapshot and error.
50
- // It's used by the `getSnapshot` closure.
51
- const state = {
52
- snapshot: undefined as ObservedValueOf<ObservableType>,
53
- error: undefined,
54
- }
55
- const entry: CacheRecord<ObservedValueOf<ObservableType>> = {
56
- state,
57
- observable: observable.pipe(
58
- map((value) => ({snapshot: value, error: undefined})),
59
- catchError((error) => of({snapshot: undefined, error})),
60
- tap(({snapshot, error}) => {
61
- state.snapshot = snapshot
62
- state.error = error
63
- }),
64
- // Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state
65
- // and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.
66
- map((value) => void value),
67
- // Ensure that the cache entry is deleted when the observable completes or errors.
68
- finalize(() => cache.delete(observable)),
69
- share({resetOnRefCountZero: () => timer(0, asapScheduler)}),
70
- ),
71
- getSnapshot: (initialValue) => {
72
- if (state.error) {
73
- throw state.error
74
- }
75
- return state.snapshot ?? (getValue(initialValue) as ObservedValueOf<ObservableType>)
76
- },
77
- }
55
+ const instance = useMemo(() => {
56
+ if (!cache.has(observable)) {
57
+ // This separate object is used as a stable reference to the cache entry's snapshot and error.
58
+ // It's used by the `getSnapshot` closure.
59
+ const state: ObservableState<ObservedValueOf<ObservableType>> = {
60
+ didEmit: false,
61
+ }
62
+ const entry: CacheRecord<ObservedValueOf<ObservableType>> = {
63
+ state,
64
+ observable: observable.pipe(
65
+ map((value) => ({snapshot: value, error: undefined})),
66
+ catchError((error) => of({snapshot: undefined, error})),
67
+ tap(({snapshot, error}) => {
68
+ state.didEmit = true
69
+ state.snapshot = snapshot
70
+ state.error = error
71
+ }),
72
+ // Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state
73
+ // and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.
74
+ map((value) => void value),
75
+ // Ensure that the cache entry is deleted when the observable completes or errors.
76
+ finalize(() => cache.delete(observable)),
77
+ share({resetOnRefCountZero: () => timer(0, asapScheduler)}),
78
+ ),
79
+ getSnapshot: (initialValue) => {
80
+ if (state.error) {
81
+ throw state.error
82
+ }
83
+ return (
84
+ state.didEmit ? state.snapshot : getValue(initialValue)
85
+ ) as ObservedValueOf<ObservableType>
86
+ },
87
+ }
78
88
 
79
- // Eagerly subscribe to sync set `state.snapshot` to what the observable returns, and keep the observable alive until the component unmounts.
80
- const subscription = entry.observable.subscribe()
81
- subscription.unsubscribe()
89
+ // Eagerly subscribe to sync set `state.snapshot` to what the observable returns, and keep the observable alive until the component unmounts.
90
+ const subscription = entry.observable.subscribe()
91
+ subscription.unsubscribe()
82
92
 
83
- cache.set(observable, entry as CacheRecord<ObservedValueOf<ObservableType>>)
84
- }
85
- const instance = cache.get(observable)!
93
+ cache.set(observable, entry)
94
+ }
95
+ return cache.get(observable)!
96
+ }, [observable])
86
97
 
87
98
  const subscribe = useCallback(
88
99
  (onStoreChange: () => void) => {