chore(errors): improve error-handling logic

This commit is contained in:
Josep M Sobrepere 2020-09-03 16:20:01 +02:00
parent 729525c033
commit 60a0fbda1e
2 changed files with 8 additions and 21 deletions

View File

@ -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

View File

@ -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) {