react-rx 4.1.2-canary.1 → 4.1.2-canary.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1,62 +1,81 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: !0 });
3
- var react = require("react"), rxjs = require("rxjs"), operators = require("rxjs/operators"), observableCallback = require("observable-callback"), useEffectEvent = require("use-effect-event");
3
+ var reactCompilerRuntime = require("react-compiler-runtime"), react = require("react"), rxjs = require("rxjs"), operators = require("rxjs/operators"), observableCallback = require("observable-callback"), useEffectEvent = require("use-effect-event");
4
4
  function getValue(value) {
5
5
  return typeof value == "function" ? value() : value;
6
6
  }
7
7
  const cache = /* @__PURE__ */ new WeakMap();
8
8
  function useObservable(observable, initialValue) {
9
- const [getInitialValue] = react.useState(
10
- () => () => getValue(initialValue)
11
- ), store = react.useMemo(() => {
12
- if (!cache.has(observable)) {
13
- const entry = {
14
- snapshot: getInitialValue()
15
- };
16
- entry.observable = observable.pipe(
17
- operators.map((value) => ({ snapshot: value, error: void 0 })),
18
- rxjs.catchError((error) => rxjs.of({ snapshot: void 0, error })),
19
- operators.tap(({ snapshot, error }) => {
20
- entry.snapshot = snapshot, entry.error = error;
21
- }),
22
- // Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state
23
- // and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.
24
- operators.map((value) => {
25
- }),
26
- // Ensure that the cache entry is deleted when the observable completes or errors.
27
- rxjs.finalize(() => cache.delete(observable)),
28
- rxjs.share({ resetOnRefCountZero: () => rxjs.timer(0, rxjs.asapScheduler) })
29
- ), entry.observable.subscribe().unsubscribe(), cache.set(observable, entry);
30
- }
31
- const instance = cache.get(observable);
32
- return {
33
- subscribe: (onStoreChange) => {
34
- const subscription = instance.observable.subscribe(onStoreChange);
35
- return () => {
36
- subscription.unsubscribe();
37
- };
38
- },
9
+ const $ = reactCompilerRuntime.c(9);
10
+ if (!cache.has(observable)) {
11
+ const state = {
12
+ snapshot: void 0,
13
+ error: void 0
14
+ }, entry = {
15
+ state,
16
+ observable: observable.pipe(operators.map(_temp$1), rxjs.catchError(_temp2), operators.tap((t02) => {
17
+ const {
18
+ snapshot,
19
+ 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
24
+ })),
39
25
  getSnapshot: () => {
40
- if (instance.error)
41
- throw instance.error;
42
- return instance.snapshot;
26
+ if (state.error)
27
+ throw state.error;
28
+ return state.snapshot;
43
29
  }
44
30
  };
45
- }, [observable, getInitialValue]);
46
- return react.useEffect(() => {
47
- console.log("stor.subscribe changed", store.subscribe);
48
- }, [store.subscribe]), react.useSyncExternalStore(
49
- store.subscribe,
50
- store.getSnapshot,
51
- typeof initialValue > "u" ? void 0 : () => getInitialValue()
52
- );
31
+ entry.observable.subscribe().unsubscribe(), cache.set(observable, entry);
32
+ }
33
+ let t0;
34
+ $[0] !== observable ? (t0 = cache.get(observable), $[0] = observable, $[1] = t0) : t0 = $[1];
35
+ const instance = t0;
36
+ let t1;
37
+ $[2] !== instance.observable ? (t1 = (onStoreChange) => {
38
+ const subscription_0 = instance.observable.subscribe(onStoreChange);
39
+ return () => {
40
+ subscription_0.unsubscribe();
41
+ };
42
+ }, $[2] = instance.observable, $[3] = t1) : t1 = $[3];
43
+ const subscribe = t1;
44
+ let t2;
45
+ $[4] !== instance || $[5] !== initialValue ? (t2 = () => instance.getSnapshot() ?? getValue(initialValue), $[4] = instance, $[5] = initialValue, $[6] = t2) : t2 = $[6];
46
+ 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
+ };
53
65
  }
54
66
  function useObservableEvent(handleEvent) {
55
- const [[calls$, call]] = react.useState(() => observableCallback.observableCallback()), onEvent = useEffectEvent.useEffectEvent((observable) => handleEvent(observable));
56
- return react.useEffect(() => {
57
- const subscription = calls$.pipe((observable) => onEvent(observable)).subscribe();
67
+ const $ = reactCompilerRuntime.c(6), [t0] = react.useState(_temp), [calls$, call] = t0;
68
+ let t1;
69
+ $[0] !== handleEvent ? (t1 = (observable) => handleEvent(observable), $[0] = handleEvent, $[1] = t1) : t1 = $[1];
70
+ const onEvent = useEffectEvent.useEffectEvent(t1);
71
+ let t2, t3;
72
+ return $[2] !== calls$ || $[3] !== onEvent ? (t2 = () => {
73
+ const subscription = calls$.pipe((observable_0) => onEvent(observable_0)).subscribe();
58
74
  return () => subscription.unsubscribe();
59
- }, [calls$, onEvent]), call;
75
+ }, t3 = [calls$, onEvent], $[2] = calls$, $[3] = onEvent, $[4] = t2, $[5] = t3) : (t2 = $[4], t3 = $[5]), react.useEffect(t2, t3), call;
76
+ }
77
+ function _temp() {
78
+ return observableCallback.observableCallback();
60
79
  }
61
80
  exports.useObservable = useObservable;
62
81
  exports.useObservableEvent = useObservableEvent;
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../src/useObservable.ts","../src/useObservableEvent.ts"],"sourcesContent":["import {useEffect, useMemo, useState, useSyncExternalStore} from 'react'\nimport {\n asapScheduler,\n catchError,\n finalize,\n type Observable,\n type ObservedValueOf,\n of,\n share,\n timer,\n} from 'rxjs'\nimport {map, tap} from 'rxjs/operators'\n\nfunction getValue<T>(value: T): T extends () => infer U ? U : T {\n return typeof value === 'function' ? value() : value\n}\n\ninterface CacheRecord<T> {\n observable: Observable<void>\n snapshot: T\n error?: unknown\n}\n\nconst cache = new WeakMap<Observable<any>, CacheRecord<any>>()\n\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n initialValue: ObservedValueOf<ObservableType> | (() => ObservedValueOf<ObservableType>),\n): ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n): undefined | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue: InitialValue | (() => InitialValue),\n): InitialValue | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue?: InitialValue | (() => InitialValue),\n): InitialValue | ObservedValueOf<ObservableType> {\n const [getInitialValue] = useState(\n () => () => getValue(initialValue) as ObservedValueOf<ObservableType>,\n )\n\n const store = useMemo(() => {\n if (!cache.has(observable)) {\n const entry: Partial<CacheRecord<ObservedValueOf<ObservableType>>> = {\n snapshot: getInitialValue(),\n }\n entry.observable = observable.pipe(\n map((value) => ({snapshot: value, error: undefined})),\n catchError((error) => of({snapshot: undefined, error})),\n tap(({snapshot, error}) => {\n entry.snapshot = snapshot\n entry.error = error\n }),\n // Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state\n // and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.\n map((value) => void value),\n // Ensure that the cache entry is deleted when the observable completes or errors.\n finalize(() => cache.delete(observable)),\n share({resetOnRefCountZero: () => timer(0, asapScheduler)}),\n )\n\n // Eagerly subscribe to sync set `entry.currentValue` to what the observable returns, and keep the observable alive until the component unmounts.\n const subscription = entry.observable.subscribe()\n subscription.unsubscribe()\n\n cache.set(observable, entry as CacheRecord<ObservedValueOf<ObservableType>>)\n }\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const instance = cache.get(observable)!\n\n return {\n subscribe: (onStoreChange: () => void) => {\n const subscription = instance.observable.subscribe(onStoreChange)\n return () => {\n subscription.unsubscribe()\n }\n },\n getSnapshot: () => {\n if (instance.error) {\n throw instance.error\n }\n return instance.snapshot\n },\n }\n }, [observable, getInitialValue])\n\n useEffect(() => {\n console.log('stor.subscribe changed', store.subscribe)\n }, [store.subscribe])\n\n return useSyncExternalStore<ObservedValueOf<ObservableType>>(\n store.subscribe,\n store.getSnapshot,\n typeof initialValue === 'undefined' ? undefined : () => getInitialValue(),\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":["useState","useMemo","map","catchError","of","tap","finalize","share","timer","asapScheduler","useEffect","useSyncExternalStore","observableCallback","useEffectEvent"],"mappings":";;;AAaA,SAAS,SAAY,OAA2C;AAC9D,SAAO,OAAO,SAAU,aAAa,MAAU,IAAA;AACjD;AAQA,MAAM,4BAAY,QAA2C;AAiB7C,SAAA,cACd,YACA,cACgD;AAC1C,QAAA,CAAC,eAAe,IAAIA,MAAA;AAAA,IACxB,MAAM,MAAM,SAAS,YAAY;AAAA,EAAA,GAG7B,QAAQC,MAAAA,QAAQ,MAAM;AAC1B,QAAI,CAAC,MAAM,IAAI,UAAU,GAAG;AAC1B,YAAM,QAA+D;AAAA,QACnE,UAAU,gBAAgB;AAAA,MAC5B;AACA,YAAM,aAAa,WAAW;AAAA,QAC5BC,cAAI,CAAC,WAAW,EAAC,UAAU,OAAO,OAAO,SAAW;AAAA,QACpDC,gBAAW,CAAC,UAAUC,QAAG,EAAC,UAAU,QA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,QAAQ,UAAU,CAAC,EAAE,UAAU;AACzE,WAAA,MAAM,aAAa,YAAY;AAAA,EACrC,GAAA,CAAC,QAAQ,OAAO,CAAC,GAEb;AACT;;;"}
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: () => 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 const state = {\n snapshot: undefined as ObservedValueOf<ObservableType>,\n error: undefined,\n }\n const entry: CacheRecord<ObservedValueOf<ObservableType>> = {\n // snapshot: getValue(initialValue) as 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: () => {\n if (state.error) {\n throw state.error\n }\n return state.snapshot\n },\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 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() ?? (getValue(initialValue) as ObservedValueOf<ObservableType>)\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","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,uBAAA,CAAA;AAAA,MAAA,CAIAN,MAAAO,IAAUJ,UAAU,GAAC;AACxB,UAAAK,QAAA;AAAA,MAAAC,UAAAC;AAAAA,MAAAC,OAAAD;AAAAA,OAIAE,QAAA;AAAA,MAAAJ;AAAAA,MAAAL,YAGcA,WAAUU,KACpBC,UAAAA,IAAAC,OAAoD,GACpDC,KAAAA,WAAAC,MAAsD,GACtDC,cAAAC,CAAAA,QAAA;AAAK,cAAA;AAAA,UAAAV;AAAAA,UAAAE,OAAAS;AAAAA,QAAAA,IAAAD;AACEV,cAAAA,WAAYA,UACjBD,MAAKG,QAASA;AAAAA,MAAAA,CACf,GAGDG,UAAAA,IAAAO,MAAyB,GAEzBC,KAAAA,SAAetB,MAAAA,MAAAuB,OAAapB,UAAU,CAAC,GACvCqB,WAAA;AAAA,QAAAC,qBAAAC;AAAAA,MAAAA,CAA0D,CAC5D;AAAA,MAACC,aAAAA,MAAA;AAAA,YAEKnB,MAAKG;AAAA,gBACDH,MAAKG;AAAA,eAENH,MAAKC;AAAAA,MAAAA;AAAAA,IAAA;AAKKG,UAAKT,WAAAyB,YACdC,YAEZ7B,GAAAA,MAAA8B,IAAU3B,YAAYS,KAAqD;AAAA,EAAA;AAACO,MAAAA;AAAAd,WAAAF,cAE7DgB,KAAAnB,MAAA+B,IAAU5B,UAAU,GAACE,OAAAF,YAAAE,OAAAc,MAAAA,KAAAd,EAAA,CAAA;AAAtC,QAAA2B,WAAiBb;AAAsBc,MAAAA;AAAA5B,IAAA,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C,YAAY1C,UAAU,GAACE,OAAAwC,aAAAxC,OAAA4B,MAAAA,KAAA5B,EAAA,CAAA;AAArF4C,QAAAA,UAAgBC,8BAAejB,EAAsD;AAAC,MAAAI,IAAAC;AAAAjC,SAAAA,EAAA0C,CAAAA,MAAAA,UAAA1C,SAAA4C,WAE5EZ,KAAAA,MAAA;AACRD,UAAAA,eAAqBW,OAAMlC,KAAAsC,CAAAA,iBAAsBF,QAAQ9C,YAAU,CAAC,EAACyB,UAAW;AAAC,WAAA,MACpEQ,aAAYP,YAAa;AAAA,EACrCS,GAAAA,KAAA,CAACS,QAAQE,OAAO,GAAC5C,OAAA0C,QAAA1C,OAAA4C,SAAA5C,OAAAgC,IAAAhC,OAAAiC,OAAAD,KAAAhC,EAAA,CAAA,GAAAiC,KAAAjC,EAAA,CAAA,IAHpB+C,MAAAA,UAAUf,IAGPC,EAAiB,GAEbU;AAAI;AAZN,SAAAjC,QAAA;AAAA,SAGmCsC,sCAAsB;AAAC;;;"}
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
- import { useState, useMemo, useEffect, useSyncExternalStore } from "react";
2
- import { catchError, of, finalize, share, timer, asapScheduler } from "rxjs";
1
+ import { c } from "react-compiler-runtime";
2
+ import { useSyncExternalStore, useState, useEffect } from "react";
3
+ import { catchError, finalize, share, timer, asapScheduler, of } from "rxjs";
3
4
  import { map, tap } from "rxjs/operators";
4
5
  import { observableCallback } from "observable-callback";
5
6
  import { useEffectEvent } from "use-effect-event";
@@ -8,57 +9,76 @@ function getValue(value) {
8
9
  }
9
10
  const cache = /* @__PURE__ */ new WeakMap();
10
11
  function useObservable(observable, initialValue) {
11
- const [getInitialValue] = useState(
12
- () => () => getValue(initialValue)
13
- ), store = useMemo(() => {
14
- if (!cache.has(observable)) {
15
- const entry = {
16
- snapshot: getInitialValue()
17
- };
18
- entry.observable = observable.pipe(
19
- map((value) => ({ snapshot: value, error: void 0 })),
20
- catchError((error) => of({ snapshot: void 0, error })),
21
- tap(({ snapshot, error }) => {
22
- entry.snapshot = snapshot, entry.error = error;
23
- }),
24
- // Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state
25
- // and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.
26
- map((value) => {
27
- }),
28
- // Ensure that the cache entry is deleted when the observable completes or errors.
29
- finalize(() => cache.delete(observable)),
30
- share({ resetOnRefCountZero: () => timer(0, asapScheduler) })
31
- ), entry.observable.subscribe().unsubscribe(), cache.set(observable, entry);
32
- }
33
- const instance = cache.get(observable);
34
- return {
35
- subscribe: (onStoreChange) => {
36
- const subscription = instance.observable.subscribe(onStoreChange);
37
- return () => {
38
- subscription.unsubscribe();
39
- };
40
- },
12
+ const $ = c(9);
13
+ if (!cache.has(observable)) {
14
+ const state = {
15
+ snapshot: void 0,
16
+ error: void 0
17
+ }, entry = {
18
+ state,
19
+ observable: observable.pipe(map(_temp$1), catchError(_temp2), tap((t02) => {
20
+ const {
21
+ snapshot,
22
+ error: error_0
23
+ } = t02;
24
+ state.snapshot = snapshot, state.error = error_0;
25
+ }), map(_temp3), finalize(() => cache.delete(observable)), share({
26
+ resetOnRefCountZero: _temp4
27
+ })),
41
28
  getSnapshot: () => {
42
- if (instance.error)
43
- throw instance.error;
44
- return instance.snapshot;
29
+ if (state.error)
30
+ throw state.error;
31
+ return state.snapshot;
45
32
  }
46
33
  };
47
- }, [observable, getInitialValue]);
48
- return useEffect(() => {
49
- console.log("stor.subscribe changed", store.subscribe);
50
- }, [store.subscribe]), useSyncExternalStore(
51
- store.subscribe,
52
- store.getSnapshot,
53
- typeof initialValue > "u" ? void 0 : () => getInitialValue()
54
- );
34
+ entry.observable.subscribe().unsubscribe(), cache.set(observable, entry);
35
+ }
36
+ let t0;
37
+ $[0] !== observable ? (t0 = cache.get(observable), $[0] = observable, $[1] = t0) : t0 = $[1];
38
+ const instance = t0;
39
+ let t1;
40
+ $[2] !== instance.observable ? (t1 = (onStoreChange) => {
41
+ const subscription_0 = instance.observable.subscribe(onStoreChange);
42
+ return () => {
43
+ subscription_0.unsubscribe();
44
+ };
45
+ }, $[2] = instance.observable, $[3] = t1) : t1 = $[3];
46
+ const subscribe = t1;
47
+ let t2;
48
+ $[4] !== instance || $[5] !== initialValue ? (t2 = () => instance.getSnapshot() ?? getValue(initialValue), $[4] = instance, $[5] = initialValue, $[6] = t2) : t2 = $[6];
49
+ 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
68
  }
56
69
  function useObservableEvent(handleEvent) {
57
- const [[calls$, call]] = useState(() => observableCallback()), onEvent = useEffectEvent((observable) => handleEvent(observable));
58
- return useEffect(() => {
59
- const subscription = calls$.pipe((observable) => onEvent(observable)).subscribe();
70
+ const $ = c(6), [t0] = useState(_temp), [calls$, call] = t0;
71
+ let t1;
72
+ $[0] !== handleEvent ? (t1 = (observable) => handleEvent(observable), $[0] = handleEvent, $[1] = t1) : t1 = $[1];
73
+ const onEvent = useEffectEvent(t1);
74
+ let t2, t3;
75
+ return $[2] !== calls$ || $[3] !== onEvent ? (t2 = () => {
76
+ const subscription = calls$.pipe((observable_0) => onEvent(observable_0)).subscribe();
60
77
  return () => subscription.unsubscribe();
61
- }, [calls$, onEvent]), call;
78
+ }, t3 = [calls$, onEvent], $[2] = calls$, $[3] = onEvent, $[4] = t2, $[5] = t3) : (t2 = $[4], t3 = $[5]), useEffect(t2, t3), call;
79
+ }
80
+ function _temp() {
81
+ return observableCallback();
62
82
  }
63
83
  export {
64
84
  useObservable,
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/useObservable.ts","../src/useObservableEvent.ts"],"sourcesContent":["import {useEffect, useMemo, useState, useSyncExternalStore} from 'react'\nimport {\n asapScheduler,\n catchError,\n finalize,\n type Observable,\n type ObservedValueOf,\n of,\n share,\n timer,\n} from 'rxjs'\nimport {map, tap} from 'rxjs/operators'\n\nfunction getValue<T>(value: T): T extends () => infer U ? U : T {\n return typeof value === 'function' ? value() : value\n}\n\ninterface CacheRecord<T> {\n observable: Observable<void>\n snapshot: T\n error?: unknown\n}\n\nconst cache = new WeakMap<Observable<any>, CacheRecord<any>>()\n\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n initialValue: ObservedValueOf<ObservableType> | (() => ObservedValueOf<ObservableType>),\n): ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>>(\n observable: ObservableType,\n): undefined | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue: InitialValue | (() => InitialValue),\n): InitialValue | ObservedValueOf<ObservableType>\n/** @public */\nexport function useObservable<ObservableType extends Observable<any>, InitialValue>(\n observable: ObservableType,\n initialValue?: InitialValue | (() => InitialValue),\n): InitialValue | ObservedValueOf<ObservableType> {\n const [getInitialValue] = useState(\n () => () => getValue(initialValue) as ObservedValueOf<ObservableType>,\n )\n\n const store = useMemo(() => {\n if (!cache.has(observable)) {\n const entry: Partial<CacheRecord<ObservedValueOf<ObservableType>>> = {\n snapshot: getInitialValue(),\n }\n entry.observable = observable.pipe(\n map((value) => ({snapshot: value, error: undefined})),\n catchError((error) => of({snapshot: undefined, error})),\n tap(({snapshot, error}) => {\n entry.snapshot = snapshot\n entry.error = error\n }),\n // Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state\n // and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.\n map((value) => void value),\n // Ensure that the cache entry is deleted when the observable completes or errors.\n finalize(() => cache.delete(observable)),\n share({resetOnRefCountZero: () => timer(0, asapScheduler)}),\n )\n\n // Eagerly subscribe to sync set `entry.currentValue` to what the observable returns, and keep the observable alive until the component unmounts.\n const subscription = entry.observable.subscribe()\n subscription.unsubscribe()\n\n cache.set(observable, entry as CacheRecord<ObservedValueOf<ObservableType>>)\n }\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const instance = cache.get(observable)!\n\n return {\n subscribe: (onStoreChange: () => void) => {\n const subscription = instance.observable.subscribe(onStoreChange)\n return () => {\n subscription.unsubscribe()\n }\n },\n getSnapshot: () => {\n if (instance.error) {\n throw instance.error\n }\n return instance.snapshot\n },\n }\n }, [observable, getInitialValue])\n\n useEffect(() => {\n console.log('stor.subscribe changed', store.subscribe)\n }, [store.subscribe])\n\n return useSyncExternalStore<ObservedValueOf<ObservableType>>(\n store.subscribe,\n store.getSnapshot,\n typeof initialValue === 'undefined' ? undefined : () => getInitialValue(),\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":[],"mappings":";;;;;AAaA,SAAS,SAAY,OAA2C;AAC9D,SAAO,OAAO,SAAU,aAAa,MAAU,IAAA;AACjD;AAQA,MAAM,4BAAY,QAA2C;AAiB7C,SAAA,cACd,YACA,cACgD;AAC1C,QAAA,CAAC,eAAe,IAAI;AAAA,IACxB,MAAM,MAAM,SAAS,YAAY;AAAA,EAAA,GAG7B,QAAQ,QAAQ,MAAM;AAC1B,QAAI,CAAC,M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M,mBAAuB,CAAA,GAEzD,UAAU,eAAe,CAAC,eAA8B,YAAY,UAAU,CAAC;AAErF,SAAA,UAAU,MAAM;AACR,UAAA,eAAe,OAAO,KAAK,CAAC,eAAe,QAAQ,UAAU,CAAC,EAAE,UAAU;AACzE,WAAA,MAAM,aAAa,YAAY;AAAA,EACrC,GAAA,CAAC,QAAQ,OAAO,CAAC,GAEb;AACT;"}
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: () => 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 const state = {\n snapshot: undefined as ObservedValueOf<ObservableType>,\n error: undefined,\n }\n const entry: CacheRecord<ObservedValueOf<ObservableType>> = {\n // snapshot: getValue(initialValue) as 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: () => {\n if (state.error) {\n throw state.error\n }\n return state.snapshot\n },\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 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() ?? (getValue(initialValue) as ObservedValueOf<ObservableType>)\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","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;AACxB,UAAAK,QAAA;AAAA,MAAAC,UAAAC;AAAAA,MAAAC,OAAAD;AAAAA,OAIAE,QAAA;AAAA,MAAAJ;AAAAA,MAAAL,YAGcA,WAAUU,KACpBC,IAAAC,OAAoD,GACpDC,WAAAC,MAAsD,GACtDC,IAAAC,CAAAA,QAAA;AAAK,cAAA;AAAA,UAAAV;AAAAA,UAAAE,OAAAS;AAAAA,QAAAA,IAAAD;AACEV,cAAAA,WAAYA,UACjBD,MAAKG,QAASA;AAAAA,MAAAA,CACf,GAGDG,IAAAO,MAAyB,GAEzBC,SAAetB,MAAAA,MAAAuB,OAAapB,UAAU,CAAC,GACvCqB,MAAA;AAAA,QAAAC,qBAAAC;AAAAA,MAAAA,CAA0D,CAC5D;AAAA,MAACC,aAAAA,MAAA;AAAA,YAEKnB,MAAKG;AAAA,gBACDH,MAAKG;AAAA,eAENH,MAAKC;AAAAA,MAAAA;AAAAA,IAAA;AAKKG,UAAKT,WAAAyB,YACdC,YAEZ7B,GAAAA,MAAA8B,IAAU3B,YAAYS,KAAqD;AAAA,EAAA;AAACO,MAAAA;AAAAd,WAAAF,cAE7DgB,KAAAnB,MAAA+B,IAAU5B,UAAU,GAACE,OA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AA7B;AAAcc,MAAAA;AAAA5B,WAAAwC,eAEUZ,KAAA9B,CAAAA,eAA+B0C,YAAY1C,UAAU,GAACE,OAAAwC,aAAAxC,OAAA4B,MAAAA,KAAA5B,EAAA,CAAA;AAArF4C,QAAAA,UAAgBC,eAAejB,EAAsD;AAAC,MAAAI,IAAAC;AAAAjC,SAAAA,EAAA0C,CAAAA,MAAAA,UAAA1C,SAAA4C,WAE5EZ,KAAAA,MAAA;AACRD,UAAAA,eAAqBW,OAAMlC,KAAAsC,CAAAA,iBAAsBF,QAAQ9C,YAAU,CAAC,EAACyB,UAAW;AAAC,WAAA,MACpEQ,aAAYP,YAAa;AAAA,EACrCS,GAAAA,KAAA,CAACS,QAAQE,OAAO,GAAC5C,OAAA0C,QAAA1C,OAAA4C,SAAA5C,OAAAgC,IAAAhC,OAAAiC,OAAAD,KAAAhC,EAAA,CAAA,GAAAiC,KAAAjC,EAAA,CAAA,IAHpB+C,UAAUf,IAGPC,EAAiB,GAEbU;AAAI;AAZN,SAAAjC,QAAA;AAAA,SAGmCsC,mBAAsB;AAAC;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-rx",
3
- "version": "4.1.2-canary.1",
3
+ "version": "4.1.2-canary.11",
4
4
  "description": "React + RxJS = <3",
5
5
  "keywords": [
6
6
  "action",
@@ -1,4 +1,4 @@
1
- import {useEffect, useMemo, useState, useSyncExternalStore} from 'react'
1
+ import {useCallback, useSyncExternalStore} from 'react'
2
2
  import {
3
3
  asapScheduler,
4
4
  catchError,
@@ -17,8 +17,11 @@ function getValue<T>(value: T): T extends () => infer U ? U : T {
17
17
 
18
18
  interface CacheRecord<T> {
19
19
  observable: Observable<void>
20
- snapshot: T
21
- error?: unknown
20
+ state: {
21
+ snapshot: T
22
+ error?: unknown
23
+ }
24
+ getSnapshot: () => T
22
25
  }
23
26
 
24
27
  const cache = new WeakMap<Observable<any>, CacheRecord<any>>()
@@ -42,21 +45,20 @@ export function useObservable<ObservableType extends Observable<any>, InitialVal
42
45
  observable: ObservableType,
43
46
  initialValue?: InitialValue | (() => InitialValue),
44
47
  ): InitialValue | ObservedValueOf<ObservableType> {
45
- const [getInitialValue] = useState(
46
- () => () => getValue(initialValue) as ObservedValueOf<ObservableType>,
47
- )
48
-
49
- const store = useMemo(() => {
50
- if (!cache.has(observable)) {
51
- const entry: Partial<CacheRecord<ObservedValueOf<ObservableType>>> = {
52
- snapshot: getInitialValue(),
53
- }
54
- entry.observable = observable.pipe(
48
+ if (!cache.has(observable)) {
49
+ const state = {
50
+ snapshot: undefined as ObservedValueOf<ObservableType>,
51
+ error: undefined,
52
+ }
53
+ const entry: CacheRecord<ObservedValueOf<ObservableType>> = {
54
+ // snapshot: getValue(initialValue) as ObservedValueOf<ObservableType>,
55
+ state,
56
+ observable: observable.pipe(
55
57
  map((value) => ({snapshot: value, error: undefined})),
56
58
  catchError((error) => of({snapshot: undefined, error})),
57
59
  tap(({snapshot, error}) => {
58
- entry.snapshot = snapshot
59
- entry.error = error
60
+ state.snapshot = snapshot
61
+ state.error = error
60
62
  }),
61
63
  // Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state
62
64
  // and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.
@@ -64,40 +66,40 @@ export function useObservable<ObservableType extends Observable<any>, InitialVal
64
66
  // Ensure that the cache entry is deleted when the observable completes or errors.
65
67
  finalize(() => cache.delete(observable)),
66
68
  share({resetOnRefCountZero: () => timer(0, asapScheduler)}),
67
- )
68
-
69
- // Eagerly subscribe to sync set `entry.currentValue` to what the observable returns, and keep the observable alive until the component unmounts.
70
- const subscription = entry.observable.subscribe()
71
- subscription.unsubscribe()
72
-
73
- cache.set(observable, entry as CacheRecord<ObservedValueOf<ObservableType>>)
74
- }
75
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
76
- const instance = cache.get(observable)!
77
-
78
- return {
79
- subscribe: (onStoreChange: () => void) => {
80
- const subscription = instance.observable.subscribe(onStoreChange)
81
- return () => {
82
- subscription.unsubscribe()
83
- }
84
- },
69
+ ),
85
70
  getSnapshot: () => {
86
- if (instance.error) {
87
- throw instance.error
71
+ if (state.error) {
72
+ throw state.error
88
73
  }
89
- return instance.snapshot
74
+ return state.snapshot
90
75
  },
91
76
  }
92
- }, [observable, getInitialValue])
93
77
 
94
- useEffect(() => {
95
- console.log('stor.subscribe changed', store.subscribe)
96
- }, [store.subscribe])
78
+ // Eagerly subscribe to sync set `entry.currentValue` to what the observable returns, and keep the observable alive until the component unmounts.
79
+ const subscription = entry.observable.subscribe()
80
+ subscription.unsubscribe()
81
+
82
+ cache.set(observable, entry as CacheRecord<ObservedValueOf<ObservableType>>)
83
+ }
84
+ const instance = cache.get(observable)!
85
+
86
+ const subscribe = useCallback(
87
+ (onStoreChange: () => void) => {
88
+ const subscription = instance.observable.subscribe(onStoreChange)
89
+ return () => {
90
+ subscription.unsubscribe()
91
+ }
92
+ },
93
+ [instance.observable],
94
+ )
97
95
 
98
96
  return useSyncExternalStore<ObservedValueOf<ObservableType>>(
99
- store.subscribe,
100
- store.getSnapshot,
101
- typeof initialValue === 'undefined' ? undefined : () => getInitialValue(),
97
+ subscribe,
98
+ () => {
99
+ return instance.getSnapshot() ?? (getValue(initialValue) as ObservedValueOf<ObservableType>)
100
+ },
101
+ typeof initialValue === 'undefined'
102
+ ? undefined
103
+ : () => getValue(initialValue) as ObservedValueOf<ObservableType>,
102
104
  )
103
105
  }