Comparing version 4.1.2-canary.1 to 4.1.2-canary.2
@@ -1,2 +0,3 @@ | ||
import { useState, useMemo, useEffect, useSyncExternalStore } from "react"; | ||
import { c } from "react-compiler-runtime"; | ||
import { useState, useSyncExternalStore, useEffect } from "react"; | ||
import { catchError, of, finalize, share, timer, asapScheduler } from "rxjs"; | ||
@@ -11,54 +12,67 @@ import { map, tap } from "rxjs/operators"; | ||
function useObservable(observable, initialValue) { | ||
const [getInitialValue] = useState( | ||
() => () => getValue(initialValue) | ||
), store = useMemo(() => { | ||
if (!cache.has(observable)) { | ||
const entry = { | ||
snapshot: getInitialValue() | ||
}; | ||
entry.observable = observable.pipe( | ||
map((value) => ({ snapshot: value, error: void 0 })), | ||
catchError((error) => of({ snapshot: void 0, error })), | ||
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) => { | ||
const subscription = instance.observable.subscribe(onStoreChange); | ||
return () => { | ||
subscription.unsubscribe(); | ||
}; | ||
}, | ||
getSnapshot: () => { | ||
if (instance.error) | ||
throw instance.error; | ||
return instance.snapshot; | ||
} | ||
const $ = c(14); | ||
let t0; | ||
$[0] !== initialValue ? (t0 = () => () => getValue(initialValue), $[0] = initialValue, $[1] = t0) : t0 = $[1]; | ||
const [getInitialValue] = useState(t0); | ||
let t1; | ||
if (!cache.has(observable)) { | ||
const entry = { | ||
snapshot: getInitialValue() | ||
}; | ||
}, [observable, getInitialValue]); | ||
return useEffect(() => { | ||
console.log("stor.subscribe changed", store.subscribe); | ||
}, [store.subscribe]), useSyncExternalStore( | ||
store.subscribe, | ||
store.getSnapshot, | ||
typeof initialValue > "u" ? void 0 : () => getInitialValue() | ||
); | ||
entry.observable = observable.pipe(map((value) => ({ | ||
snapshot: value, | ||
error: void 0 | ||
})), catchError((error) => of({ | ||
snapshot: void 0, | ||
error | ||
})), tap((t22) => { | ||
const { | ||
snapshot, | ||
error: error_0 | ||
} = t22; | ||
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 t2; | ||
$[2] !== observable ? (t2 = cache.get(observable), $[2] = observable, $[3] = t2) : t2 = $[3]; | ||
const instance = t2; | ||
let t3; | ||
$[4] !== instance.observable ? (t3 = (onStoreChange) => { | ||
const subscription_0 = instance.observable.subscribe(onStoreChange); | ||
return () => { | ||
subscription_0.unsubscribe(); | ||
}; | ||
}, $[4] = instance.observable, $[5] = t3) : t3 = $[5]; | ||
let t4; | ||
$[6] !== instance.error || $[7] !== instance.snapshot ? (t4 = () => { | ||
if (instance.error) | ||
throw instance.error; | ||
return instance.snapshot; | ||
}, $[6] = instance.error, $[7] = instance.snapshot, $[8] = t4) : t4 = $[8]; | ||
let t5; | ||
$[9] !== t3 || $[10] !== t4 ? (t5 = { | ||
subscribe: t3, | ||
getSnapshot: t4 | ||
}, $[9] = t3, $[10] = t4, $[11] = t5) : t5 = $[11], t1 = t5; | ||
const store = t1; | ||
let t6; | ||
return $[12] !== initialValue ? (t6 = typeof initialValue > "u" ? void 0 : () => getValue(initialValue), $[12] = initialValue, $[13] = t6) : t6 = $[13], useSyncExternalStore(store.subscribe, store.getSnapshot, t6); | ||
} | ||
function useObservableEvent(handleEvent) { | ||
const [[calls$, call]] = useState(() => observableCallback()), onEvent = useEffectEvent((observable) => handleEvent(observable)); | ||
return useEffect(() => { | ||
const subscription = calls$.pipe((observable) => onEvent(observable)).subscribe(); | ||
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(); | ||
return () => subscription.unsubscribe(); | ||
}, [calls$, onEvent]), call; | ||
}, t3 = [calls$, onEvent], $[2] = calls$, $[3] = onEvent, $[4] = t2, $[5] = t3) : (t2 = $[4], t3 = $[5]), useEffect(t2, t3), call; | ||
} | ||
function _temp() { | ||
return observableCallback(); | ||
} | ||
export { | ||
@@ -65,0 +79,0 @@ useObservable, |
{ | ||
"name": "react-rx", | ||
"version": "4.1.2-canary.1", | ||
"version": "4.1.2-canary.2", | ||
"description": "React + RxJS = <3", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -1,2 +0,2 @@ | ||
import {useEffect, useMemo, useState, useSyncExternalStore} from 'react' | ||
import {useMemo, useState, useSyncExternalStore} from 'react' | ||
import { | ||
@@ -94,11 +94,9 @@ asapScheduler, | ||
useEffect(() => { | ||
console.log('stor.subscribe changed', store.subscribe) | ||
}, [store.subscribe]) | ||
return useSyncExternalStore<ObservedValueOf<ObservableType>>( | ||
store.subscribe, | ||
store.getSnapshot, | ||
typeof initialValue === 'undefined' ? undefined : () => getInitialValue(), | ||
typeof initialValue === 'undefined' | ||
? undefined | ||
: () => getValue(initialValue) as ObservedValueOf<ObservableType>, | ||
) | ||
} |
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
47086
608