react-rx 4.1.2-canary.3 → 4.1.2-canary.5

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,8 +5,8 @@ function getValue(value) {
5
5
  return typeof value == "function" ? value() : value;
6
6
  }
7
7
  const cache = /* @__PURE__ */ new WeakMap();
8
- function useObservable(observable, initialValue) {
9
- const $ = reactCompilerRuntime.c(15);
8
+ function useObservable(observable, initialValue, debug) {
9
+ const $ = reactCompilerRuntime.c(18);
10
10
  let t0;
11
11
  if (!cache.has(observable)) {
12
12
  const entry = {};
@@ -21,7 +21,7 @@ function useObservable(observable, initialValue) {
21
21
  snapshot,
22
22
  error: error_0
23
23
  } = t12;
24
- entry.snapshot = snapshot, entry.error = error_0;
24
+ debug && console.log("tap", snapshot, error_0), entry.snapshot = snapshot, entry.error = error_0;
25
25
  }), operators.map((value_0) => {
26
26
  }), rxjs.finalize(() => cache.delete(observable)), rxjs.share({
27
27
  resetOnRefCountZero: () => rxjs.timer(0, rxjs.asapScheduler)
@@ -31,28 +31,29 @@ function useObservable(observable, initialValue) {
31
31
  $[0] !== observable ? (t1 = cache.get(observable), $[0] = observable, $[1] = t1) : t1 = $[1];
32
32
  const instance = t1;
33
33
  let t2;
34
- $[2] !== instance.observable ? (t2 = (onStoreChange) => {
34
+ $[2] !== debug || $[3] !== observable || $[4] !== instance.observable ? (t2 = (onStoreChange) => {
35
+ debug && console.log("subscribe", observable);
35
36
  const subscription_0 = instance.observable.subscribe(onStoreChange);
36
37
  return () => {
37
- subscription_0.unsubscribe();
38
+ debug && console.log("unsubscribe", observable), subscription_0.unsubscribe();
38
39
  };
39
- }, $[2] = instance.observable, $[3] = t2) : t2 = $[3];
40
+ }, $[2] = debug, $[3] = observable, $[4] = instance.observable, $[5] = t2) : t2 = $[5];
40
41
  let t3;
41
- $[4] !== instance.error || $[5] !== instance.snapshot ? (t3 = () => {
42
- if (instance.error)
42
+ $[6] !== debug || $[7] !== instance.snapshot || $[8] !== instance.error ? (t3 = () => {
43
+ if (debug && console.log("getSnapshot", instance.snapshot, instance.error), instance.error)
43
44
  throw instance.error;
44
45
  return instance.snapshot;
45
- }, $[4] = instance.error, $[5] = instance.snapshot, $[6] = t3) : t3 = $[6];
46
+ }, $[6] = debug, $[7] = instance.snapshot, $[8] = instance.error, $[9] = t3) : t3 = $[9];
46
47
  let t4;
47
- $[7] !== t2 || $[8] !== t3 ? (t4 = {
48
+ $[10] !== t2 || $[11] !== t3 ? (t4 = {
48
49
  subscribe: t2,
49
50
  getSnapshot: t3
50
- }, $[7] = t2, $[8] = t3, $[9] = t4) : t4 = $[9], t0 = t4;
51
+ }, $[10] = t2, $[11] = t3, $[12] = t4) : t4 = $[12], t0 = t4;
51
52
  const store = t0;
52
53
  let t5;
53
- $[10] !== store || $[11] !== initialValue ? (t5 = () => store.getSnapshot() ?? getValue(initialValue), $[10] = store, $[11] = initialValue, $[12] = t5) : t5 = $[12];
54
+ $[13] !== store || $[14] !== initialValue ? (t5 = () => store.getSnapshot() ?? getValue(initialValue), $[13] = store, $[14] = initialValue, $[15] = t5) : t5 = $[15];
54
55
  let t6;
55
- return $[13] !== initialValue ? (t6 = typeof initialValue > "u" ? void 0 : () => getValue(initialValue), $[13] = initialValue, $[14] = t6) : t6 = $[14], react.useSyncExternalStore(store.subscribe, t5, t6);
56
+ return $[16] !== initialValue ? (t6 = typeof initialValue > "u" ? void 0 : () => getValue(initialValue), $[16] = initialValue, $[17] = t6) : t6 = $[17], react.useSyncExternalStore(store.subscribe, t5, t6);
56
57
  }
57
58
  function useObservableEvent(handleEvent) {
58
59
  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 {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 CacheRecord<T> {\n observable: Observable<void>\n snapshot: T\n error?: unknown\n}\n\nconst cache = new WeakMap<Observable<any>, CacheRecord<any>>()\n\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n initialValue: ObservedValueOf<ObservableType> | (() => ObservedValueOf<ObservableType>),\n): ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n): undefined | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue: InitialValue | (() => InitialValue),\n): InitialValue | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue?: InitialValue | (() => InitialValue),\n): InitialValue | ObservedValueOf<ObservableType> {\n const store = useMemo(() => {\n if (!cache.has(observable)) {\n const entry: Partial<CacheRecord<ObservedValueOf<ObservableType>>> = {}\n entry.observable = observable.pipe(\n map((value) => ({snapshot: value, error: undefined})),\n catchError((error) => of({snapshot: undefined, error})),\n tap(({snapshot, error}) => {\n entry.snapshot = snapshot\n entry.error = error\n }),\n // Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state\n // and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.\n map((value) => void value),\n // Ensure that the cache entry is deleted when the observable completes or errors.\n finalize(() => cache.delete(observable)),\n share({resetOnRefCountZero: () => timer(0, asapScheduler)}),\n )\n\n // Eagerly subscribe to sync set `entry.currentValue` to what the observable returns, and keep the observable alive until the component unmounts.\n const subscription = entry.observable.subscribe()\n subscription.unsubscribe()\n\n cache.set(observable, entry as CacheRecord<ObservedValueOf<ObservableType>>)\n }\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const instance = cache.get(observable)!\n\n return {\n subscribe: (onStoreChange: () => void) => {\n const subscription = instance.observable.subscribe(onStoreChange)\n return () => {\n subscription.unsubscribe()\n }\n },\n getSnapshot: () => {\n if (instance.error) {\n throw instance.error\n }\n return instance.snapshot\n },\n }\n }, [observable])\n\n return useSyncExternalStore<ObservedValueOf<ObservableType>>(\n store.subscribe,\n () => store.getSnapshot() ?? (getValue(initialValue) as ObservedValueOf<ObservableType>),\n typeof initialValue === 'undefined'\n ? undefined\n : () => getValue(initialValue) as ObservedValueOf<ObservableType>,\n )\n}\n","import {observableCallback} from 'observable-callback'\nimport {useEffect, useState} from 'react'\nimport {type Observable} from 'rxjs'\nimport {useEffectEvent} from 'use-effect-event'\n\n/** @public */\nexport function useObservableEvent<T, U>(\n handleEvent: (arg: Observable<T>) => Observable<U>,\n): (arg: T) => void {\n const [[calls$, call]] = useState(() => observableCallback<T>())\n\n const onEvent = useEffectEvent((observable: Observable<T>) => handleEvent(observable))\n\n useEffect(() => {\n const subscription = calls$.pipe((observable) => onEvent(observable)).subscribe()\n return () => subscription.unsubscribe()\n }, [calls$, onEvent])\n\n return call\n}\n"],"names":["getValue","value","cache","WeakMap","useObservable","observable","initialValue","$","_c","t0","has","entry","pipe","map","snapshot","error","undefined","catchError","of","tap","t1","error_0","value_0","finalize","delete","share","resetOnRefCountZero","timer","asapScheduler","subscribe","unsubscribe","set","get","instance","t2","onStoreChange","subscription_0","subscription","t3","t4","getSnapshot","store","t5","t6","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;AAQA,MAAMC,4BAAYC,QAA2C;AAiBtDC,SAAAA,cAAAC,YAAAC,cAAA;AAAAC,QAAAA,IAAAC,uBAAA,EAAA;AAAAC,MAAAA;AAAA,MAAA,CAK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,EAAsD;AAAC,MAAAc,IAAAI;AAAA/B,SAAAA,EAAA0C,CAAAA,MAAAA,UAAA1C,SAAA4C,WAE5EjB,KAAAA,MAAA;AACRG,UAAAA,eAAqBY,OAAMrC,KAAAyC,CAAAA,iBAAsBF,QAAQ9C,YAAU,CAAC,EAACwB,UAAW;AAAC,WAAA,MACpEQ,aAAYP,YAAa;AAAA,EACrCQ,GAAAA,KAAA,CAACW,QAAQE,OAAO,GAAC5C,OAAA0C,QAAA1C,OAAA4C,SAAA5C,OAAA2B,IAAA3B,OAAA+B,OAAAJ,KAAA3B,EAAA,CAAA,GAAA+B,KAAA/B,EAAA,CAAA,IAHpB+C,MAAAA,UAAUpB,IAGPI,EAAiB,GAEbY;AAAI;AAZN,SAAAF,QAAA;AAAA,SAGmCO,sCAAsB;AAAC;;;"}
1
+ {"version":3,"file":"index.cjs","sources":["../src/useObservable.ts","../src/useObservableEvent.ts"],"sourcesContent":["import {useMemo, 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 debug?: boolean,\n): ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n): undefined | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue: InitialValue | (() => InitialValue),\n debug?: boolean,\n): InitialValue | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue?: InitialValue | (() => InitialValue),\n debug?: boolean,\n): InitialValue | ObservedValueOf<ObservableType> {\n const store = useMemo(() => {\n if (!cache.has(observable)) {\n const entry: Partial<CacheRecord<ObservedValueOf<ObservableType>>> = {}\n entry.observable = observable.pipe(\n map((value) => ({snapshot: value, error: undefined})),\n catchError((error) => of({snapshot: undefined, error})),\n tap(({snapshot, error}) => {\n if (debug) {\n console.log('tap', snapshot, error)\n }\n entry.snapshot = snapshot\n entry.error = error\n }),\n // Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state\n // and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.\n map((value) => void value),\n // Ensure that the cache entry is deleted when the observable completes or errors.\n finalize(() => cache.delete(observable)),\n share({resetOnRefCountZero: () => timer(0, asapScheduler)}),\n )\n\n // Eagerly subscribe to sync set `entry.currentValue` to what the observable returns, and keep the observable alive until the component unmounts.\n const subscription = entry.observable.subscribe()\n subscription.unsubscribe()\n\n cache.set(observable, entry as CacheRecord<ObservedValueOf<ObservableType>>)\n }\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const instance = cache.get(observable)!\n\n return {\n subscribe: (onStoreChange: () => void) => {\n if (debug) {\n console.log('subscribe', observable)\n }\n const subscription = instance.observable.subscribe(onStoreChange)\n return () => {\n if (debug) {\n console.log('unsubscribe', observable)\n }\n subscription.unsubscribe()\n }\n },\n getSnapshot: () => {\n if (debug) {\n console.log('getSnapshot', instance.snapshot, instance.error)\n }\n if (instance.error) {\n throw instance.error\n }\n return instance.snapshot\n },\n }\n }, [debug, observable])\n\n return useSyncExternalStore<ObservedValueOf<ObservableType>>(\n store.subscribe,\n () => store.getSnapshot() ?? (getValue(initialValue) as ObservedValueOf<ObservableType>),\n typeof initialValue === 'undefined'\n ? undefined\n : () => getValue(initialValue) as ObservedValueOf<ObservableType>,\n )\n}\n","import {observableCallback} from 'observable-callback'\nimport {useEffect, useState} from 'react'\nimport {type Observable} from 'rxjs'\nimport {useEffectEvent} from 'use-effect-event'\n\n/** @public */\nexport function useObservableEvent<T, U>(\n handleEvent: (arg: Observable<T>) => Observable<U>,\n): (arg: T) => void {\n const [[calls$, call]] = useState(() => observableCallback<T>())\n\n const onEvent = useEffectEvent((observable: Observable<T>) => handleEvent(observable))\n\n useEffect(() => {\n const subscription = calls$.pipe((observable) => onEvent(observable)).subscribe()\n return () => subscription.unsubscribe()\n }, [calls$, onEvent])\n\n return call\n}\n"],"names":["getValue","value","cache","WeakMap","useObservable","observable","initialValue","debug","$","_c","t0","has","entry","pipe","map","snapshot","error","undefined","catchError","of","tap","t1","error_0","console","log","value_0","finalize","delete","share","resetOnRefCountZero","timer","asapScheduler","subscribe","unsubscribe","set","get","instance","t2","onStoreChange","subscription_0","subscription","t3","t4","getSnapshot","store","t5","t6","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;AAQA,MAAMC,4BAAYC,QAA2C;AAmBtDC,SAAAA,cAAAC,YAAAC,cAAAC,OAAA;AAAAC,QAAAA,IAAAC,uBAAA,EAAA;AAAAC,MAAAA;AAAA,MAAA,CAMER,MAAAS,IAAUN,UAAU,GAAC;AACxB,UAAAO,QAAA,CAAA;AACAA,UAAKP,aAAcA,WAAUQ,KAC3BC,UAAAA,IAAAb,CAAA,WAAA;AAAA,MAAAc,UAA2Bd;AAAAA,MAAKe,OAAAC;AAAAA,IAAoB,EAAA,GACpDC,KAAAA,WAAAF,CAAAA,UAAsBG,KAAAA,GAAA;AAAA,MAAAJ,UAAAE;AAAAA,MAAAD;AAAAA,IAAAA,CAA+B,CAAC,GACtDI,cAAAC,CAAAA,QAAA;AAAK,YAAA;AAAA,QAAAN;AAAAA,QAAAC,OAAAM;AAAAA,MAAA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AAAA,MAAAA,UAAA5C,SAAA8C,WAE5EjB,KAAAA,MAAA;AACRG,UAAAA,eAAqBY,OAAMvC,KAAA2C,CAAAA,iBAAsBF,QAAQjD,YAAU,CAAC,EAAC2B,UAAW;AAAC,WAAA,MACpEQ,aAAYP,YAAa;AAAA,EACrCQ,GAAAA,KAAA,CAACW,QAAQE,OAAO,GAAC9C,OAAA4C,QAAA5C,OAAA8C,SAAA9C,OAAA6B,IAAA7B,OAAAiC,OAAAJ,KAAA7B,EAAA,CAAA,GAAAiC,KAAAjC,EAAA,CAAA,IAHpBiD,MAAAA,UAAUpB,IAGPI,EAAiB,GAEbY;AAAI;AAZN,SAAAF,QAAA;AAAA,SAGmCO,sCAAsB;AAAC;;;"}
package/dist/index.d.cts CHANGED
@@ -5,6 +5,7 @@ import {ObservedValueOf} from 'rxjs'
5
5
  export declare function useObservable<ObservableType extends Observable<any>>(
6
6
  observable: ObservableType,
7
7
  initialValue: ObservedValueOf<ObservableType> | (() => ObservedValueOf<ObservableType>),
8
+ debug?: boolean,
8
9
  ): ObservedValueOf<ObservableType>
9
10
 
10
11
  /** @public */
@@ -16,6 +17,7 @@ export declare function useObservable<ObservableType extends Observable<any>>(
16
17
  export declare function useObservable<ObservableType extends Observable<any>, InitialValue>(
17
18
  observable: ObservableType,
18
19
  initialValue: InitialValue | (() => InitialValue),
20
+ debug?: boolean,
19
21
  ): InitialValue | ObservedValueOf<ObservableType>
20
22
 
21
23
  /** @public */
package/dist/index.d.ts CHANGED
@@ -5,6 +5,7 @@ import {ObservedValueOf} from 'rxjs'
5
5
  export declare function useObservable<ObservableType extends Observable<any>>(
6
6
  observable: ObservableType,
7
7
  initialValue: ObservedValueOf<ObservableType> | (() => ObservedValueOf<ObservableType>),
8
+ debug?: boolean,
8
9
  ): ObservedValueOf<ObservableType>
9
10
 
10
11
  /** @public */
@@ -16,6 +17,7 @@ export declare function useObservable<ObservableType extends Observable<any>>(
16
17
  export declare function useObservable<ObservableType extends Observable<any>, InitialValue>(
17
18
  observable: ObservableType,
18
19
  initialValue: InitialValue | (() => InitialValue),
20
+ debug?: boolean,
19
21
  ): InitialValue | ObservedValueOf<ObservableType>
20
22
 
21
23
  /** @public */
package/dist/index.js CHANGED
@@ -8,8 +8,8 @@ function getValue(value) {
8
8
  return typeof value == "function" ? value() : value;
9
9
  }
10
10
  const cache = /* @__PURE__ */ new WeakMap();
11
- function useObservable(observable, initialValue) {
12
- const $ = c(15);
11
+ function useObservable(observable, initialValue, debug) {
12
+ const $ = c(18);
13
13
  let t0;
14
14
  if (!cache.has(observable)) {
15
15
  const entry = {};
@@ -24,7 +24,7 @@ function useObservable(observable, initialValue) {
24
24
  snapshot,
25
25
  error: error_0
26
26
  } = t12;
27
- entry.snapshot = snapshot, entry.error = error_0;
27
+ debug && console.log("tap", snapshot, error_0), entry.snapshot = snapshot, entry.error = error_0;
28
28
  }), map((value_0) => {
29
29
  }), finalize(() => cache.delete(observable)), share({
30
30
  resetOnRefCountZero: () => timer(0, asapScheduler)
@@ -34,28 +34,29 @@ function useObservable(observable, initialValue) {
34
34
  $[0] !== observable ? (t1 = cache.get(observable), $[0] = observable, $[1] = t1) : t1 = $[1];
35
35
  const instance = t1;
36
36
  let t2;
37
- $[2] !== instance.observable ? (t2 = (onStoreChange) => {
37
+ $[2] !== debug || $[3] !== observable || $[4] !== instance.observable ? (t2 = (onStoreChange) => {
38
+ debug && console.log("subscribe", observable);
38
39
  const subscription_0 = instance.observable.subscribe(onStoreChange);
39
40
  return () => {
40
- subscription_0.unsubscribe();
41
+ debug && console.log("unsubscribe", observable), subscription_0.unsubscribe();
41
42
  };
42
- }, $[2] = instance.observable, $[3] = t2) : t2 = $[3];
43
+ }, $[2] = debug, $[3] = observable, $[4] = instance.observable, $[5] = t2) : t2 = $[5];
43
44
  let t3;
44
- $[4] !== instance.error || $[5] !== instance.snapshot ? (t3 = () => {
45
- if (instance.error)
45
+ $[6] !== debug || $[7] !== instance.snapshot || $[8] !== instance.error ? (t3 = () => {
46
+ if (debug && console.log("getSnapshot", instance.snapshot, instance.error), instance.error)
46
47
  throw instance.error;
47
48
  return instance.snapshot;
48
- }, $[4] = instance.error, $[5] = instance.snapshot, $[6] = t3) : t3 = $[6];
49
+ }, $[6] = debug, $[7] = instance.snapshot, $[8] = instance.error, $[9] = t3) : t3 = $[9];
49
50
  let t4;
50
- $[7] !== t2 || $[8] !== t3 ? (t4 = {
51
+ $[10] !== t2 || $[11] !== t3 ? (t4 = {
51
52
  subscribe: t2,
52
53
  getSnapshot: t3
53
- }, $[7] = t2, $[8] = t3, $[9] = t4) : t4 = $[9], t0 = t4;
54
+ }, $[10] = t2, $[11] = t3, $[12] = t4) : t4 = $[12], t0 = t4;
54
55
  const store = t0;
55
56
  let t5;
56
- $[10] !== store || $[11] !== initialValue ? (t5 = () => store.getSnapshot() ?? getValue(initialValue), $[10] = store, $[11] = initialValue, $[12] = t5) : t5 = $[12];
57
+ $[13] !== store || $[14] !== initialValue ? (t5 = () => store.getSnapshot() ?? getValue(initialValue), $[13] = store, $[14] = initialValue, $[15] = t5) : t5 = $[15];
57
58
  let t6;
58
- return $[13] !== initialValue ? (t6 = typeof initialValue > "u" ? void 0 : () => getValue(initialValue), $[13] = initialValue, $[14] = t6) : t6 = $[14], useSyncExternalStore(store.subscribe, t5, t6);
59
+ return $[16] !== initialValue ? (t6 = typeof initialValue > "u" ? void 0 : () => getValue(initialValue), $[16] = initialValue, $[17] = t6) : t6 = $[17], useSyncExternalStore(store.subscribe, t5, t6);
59
60
  }
60
61
  function useObservableEvent(handleEvent) {
61
62
  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 {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 CacheRecord<T> {\n observable: Observable<void>\n snapshot: T\n error?: unknown\n}\n\nconst cache = new WeakMap<Observable<any>, CacheRecord<any>>()\n\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n initialValue: ObservedValueOf<ObservableType> | (() => ObservedValueOf<ObservableType>),\n): ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n): undefined | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue: InitialValue | (() => InitialValue),\n): InitialValue | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue?: InitialValue | (() => InitialValue),\n): InitialValue | ObservedValueOf<ObservableType> {\n const store = useMemo(() => {\n if (!cache.has(observable)) {\n const entry: Partial<CacheRecord<ObservedValueOf<ObservableType>>> = {}\n entry.observable = observable.pipe(\n map((value) => ({snapshot: value, error: undefined})),\n catchError((error) => of({snapshot: undefined, error})),\n tap(({snapshot, error}) => {\n entry.snapshot = snapshot\n entry.error = error\n }),\n // Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state\n // and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.\n map((value) => void value),\n // Ensure that the cache entry is deleted when the observable completes or errors.\n finalize(() => cache.delete(observable)),\n share({resetOnRefCountZero: () => timer(0, asapScheduler)}),\n )\n\n // Eagerly subscribe to sync set `entry.currentValue` to what the observable returns, and keep the observable alive until the component unmounts.\n const subscription = entry.observable.subscribe()\n subscription.unsubscribe()\n\n cache.set(observable, entry as CacheRecord<ObservedValueOf<ObservableType>>)\n }\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const instance = cache.get(observable)!\n\n return {\n subscribe: (onStoreChange: () => void) => {\n const subscription = instance.observable.subscribe(onStoreChange)\n return () => {\n subscription.unsubscribe()\n }\n },\n getSnapshot: () => {\n if (instance.error) {\n throw instance.error\n }\n return instance.snapshot\n },\n }\n }, [observable])\n\n return useSyncExternalStore<ObservedValueOf<ObservableType>>(\n store.subscribe,\n () => store.getSnapshot() ?? (getValue(initialValue) as ObservedValueOf<ObservableType>),\n typeof initialValue === 'undefined'\n ? undefined\n : () => getValue(initialValue) as ObservedValueOf<ObservableType>,\n )\n}\n","import {observableCallback} from 'observable-callback'\nimport {useEffect, useState} from 'react'\nimport {type Observable} from 'rxjs'\nimport {useEffectEvent} from 'use-effect-event'\n\n/** @public */\nexport function useObservableEvent<T, U>(\n handleEvent: (arg: Observable<T>) => Observable<U>,\n): (arg: T) => void {\n const [[calls$, call]] = useState(() => observableCallback<T>())\n\n const onEvent = useEffectEvent((observable: Observable<T>) => handleEvent(observable))\n\n useEffect(() => {\n const subscription = calls$.pipe((observable) => onEvent(observable)).subscribe()\n return () => subscription.unsubscribe()\n }, [calls$, onEvent])\n\n return call\n}\n"],"names":["getValue","value","cache","WeakMap","useObservable","observable","initialValue","$","_c","t0","has","entry","pipe","map","snapshot","error","undefined","catchError","of","tap","t1","error_0","value_0","finalize","delete","share","resetOnRefCountZero","timer","asapScheduler","subscribe","unsubscribe","set","get","instance","t2","onStoreChange","subscription_0","subscription","t3","t4","getSnapshot","store","t5","t6","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;AAQA,MAAMC,4BAAYC,QAA2C;AAiBtDC,SAAAA,cAAAC,YAAAC,cAAA;AAAAC,QAAAA,IAAAC,EAAA,EAAA;AAAAC,MAAAA;AAAA,MAAA,CAKEP,MAAAQ,IAAUL,UAAU,GAAC;AACxB,UAAAM,QAAA,C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G,UAAAA,eAAqBY,OAAMrC,KAAAyC,CAAAA,iBAAsBF,QAAQ9C,YAAU,CAAC,EAACwB,UAAW;AAAC,WAAA,MACpEQ,aAAYP,YAAa;AAAA,EACrCQ,GAAAA,KAAA,CAACW,QAAQE,OAAO,GAAC5C,OAAA0C,QAAA1C,OAAA4C,SAAA5C,OAAA2B,IAAA3B,OAAA+B,OAAAJ,KAAA3B,EAAA,CAAA,GAAA+B,KAAA/B,EAAA,CAAA,IAHpB+C,UAAUpB,IAGPI,EAAiB,GAEbY;AAAI;AAZN,SAAAF,QAAA;AAAA,SAGmCO,mBAAsB;AAAC;"}
1
+ {"version":3,"file":"index.js","sources":["../src/useObservable.ts","../src/useObservableEvent.ts"],"sourcesContent":["import {useMemo, 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 debug?: boolean,\n): ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n): undefined | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue: InitialValue | (() => InitialValue),\n debug?: boolean,\n): InitialValue | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue?: InitialValue | (() => InitialValue),\n debug?: boolean,\n): InitialValue | ObservedValueOf<ObservableType> {\n const store = useMemo(() => {\n if (!cache.has(observable)) {\n const entry: Partial<CacheRecord<ObservedValueOf<ObservableType>>> = {}\n entry.observable = observable.pipe(\n map((value) => ({snapshot: value, error: undefined})),\n catchError((error) => of({snapshot: undefined, error})),\n tap(({snapshot, error}) => {\n if (debug) {\n console.log('tap', snapshot, error)\n }\n entry.snapshot = snapshot\n entry.error = error\n }),\n // Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state\n // and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.\n map((value) => void value),\n // Ensure that the cache entry is deleted when the observable completes or errors.\n finalize(() => cache.delete(observable)),\n share({resetOnRefCountZero: () => timer(0, asapScheduler)}),\n )\n\n // Eagerly subscribe to sync set `entry.currentValue` to what the observable returns, and keep the observable alive until the component unmounts.\n const subscription = entry.observable.subscribe()\n subscription.unsubscribe()\n\n cache.set(observable, entry as CacheRecord<ObservedValueOf<ObservableType>>)\n }\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const instance = cache.get(observable)!\n\n return {\n subscribe: (onStoreChange: () => void) => {\n if (debug) {\n console.log('subscribe', observable)\n }\n const subscription = instance.observable.subscribe(onStoreChange)\n return () => {\n if (debug) {\n console.log('unsubscribe', observable)\n }\n subscription.unsubscribe()\n }\n },\n getSnapshot: () => {\n if (debug) {\n console.log('getSnapshot', instance.snapshot, instance.error)\n }\n if (instance.error) {\n throw instance.error\n }\n return instance.snapshot\n },\n }\n }, [debug, observable])\n\n return useSyncExternalStore<ObservedValueOf<ObservableType>>(\n store.subscribe,\n () => store.getSnapshot() ?? (getValue(initialValue) as ObservedValueOf<ObservableType>),\n typeof initialValue === 'undefined'\n ? undefined\n : () => getValue(initialValue) as ObservedValueOf<ObservableType>,\n )\n}\n","import {observableCallback} from 'observable-callback'\nimport {useEffect, useState} from 'react'\nimport {type Observable} from 'rxjs'\nimport {useEffectEvent} from 'use-effect-event'\n\n/** @public */\nexport function useObservableEvent<T, U>(\n handleEvent: (arg: Observable<T>) => Observable<U>,\n): (arg: T) => void {\n const [[calls$, call]] = useState(() => observableCallback<T>())\n\n const onEvent = useEffectEvent((observable: Observable<T>) => handleEvent(observable))\n\n useEffect(() => {\n const subscription = calls$.pipe((observable) => onEvent(observable)).subscribe()\n return () => subscription.unsubscribe()\n }, [calls$, onEvent])\n\n return call\n}\n"],"names":["getValue","value","cache","WeakMap","useObservable","observable","initialValue","debug","$","_c","t0","has","entry","pipe","map","snapshot","error","undefined","catchError","of","tap","t1","error_0","console","log","value_0","finalize","delete","share","resetOnRefCountZero","timer","asapScheduler","subscribe","unsubscribe","set","get","instance","t2","onStoreChange","subscription_0","subscription","t3","t4","getSnapshot","store","t5","t6","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;AAQA,MAAMC,4BAAYC,QAA2C;AAmBtDC,SAAAA,cAAAC,YAAAC,cAAAC,OAAA;AAAAC,QAAAA,IAAAC,EAAA,EAAA;AAAAC,MAAAA;AAAA,MAAA,CAMER,MAAAS,IAAUN,UAAU,GAAC;AACxB,UAAAO,QAAA,CAAA;AACAA,UAAKP,aAAcA,WAAUQ,KAC3BC,IAAAb,CAAA,WAAA;AAAA,MAAAc,UAA2Bd;AAAAA,MAAKe,OAAAC;AAAAA,IAAoB,EAAA,GACpDC,WAAAF,CAAAA,UAAsBG,GAAA;AAAA,MAAAJ,UAAAE;AAAAA,MAAAD;AAAAA,IAAAA,CAA+B,CAAC,GACtDI,IAAAC,CAAAA,QAAA;AAAK,YAAA;AAAA,QAAAN;AAAAA,QAAAC,OAAAM;AAAAA,MAAAA,IAAAD;AACCd,eACFgB,QAAAC,IAAY,OAAOT,UAAUC,OAAK,GAEpCJ,MAAKG,WAAYA,UACjBH,MAAKI,QAASA;AAAAA,IAAAA,CACf,GAGDF,IAAAW,CAAA,Y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AA,EACrCQ,GAAAA,KAAA,CAACW,QAAQE,OAAO,GAAC9C,OAAA4C,QAAA5C,OAAA8C,SAAA9C,OAAA6B,IAAA7B,OAAAiC,OAAAJ,KAAA7B,EAAA,CAAA,GAAAiC,KAAAjC,EAAA,CAAA,IAHpBiD,UAAUpB,IAGPI,EAAiB,GAEbY;AAAI;AAZN,SAAAF,QAAA;AAAA,SAGmCO,mBAAsB;AAAC;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-rx",
3
- "version": "4.1.2-canary.3",
3
+ "version": "4.1.2-canary.5",
4
4
  "description": "React + RxJS = <3",
5
5
  "keywords": [
6
6
  "action",
@@ -27,6 +27,7 @@ const cache = new WeakMap<Observable<any>, CacheRecord<any>>()
27
27
  export function useObservable<ObservableType extends Observable<any>>(
28
28
  observable: ObservableType,
29
29
  initialValue: ObservedValueOf<ObservableType> | (() => ObservedValueOf<ObservableType>),
30
+ debug?: boolean,
30
31
  ): ObservedValueOf<ObservableType>
31
32
  /** @public */
32
33
  export function useObservable<ObservableType extends Observable<any>>(
@@ -36,11 +37,13 @@ export function useObservable<ObservableType extends Observable<any>>(
36
37
  export function useObservable<ObservableType extends Observable<any>, InitialValue>(
37
38
  observable: ObservableType,
38
39
  initialValue: InitialValue | (() => InitialValue),
40
+ debug?: boolean,
39
41
  ): InitialValue | ObservedValueOf<ObservableType>
40
42
  /** @public */
41
43
  export function useObservable<ObservableType extends Observable<any>, InitialValue>(
42
44
  observable: ObservableType,
43
45
  initialValue?: InitialValue | (() => InitialValue),
46
+ debug?: boolean,
44
47
  ): InitialValue | ObservedValueOf<ObservableType> {
45
48
  const store = useMemo(() => {
46
49
  if (!cache.has(observable)) {
@@ -49,6 +52,9 @@ export function useObservable<ObservableType extends Observable<any>, InitialVal
49
52
  map((value) => ({snapshot: value, error: undefined})),
50
53
  catchError((error) => of({snapshot: undefined, error})),
51
54
  tap(({snapshot, error}) => {
55
+ if (debug) {
56
+ console.log('tap', snapshot, error)
57
+ }
52
58
  entry.snapshot = snapshot
53
59
  entry.error = error
54
60
  }),
@@ -71,19 +77,28 @@ export function useObservable<ObservableType extends Observable<any>, InitialVal
71
77
 
72
78
  return {
73
79
  subscribe: (onStoreChange: () => void) => {
80
+ if (debug) {
81
+ console.log('subscribe', observable)
82
+ }
74
83
  const subscription = instance.observable.subscribe(onStoreChange)
75
84
  return () => {
85
+ if (debug) {
86
+ console.log('unsubscribe', observable)
87
+ }
76
88
  subscription.unsubscribe()
77
89
  }
78
90
  },
79
91
  getSnapshot: () => {
92
+ if (debug) {
93
+ console.log('getSnapshot', instance.snapshot, instance.error)
94
+ }
80
95
  if (instance.error) {
81
96
  throw instance.error
82
97
  }
83
98
  return instance.snapshot
84
99
  },
85
100
  }
86
- }, [observable])
101
+ }, [debug, observable])
87
102
 
88
103
  return useSyncExternalStore<ObservedValueOf<ObservableType>>(
89
104
  store.subscribe,