mirror of
https://github.com/re-rxjs/react-rxjs.git
synced 2025-12-08 18:01:51 +00:00
chore(core/useObservable): improve implementation
This commit is contained in:
parent
eacde15d2a
commit
ae2c645109
@ -34,24 +34,18 @@ export const useObservable = <O>(
|
||||
}, onError)
|
||||
if (err !== EMPTY_VALUE) return
|
||||
|
||||
const set = (value: O | (() => O)) => {
|
||||
const set = (value: O) => {
|
||||
if (!Object.is(prevStateRef.current, value)) {
|
||||
prevStateRef.current = value
|
||||
if (typeof value === "function") {
|
||||
setState(() => [(value as any)(), source$])
|
||||
} else {
|
||||
setState([value, source$])
|
||||
}
|
||||
setState([(prevStateRef.current = value), source$])
|
||||
}
|
||||
}
|
||||
|
||||
if (syncVal === EMPTY_VALUE) {
|
||||
set(defaultValue)
|
||||
}
|
||||
if (syncVal === EMPTY_VALUE) set(defaultValue)
|
||||
|
||||
const t = subscription
|
||||
subscription = source$.subscribe((value: O | typeof SUSPENSE) => {
|
||||
set(value === SUSPENSE ? gV : value)
|
||||
if (value !== SUSPENSE) set(value)
|
||||
else setState(gV as any)
|
||||
}, onError)
|
||||
t.unsubscribe()
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user