mirror of
https://github.com/re-rxjs/react-rxjs.git
synced 2025-12-08 18:01:51 +00:00
chore(errors): improve error-handling logic
This commit is contained in:
parent
729525c033
commit
60a0fbda1e
@ -58,7 +58,7 @@ const reactEnhancer = <T>(source$: Observable<T>): BehaviorObservable<T> => {
|
||||
timeoutToken = setTimeout(() => {
|
||||
error = EMPTY_VALUE
|
||||
}, 50)
|
||||
throw error
|
||||
return error
|
||||
}
|
||||
|
||||
try {
|
||||
@ -70,7 +70,6 @@ const reactEnhancer = <T>(source$: Observable<T>): BehaviorObservable<T> => {
|
||||
if (promise) return promise
|
||||
|
||||
let value = EMPTY_VALUE
|
||||
let isSyncError = false
|
||||
promise = {
|
||||
type: "s",
|
||||
payload: result
|
||||
@ -82,7 +81,7 @@ const reactEnhancer = <T>(source$: Observable<T>): BehaviorObservable<T> => {
|
||||
value = v
|
||||
},
|
||||
error(e) {
|
||||
error = e
|
||||
error = { type: "e", payload: e }
|
||||
timeoutToken = setTimeout(() => {
|
||||
error = EMPTY_VALUE
|
||||
}, 50)
|
||||
@ -90,10 +89,7 @@ const reactEnhancer = <T>(source$: Observable<T>): BehaviorObservable<T> => {
|
||||
}),
|
||||
)
|
||||
.toPromise()
|
||||
.catch((e) => {
|
||||
if (isSyncError) return
|
||||
throw e
|
||||
})
|
||||
.catch(() => {})
|
||||
.finally(() => {
|
||||
promise = undefined
|
||||
valueResult = undefined
|
||||
@ -105,8 +101,7 @@ const reactEnhancer = <T>(source$: Observable<T>): BehaviorObservable<T> => {
|
||||
}
|
||||
|
||||
if (error !== EMPTY_VALUE) {
|
||||
isSyncError = true
|
||||
throw error
|
||||
return error
|
||||
}
|
||||
|
||||
return promise
|
||||
|
||||
@ -10,15 +10,10 @@ type Action = "e" | "v" | "s"
|
||||
const reducer = (
|
||||
current: { type: Action; payload: any },
|
||||
action: { type: Action; payload: any },
|
||||
) => {
|
||||
if (action.type === ERROR) {
|
||||
throw action.payload
|
||||
}
|
||||
return Object.is(current.payload, action.payload) &&
|
||||
current.type === action.type
|
||||
) =>
|
||||
Object.is(current.payload, action.payload) && current.type === action.type
|
||||
? current
|
||||
: action
|
||||
}
|
||||
|
||||
const init = (source$: BehaviorObservable<any>) => source$.getValue()
|
||||
|
||||
@ -26,13 +21,10 @@ export const useObservable = <O>(
|
||||
source$: BehaviorObservable<O>,
|
||||
): Exclude<O, typeof SUSPENSE> => {
|
||||
const [state, dispatch] = useReducer(reducer, source$, init)
|
||||
if (state.type === ERROR) throw state.payload
|
||||
|
||||
useEffect(() => {
|
||||
try {
|
||||
dispatch(source$.getValue())
|
||||
} catch (e) {
|
||||
return dispatch({ type: ERROR, payload: e })
|
||||
}
|
||||
dispatch(source$.getValue())
|
||||
const subscription = source$.subscribe(
|
||||
(value) => {
|
||||
if ((value as any) === SUSPENSE) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user