Comparing version 4.1.2-canary.6 to 4.1.2-canary.7
@@ -1,3 +0,2 @@ | ||
import { c } from "react-compiler-runtime"; | ||
import { useSyncExternalStore, useState, useEffect } from "react"; | ||
import { useMemo, useSyncExternalStore, useState, useEffect } from "react"; | ||
import { catchError, of, finalize, share, timer, asapScheduler } from "rxjs"; | ||
@@ -12,65 +11,51 @@ import { map, tap } from "rxjs/operators"; | ||
function useObservable(observable, initialValue, debug) { | ||
const $ = c(18); | ||
let t0; | ||
if (!cache.has(observable)) { | ||
const entry = {}; | ||
entry.observable = observable.pipe(map((value) => ({ | ||
snapshot: value, | ||
error: void 0 | ||
})), catchError((error) => of({ | ||
snapshot: void 0, | ||
error | ||
})), tap((t12) => { | ||
const { | ||
snapshot, | ||
error: error_0 | ||
} = t12; | ||
debug && console.log("tap", snapshot, error_0), entry.snapshot = snapshot, entry.error = error_0; | ||
}), map((value_0) => { | ||
}), finalize(() => cache.delete(observable)), share({ | ||
resetOnRefCountZero: () => timer(0, asapScheduler) | ||
})), entry.observable.subscribe().unsubscribe(), cache.set(observable, entry); | ||
} | ||
let t1; | ||
$[0] !== observable ? (t1 = cache.get(observable), $[0] = observable, $[1] = t1) : t1 = $[1]; | ||
const instance = t1; | ||
let t2; | ||
$[2] !== debug || $[3] !== observable || $[4] !== instance.observable ? (t2 = (onStoreChange) => { | ||
debug && console.log("subscribe", observable); | ||
const subscription_0 = instance.observable.subscribe(onStoreChange); | ||
return () => { | ||
debug && console.log("unsubscribe", observable), subscription_0.unsubscribe(); | ||
const store = useMemo(() => { | ||
if (!cache.has(observable)) { | ||
const entry = {}; | ||
entry.observable = observable.pipe( | ||
map((value) => ({ snapshot: value, error: void 0 })), | ||
catchError((error) => of({ snapshot: void 0, error })), | ||
tap(({ snapshot, error }) => { | ||
debug && console.log("tap", snapshot, error), entry.snapshot = snapshot, entry.error = error; | ||
}), | ||
// Note: any value or error emitted by the provided observable will be mapped to the cache entry's mutable state | ||
// and the observable is thereafter only used as a notifier to call `onStoreChange`, hence the `void` return type. | ||
map((value) => { | ||
}), | ||
// Ensure that the cache entry is deleted when the observable completes or errors. | ||
finalize(() => cache.delete(observable)), | ||
share({ resetOnRefCountZero: () => timer(0, asapScheduler) }) | ||
), entry.observable.subscribe().unsubscribe(), cache.set(observable, entry); | ||
} | ||
const instance = cache.get(observable); | ||
return { | ||
subscribe: (onStoreChange) => { | ||
debug && console.log("subscribe", observable); | ||
const subscription = instance.observable.subscribe(() => { | ||
console.log("onStoreChange", observable), onStoreChange(); | ||
}); | ||
return () => { | ||
debug && console.log("unsubscribe", observable), subscription.unsubscribe(); | ||
}; | ||
}, | ||
getSnapshot: () => { | ||
if (debug && console.log("getSnapshot", instance.snapshot, instance.error), instance.error) | ||
throw instance.error; | ||
return instance.snapshot; | ||
} | ||
}; | ||
}, $[2] = debug, $[3] = observable, $[4] = instance.observable, $[5] = t2) : t2 = $[5]; | ||
let t3; | ||
$[6] !== debug || $[7] !== instance.snapshot || $[8] !== instance.error ? (t3 = () => { | ||
if (debug && console.log("getSnapshot", instance.snapshot, instance.error), instance.error) | ||
throw instance.error; | ||
return instance.snapshot; | ||
}, $[6] = debug, $[7] = instance.snapshot, $[8] = instance.error, $[9] = t3) : t3 = $[9]; | ||
let t4; | ||
$[10] !== t2 || $[11] !== t3 ? (t4 = { | ||
subscribe: t2, | ||
getSnapshot: t3 | ||
}, $[10] = t2, $[11] = t3, $[12] = t4) : t4 = $[12], t0 = t4; | ||
const store = t0; | ||
let t5; | ||
$[13] !== store || $[14] !== initialValue ? (t5 = () => store.getSnapshot() ?? getValue(initialValue), $[13] = store, $[14] = initialValue, $[15] = t5) : t5 = $[15]; | ||
let t6; | ||
return $[16] !== initialValue ? (t6 = typeof initialValue > "u" ? void 0 : () => getValue(initialValue), $[16] = initialValue, $[17] = t6) : t6 = $[17], useSyncExternalStore(store.subscribe, t5, t6); | ||
}, [debug, observable]); | ||
return useSyncExternalStore( | ||
store.subscribe, | ||
() => store.getSnapshot() ?? getValue(initialValue), | ||
typeof initialValue > "u" ? void 0 : () => getValue(initialValue) | ||
); | ||
} | ||
function useObservableEvent(handleEvent) { | ||
const $ = c(6), [t0] = useState(_temp), [calls$, call] = t0; | ||
let t1; | ||
$[0] !== handleEvent ? (t1 = (observable) => handleEvent(observable), $[0] = handleEvent, $[1] = t1) : t1 = $[1]; | ||
const onEvent = useEffectEvent(t1); | ||
let t2, t3; | ||
return $[2] !== calls$ || $[3] !== onEvent ? (t2 = () => { | ||
const subscription = calls$.pipe((observable_0) => onEvent(observable_0)).subscribe(); | ||
const [[calls$, call]] = useState(() => observableCallback()), onEvent = useEffectEvent((observable) => handleEvent(observable)); | ||
return useEffect(() => { | ||
const subscription = calls$.pipe((observable) => onEvent(observable)).subscribe(); | ||
return () => subscription.unsubscribe(); | ||
}, t3 = [calls$, onEvent], $[2] = calls$, $[3] = onEvent, $[4] = t2, $[5] = t3) : (t2 = $[4], t3 = $[5]), useEffect(t2, t3), call; | ||
}, [calls$, onEvent]), call; | ||
} | ||
function _temp() { | ||
return observableCallback(); | ||
} | ||
export { | ||
@@ -77,0 +62,0 @@ useObservable, |
{ | ||
"name": "react-rx", | ||
"version": "4.1.2-canary.6", | ||
"version": "4.1.2-canary.7", | ||
"description": "React + RxJS = <3", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -83,3 +83,6 @@ import {useMemo, useSyncExternalStore} from 'react' | ||
} | ||
const subscription = instance.observable.subscribe(onStoreChange) | ||
const subscription = instance.observable.subscribe(() => { | ||
console.log('onStoreChange', observable) | ||
onStoreChange() | ||
}) | ||
return () => { | ||
@@ -86,0 +89,0 @@ if (debug) { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
44276
590