react-rx 4.0.1-canary.1 → 4.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +39 -62
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +40 -64
- package/dist/index.js.map +1 -1
- package/package.json +19 -19
package/dist/index.cjs
CHANGED
|
@@ -1,81 +1,58 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: !0 });
|
|
3
|
-
var
|
|
3
|
+
var 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 $ = reactCompilerRuntime.c(12);
|
|
10
9
|
if (!cache.has(observable)) {
|
|
11
10
|
const entry = {
|
|
12
11
|
snapshot: getValue(initialValue)
|
|
13
12
|
};
|
|
14
|
-
entry.observable = observable.pipe(
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
13
|
+
entry.observable = observable.pipe(
|
|
14
|
+
operators.map((value) => ({ snapshot: value, error: void 0 })),
|
|
15
|
+
rxjs.catchError((error) => rxjs.of({ snapshot: void 0, error })),
|
|
16
|
+
operators.tap(({ snapshot, error }) => {
|
|
17
|
+
entry.snapshot = snapshot, entry.error = error;
|
|
18
|
+
}),
|
|
19
|
+
// Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state
|
|
20
|
+
// and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.
|
|
21
|
+
operators.map((value) => {
|
|
22
|
+
}),
|
|
23
|
+
// Ensure that the cache entry is deleted when the observable completes or errors.
|
|
24
|
+
rxjs.finalize(() => cache.delete(observable)),
|
|
25
|
+
rxjs.share({ resetOnRefCountZero: () => rxjs.timer(0, rxjs.asapScheduler) })
|
|
26
|
+
), entry.observable.subscribe().unsubscribe(), cache.set(observable, entry);
|
|
23
27
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
28
|
+
const store = react.useMemo(() => {
|
|
29
|
+
const instance = cache.get(observable);
|
|
30
|
+
return {
|
|
31
|
+
subscribe: (onStoreChange) => {
|
|
32
|
+
const subscription = instance.observable.subscribe(onStoreChange);
|
|
33
|
+
return () => {
|
|
34
|
+
subscription.unsubscribe();
|
|
35
|
+
};
|
|
36
|
+
},
|
|
37
|
+
getSnapshot: () => {
|
|
38
|
+
if (instance.error)
|
|
39
|
+
throw instance.error;
|
|
40
|
+
return instance.snapshot;
|
|
41
|
+
}
|
|
32
42
|
};
|
|
33
|
-
},
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}, $[4] = instance.error, $[5] = instance.snapshot, $[6] = t3) : t3 = $[6];
|
|
40
|
-
let t4;
|
|
41
|
-
$[7] !== t2 || $[8] !== t3 ? (t4 = {
|
|
42
|
-
subscribe: t2,
|
|
43
|
-
getSnapshot: t3
|
|
44
|
-
}, $[7] = t2, $[8] = t3, $[9] = t4) : t4 = $[9], t0 = t4;
|
|
45
|
-
const store = t0;
|
|
46
|
-
let t5;
|
|
47
|
-
return $[10] !== initialValue ? (t5 = typeof initialValue > "u" ? void 0 : () => getValue(initialValue), $[10] = initialValue, $[11] = t5) : t5 = $[11], react.useSyncExternalStore(store.subscribe, store.getSnapshot, t5);
|
|
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
|
-
};
|
|
43
|
+
}, [observable]);
|
|
44
|
+
return react.useSyncExternalStore(
|
|
45
|
+
store.subscribe,
|
|
46
|
+
store.getSnapshot,
|
|
47
|
+
typeof initialValue > "u" ? void 0 : () => getValue(initialValue)
|
|
48
|
+
);
|
|
65
49
|
}
|
|
66
50
|
function useObservableEvent(handleEvent) {
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
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();
|
|
51
|
+
const [[calls$, call]] = react.useState(() => observableCallback.observableCallback()), onEvent = useEffectEvent.useEffectEvent((observable) => handleEvent(observable));
|
|
52
|
+
return react.useEffect(() => {
|
|
53
|
+
const subscription = calls$.pipe((observable) => onEvent(observable)).subscribe();
|
|
74
54
|
return () => subscription.unsubscribe();
|
|
75
|
-
},
|
|
76
|
-
}
|
|
77
|
-
function _temp() {
|
|
78
|
-
return observableCallback.observableCallback();
|
|
55
|
+
}, [calls$, onEvent]), call;
|
|
79
56
|
}
|
|
80
57
|
exports.useObservable = useObservable;
|
|
81
58
|
exports.useObservableEvent = useObservableEvent;
|
package/dist/index.cjs.map
CHANGED
|
@@ -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 if (!cache.has(observable)) {\n const entry: Partial<CacheRecord<ObservedValueOf<ObservableType>>> = {\n snapshot: getValue(initialValue) as ObservedValueOf<ObservableType>,\n }\n entry.observable = observable.pipe(\n map((value) => ({snapshot: value, error: undefined})),\n catchError((error) => of({snapshot: undefined, error})),\n tap(({snapshot, error}) => {\n entry.snapshot = snapshot\n entry.error = error\n }),\n // Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state\n // and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.\n map((value) => void value),\n // Ensure that the cache entry is deleted when the observable completes or errors.\n finalize(() => cache.delete(observable)),\n share({resetOnRefCountZero: () => timer(0, asapScheduler)}),\n )\n\n // Eagerly subscribe to sync set `entry.currentValue` to what the observable returns, and keep the observable alive until the component unmounts.\n const subscription = entry.observable.subscribe()\n subscription.unsubscribe()\n\n cache.set(observable, entry as CacheRecord<ObservedValueOf<ObservableType>>)\n }\n\n const store = useMemo(() => {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const instance = cache.get(observable)!\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,\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":["
|
|
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 if (!cache.has(observable)) {\n const entry: Partial<CacheRecord<ObservedValueOf<ObservableType>>> = {\n snapshot: getValue(initialValue) as ObservedValueOf<ObservableType>,\n }\n entry.observable = observable.pipe(\n map((value) => ({snapshot: value, error: undefined})),\n catchError((error) => of({snapshot: undefined, error})),\n tap(({snapshot, error}) => {\n entry.snapshot = snapshot\n entry.error = error\n }),\n // Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state\n // and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.\n map((value) => void value),\n // Ensure that the cache entry is deleted when the observable completes or errors.\n finalize(() => cache.delete(observable)),\n share({resetOnRefCountZero: () => timer(0, asapScheduler)}),\n )\n\n // Eagerly subscribe to sync set `entry.currentValue` to what the observable returns, and keep the observable alive until the component unmounts.\n const subscription = entry.observable.subscribe()\n subscription.unsubscribe()\n\n cache.set(observable, entry as CacheRecord<ObservedValueOf<ObservableType>>)\n }\n\n const store = useMemo(() => {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const instance = cache.get(observable)!\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,\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":["map","catchError","of","tap","finalize","share","timer","asapScheduler","useMemo","useSyncExternalStore","useState","observableCallback","useEffectEvent","useEffect"],"mappings":";;;AAaA,SAAS,SAAY,OAA2C;AAC9D,SAAO,OAAO,SAAU,aAAa,MAAU,IAAA;AACjD;AAQA,MAAM,4BAAY,QAA2C;AAiB7C,SAAA,cACd,YACA,cACgD;AAChD,MAAI,CAAC,MAAM,IAAI,UAAU,GAAG;AAC1B,UAAM,QAA+D;AAAA,MACnE,UAAU,SAAS,YAAY;AAAA,IACjC;AACA,UAAM,aAAa,WAAW;AAAA,MAC5BA,cAAI,CAAC,WAAW,EAAC,UAAU,OAAO,OAAO,SAAW;AAAA,MACpDC,gBAAW,CAAC,UAAUC,QAAG,EAAC,UAAU,QAAW,MAAK,CAAC,CAAC;AAAA,MACtDC,UAAAA,IAAI,CAAC,EAAC,UAAU,YAAW;AACnB,cAAA,WAAW,UACjB,MAAM,QAAQ;AAAA,MAAA,CACf;AAAA;AAAA;AAAA,MAGDH,UAAA,IAAI,CAAC,UAAO;AAAA,MAAA,CAAa;AAAA;AAAA,MAEzBI,KAAAA,SAAS,MAAM,MAAM,OAAO,UAAU,CAAC;AAAA,MACvCC,KAAA,MAAM,EAAC,qBAAqB,MAAMC,WAAM,GAAGC,KAAa,aAAA,EAAE,CAAA;AAAA,IAAA,GAIvC,MAAM,WAAW,YACzB,YAEb,GAAA,MAAM,IAAI,YAAY,KAAqD;AAAA,EAAA;AAGvE,QAAA,QAAQC,MAAAA,QAAQ,MAAM;AAEpB,UAAA,WAAW,MAAM,IAAI,UAAU;AAC9B,WAAA;AAAA,MACL,WAAW,CAAC,kBAA8B;AACxC,cAAM,eAAe,SAAS,WAAW,UAAU,aAAa;AAChE,eAAO,MAAM;AACX,uBAAa,YAAY;AAAA,QAC3B;AAAA,MACF;AAAA,MACA,aAAa,MAAM;AACjB,YAAI,SAAS;AACX,gBAAM,SAAS;AAEjB,eAAO,SAAS;AAAA,MAAA;AAAA,IAEpB;AAAA,EAAA,GACC,CAAC,UAAU,CAAC;AAER,SAAAC,MAAA;AAAA,IACL,MAAM;AAAA,IACN,MAAM;AAAA,IACN,OAAO,eAAiB,MACpB,SACA,MAAM,SAAS,YAAY;AAAA,EACjC;AACF;AC1FO,SAAS,mBACd,aACkB;AAClB,QAAM,CAAC,CAAC,QAAQ,IAAI,CAAC,IAAIC,MAAS,SAAA,MAAMC,mBAAAA,mBAAuB,CAAA,GAEzD,UAAUC,eAAAA,eAAe,CAAC,eAA8B,YAAY,UAAU,CAAC;AAErF,SAAAC,MAAA,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;;;"}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { catchError, finalize, share, timer, asapScheduler, of } from "rxjs";
|
|
1
|
+
import { useMemo, useSyncExternalStore, useState, useEffect } from "react";
|
|
2
|
+
import { catchError, of, finalize, share, timer, asapScheduler } from "rxjs";
|
|
4
3
|
import { map, tap } from "rxjs/operators";
|
|
5
4
|
import { observableCallback } from "observable-callback";
|
|
6
5
|
import { useEffectEvent } from "use-effect-event";
|
|
@@ -9,76 +8,53 @@ function getValue(value) {
|
|
|
9
8
|
}
|
|
10
9
|
const cache = /* @__PURE__ */ new WeakMap();
|
|
11
10
|
function useObservable(observable, initialValue) {
|
|
12
|
-
const $ = c(12);
|
|
13
11
|
if (!cache.has(observable)) {
|
|
14
12
|
const entry = {
|
|
15
13
|
snapshot: getValue(initialValue)
|
|
16
14
|
};
|
|
17
|
-
entry.observable = observable.pipe(
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
15
|
+
entry.observable = observable.pipe(
|
|
16
|
+
map((value) => ({ snapshot: value, error: void 0 })),
|
|
17
|
+
catchError((error) => of({ snapshot: void 0, error })),
|
|
18
|
+
tap(({ snapshot, error }) => {
|
|
19
|
+
entry.snapshot = snapshot, entry.error = error;
|
|
20
|
+
}),
|
|
21
|
+
// Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state
|
|
22
|
+
// and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.
|
|
23
|
+
map((value) => {
|
|
24
|
+
}),
|
|
25
|
+
// Ensure that the cache entry is deleted when the observable completes or errors.
|
|
26
|
+
finalize(() => cache.delete(observable)),
|
|
27
|
+
share({ resetOnRefCountZero: () => timer(0, asapScheduler) })
|
|
28
|
+
), entry.observable.subscribe().unsubscribe(), cache.set(observable, entry);
|
|
26
29
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
const store = useMemo(() => {
|
|
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
|
+
},
|
|
39
|
+
getSnapshot: () => {
|
|
40
|
+
if (instance.error)
|
|
41
|
+
throw instance.error;
|
|
42
|
+
return instance.snapshot;
|
|
43
|
+
}
|
|
35
44
|
};
|
|
36
|
-
},
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}, $[4] = instance.error, $[5] = instance.snapshot, $[6] = t3) : t3 = $[6];
|
|
43
|
-
let t4;
|
|
44
|
-
$[7] !== t2 || $[8] !== t3 ? (t4 = {
|
|
45
|
-
subscribe: t2,
|
|
46
|
-
getSnapshot: t3
|
|
47
|
-
}, $[7] = t2, $[8] = t3, $[9] = t4) : t4 = $[9], t0 = t4;
|
|
48
|
-
const store = t0;
|
|
49
|
-
let t5;
|
|
50
|
-
return $[10] !== initialValue ? (t5 = typeof initialValue > "u" ? void 0 : () => getValue(initialValue), $[10] = initialValue, $[11] = t5) : t5 = $[11], useSyncExternalStore(store.subscribe, store.getSnapshot, t5);
|
|
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
|
-
};
|
|
45
|
+
}, [observable]);
|
|
46
|
+
return useSyncExternalStore(
|
|
47
|
+
store.subscribe,
|
|
48
|
+
store.getSnapshot,
|
|
49
|
+
typeof initialValue > "u" ? void 0 : () => getValue(initialValue)
|
|
50
|
+
);
|
|
68
51
|
}
|
|
69
52
|
function useObservableEvent(handleEvent) {
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
|
|
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();
|
|
53
|
+
const [[calls$, call]] = useState(() => observableCallback()), onEvent = useEffectEvent((observable) => handleEvent(observable));
|
|
54
|
+
return useEffect(() => {
|
|
55
|
+
const subscription = calls$.pipe((observable) => onEvent(observable)).subscribe();
|
|
77
56
|
return () => subscription.unsubscribe();
|
|
78
|
-
},
|
|
79
|
-
}
|
|
80
|
-
function _temp() {
|
|
81
|
-
return observableCallback();
|
|
57
|
+
}, [calls$, onEvent]), call;
|
|
82
58
|
}
|
|
83
59
|
export {
|
|
84
60
|
useObservable,
|
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 if (!cache.has(observable)) {\n const entry: Partial<CacheRecord<ObservedValueOf<ObservableType>>> = {\n snapshot: getValue(initialValue) as ObservedValueOf<ObservableType>,\n }\n entry.observable = observable.pipe(\n map((value) => ({snapshot: value, error: undefined})),\n catchError((error) => of({snapshot: undefined, error})),\n tap(({snapshot, error}) => {\n entry.snapshot = snapshot\n entry.error = error\n }),\n // Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state\n // and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.\n map((value) => void value),\n // Ensure that the cache entry is deleted when the observable completes or errors.\n finalize(() => cache.delete(observable)),\n share({resetOnRefCountZero: () => timer(0, asapScheduler)}),\n )\n\n // Eagerly subscribe to sync set `entry.currentValue` to what the observable returns, and keep the observable alive until the component unmounts.\n const subscription = entry.observable.subscribe()\n subscription.unsubscribe()\n\n cache.set(observable, entry as CacheRecord<ObservedValueOf<ObservableType>>)\n }\n\n const store = useMemo(() => {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const instance = cache.get(observable)!\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,\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":[
|
|
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 if (!cache.has(observable)) {\n const entry: Partial<CacheRecord<ObservedValueOf<ObservableType>>> = {\n snapshot: getValue(initialValue) as ObservedValueOf<ObservableType>,\n }\n entry.observable = observable.pipe(\n map((value) => ({snapshot: value, error: undefined})),\n catchError((error) => of({snapshot: undefined, error})),\n tap(({snapshot, error}) => {\n entry.snapshot = snapshot\n entry.error = error\n }),\n // Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state\n // and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type.\n map((value) => void value),\n // Ensure that the cache entry is deleted when the observable completes or errors.\n finalize(() => cache.delete(observable)),\n share({resetOnRefCountZero: () => timer(0, asapScheduler)}),\n )\n\n // Eagerly subscribe to sync set `entry.currentValue` to what the observable returns, and keep the observable alive until the component unmounts.\n const subscription = entry.observable.subscribe()\n subscription.unsubscribe()\n\n cache.set(observable, entry as CacheRecord<ObservedValueOf<ObservableType>>)\n }\n\n const store = useMemo(() => {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const instance = cache.get(observable)!\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,\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":[],"mappings":";;;;;AAaA,SAAS,SAAY,OAA2C;AAC9D,SAAO,OAAO,SAAU,aAAa,MAAU,IAAA;AACjD;AAQA,MAAM,4BAAY,QAA2C;AAiB7C,SAAA,cACd,YACA,cACgD;AAChD,MAAI,CAAC,MAAM,IAAI,UAAU,GAAG;AAC1B,UAAM,QAA+D;AAAA,MACnE,UAAU,SAAS,YAAY;AAAA,IACjC;AACA,UAAM,aAAa,WAAW;AAAA,MAC5B,IAAI,CAAC,WAAW,EAAC,UAAU,OAAO,OAAO,SAAW;AAAA,MACpD,WAAW,CAAC,UAAU,GAAG,EAAC,UAAU,QAAW,MAAK,CAAC,CAAC;AAAA,MACtD,IAAI,CAAC,EAAC,UAAU,YAAW;AACnB,cAAA,WAAW,UACjB,MAAM,QAAQ;AAAA,MAAA,CACf;AAAA;AAAA;AAAA,MAGD,IAAI,CAAC,UAAO;AAAA,MAAA,CAAa;AAAA;AAAA,MAEzB,SAAS,MAAM,MAAM,OAAO,UAAU,CAAC;AAAA,MACvC,MAAM,EAAC,qBAAqB,MAAM,MAAM,GAAG,aAAa,EAAE,CAAA;AAAA,IAAA,GAIvC,MAAM,WAAW,YACzB,YAEb,GAAA,MAAM,IAAI,YAAY,KAAqD;AAAA,EAAA;AAGvE,QAAA,QAAQ,QAAQ,MAAM;AAEpB,UAAA,WAAW,MAAM,IAAI,UAAU;AAC9B,WAAA;AAAA,MACL,WAAW,CAAC,kBAA8B;AACxC,cAAM,eAAe,SAAS,WAAW,UAAU,aAAa;AAChE,eAAO,MAAM;AACX,uBAAa,YAAY;AAAA,QAC3B;AAAA,MACF;AAAA,MACA,aAAa,MAAM;AACjB,YAAI,SAAS;AACX,gBAAM,SAAS;AAEjB,eAAO,SAAS;AAAA,MAAA;AAAA,IAEpB;AAAA,EAAA,GACC,CAAC,UAAU,CAAC;AAER,SAAA;AAAA,IACL,MAAM;AAAA,IACN,MAAM;AAAA,IACN,OAAO,eAAiB,MACpB,SACA,MAAM,SAAS,YAAY;AAAA,EACjC;AACF;AC1FO,SAAS,mBACd,aACkB;AAClB,QAAM,CAAC,CAAC,QAAQ,IAAI,CAAC,IAAI,SAAS,MAAM,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;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-rx",
|
|
3
|
-
"version": "4.0.1
|
|
3
|
+
"version": "4.0.1",
|
|
4
4
|
"description": "React + RxJS = <3",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"action",
|
|
@@ -62,11 +62,19 @@
|
|
|
62
62
|
"dist",
|
|
63
63
|
"src"
|
|
64
64
|
],
|
|
65
|
+
"scripts": {
|
|
66
|
+
"build": "pkg build --strict --clean --check",
|
|
67
|
+
"dev": "pnpm --filter 'react-rx-website' dev",
|
|
68
|
+
"format": "prettier --cache --write .",
|
|
69
|
+
"lint": "eslint --cache .",
|
|
70
|
+
"prepublishOnly": "pnpm build",
|
|
71
|
+
"test": "vitest run --typecheck",
|
|
72
|
+
"watch": "pnpm build -- --watch"
|
|
73
|
+
},
|
|
65
74
|
"browserslist": "extends @sanity/browserslist-config",
|
|
66
75
|
"prettier": "@sanity/prettier-config",
|
|
67
76
|
"dependencies": {
|
|
68
77
|
"observable-callback": "^1.0.3",
|
|
69
|
-
"react-compiler-runtime": "19.0.0-beta-6fc168f-20241025",
|
|
70
78
|
"use-effect-event": "^1.0.2"
|
|
71
79
|
},
|
|
72
80
|
"devDependencies": {
|
|
@@ -76,17 +84,16 @@
|
|
|
76
84
|
"@testing-library/dom": "^10.4.0",
|
|
77
85
|
"@testing-library/react": "^16.0.1",
|
|
78
86
|
"@types/node": "^18.17.5",
|
|
79
|
-
"@types/react": "^18.3.
|
|
80
|
-
"@types/react-dom": "^18.3.
|
|
81
|
-
"@typescript-eslint/eslint-plugin": "^
|
|
82
|
-
"@typescript-eslint/parser": "^
|
|
83
|
-
"babel-plugin-react-compiler": "beta",
|
|
87
|
+
"@types/react": "^18.3.12",
|
|
88
|
+
"@types/react-dom": "^18.3.1",
|
|
89
|
+
"@typescript-eslint/eslint-plugin": "^8.12.2",
|
|
90
|
+
"@typescript-eslint/parser": "^8.12.2",
|
|
84
91
|
"eslint": "^8.57.1",
|
|
85
92
|
"eslint-config-prettier": "^9.1.0",
|
|
86
93
|
"eslint-plugin-prettier": "^5.2.1",
|
|
87
94
|
"eslint-plugin-react": "^7.37.2",
|
|
88
95
|
"eslint-plugin-react-compiler": "beta",
|
|
89
|
-
"eslint-plugin-react-hooks": "^
|
|
96
|
+
"eslint-plugin-react-hooks": "^5.0.0",
|
|
90
97
|
"eslint-plugin-simple-import-sort": "^12.1.1",
|
|
91
98
|
"jsdom": "^24.1.0",
|
|
92
99
|
"prettier": "^3.3.3",
|
|
@@ -96,18 +103,11 @@
|
|
|
96
103
|
"rxjs": "^7.8.1",
|
|
97
104
|
"semantic-release": "^24.1.0",
|
|
98
105
|
"typescript": "5.6.3",
|
|
99
|
-
"vitest": "^2.
|
|
106
|
+
"vitest": "^2.1.4"
|
|
100
107
|
},
|
|
101
108
|
"peerDependencies": {
|
|
102
|
-
"react": "^18.3 || >=19.0.0-
|
|
109
|
+
"react": "^18.3 || >=19.0.0-rc",
|
|
103
110
|
"rxjs": "^7"
|
|
104
111
|
},
|
|
105
|
-
"
|
|
106
|
-
|
|
107
|
-
"dev": "pnpm --filter 'react-rx-website' dev",
|
|
108
|
-
"format": "prettier --cache --write .",
|
|
109
|
-
"lint": "eslint --cache .",
|
|
110
|
-
"test": "vitest run --typecheck",
|
|
111
|
-
"watch": "pnpm build -- --watch"
|
|
112
|
-
}
|
|
113
|
-
}
|
|
112
|
+
"packageManager": "pnpm@9.12.3"
|
|
113
|
+
}
|