|
1 | | -import type { DarkElement, SlotProps, Callback } from '../shared'; |
| 1 | +import type { DarkElement, SlotProps, Callback, AppResource } from '../shared'; |
| 2 | +import { detectIsFunction, createError } from '../utils'; |
2 | 3 | import { __useCursor as useCursor } from '../internal'; |
3 | | -import { detectIsFunction } from '../utils'; |
| 4 | +import { useUpdate } from '../use-update'; |
4 | 5 | import { useEffect } from '../use-effect'; |
5 | 6 | import { component } from '../component'; |
6 | 7 | import { useState } from '../use-state'; |
7 | 8 | import { useEvent } from '../use-event'; |
| 9 | +import { useMemo } from '../use-memo'; |
| 10 | +import { $$scope } from '../scope'; |
8 | 11 |
|
9 | | -function useError(): [Error | null, Callback] { |
| 12 | +function useError(__id?: number): [Error | null, Callback] { |
10 | 13 | const cursor = useCursor(); |
11 | | - const [error, setError] = useState<Error>(null); |
12 | | - const reset = useEvent(() => setError(null)); |
| 14 | + const $scope = $$scope(); |
| 15 | + const inBoundary = cursor.parent?.hook?.getIsBoundary(); |
| 16 | + const res = inBoundary ? $scope.getResource(__id) : null; |
| 17 | + const [error, setError] = useState<Error>(() => (inBoundary ? init(res) : null)); |
| 18 | + const reset = useEvent(() => { |
| 19 | + inBoundary && $scope.setResource(__id, null); |
| 20 | + setError(null); |
| 21 | + }); |
13 | 22 |
|
14 | | - cursor.hook.setCatch(setError); |
| 23 | + if (inBoundary) { |
| 24 | + cursor.parent.hook.setCatch(setError); |
| 25 | + } else { |
| 26 | + cursor.hook.setCatch(setError); |
| 27 | + } |
15 | 28 |
|
16 | 29 | return [error, reset]; |
17 | 30 | } |
18 | 31 |
|
| 32 | +const init = (res: AppResource) => (res?.[1] ? createError(res?.[1]) : null); |
| 33 | + |
19 | 34 | type ErrorBoundaryProps = { |
20 | 35 | fallback?: DarkElement; |
21 | 36 | renderFallback?: (x: RenderFallbackOptions) => DarkElement; |
22 | 37 | onError?: (e: Error) => void; |
23 | 38 | } & Required<SlotProps>; |
24 | 39 |
|
25 | 40 | const ErrorBoundary = component<ErrorBoundaryProps>( |
26 | | - ({ fallback = null, renderFallback, onError, slot }) => { |
27 | | - const [error, reset] = useError(); |
| 41 | + props => { |
| 42 | + const cursor = useCursor(); |
| 43 | + const update = useUpdate(); |
| 44 | + const $scope = $$scope(); |
| 45 | + const id = useMemo(() => $scope.getNextResourceId(), []); |
| 46 | + |
| 47 | + cursor.hook.setIsBoundary(true); |
| 48 | + cursor.hook.setUpdate(update); |
| 49 | + cursor.hook.setLevel($scope.getMountLevel()); |
| 50 | + cursor.hook.setResId(id); |
| 51 | + |
| 52 | + return <ErrorBoundaryInternal {...props} id={id} />; |
| 53 | + }, |
| 54 | + { |
| 55 | + displayName: 'ErrorBoundary', |
| 56 | + }, |
| 57 | +); |
| 58 | + |
| 59 | +type ErrorBoundaryInternalProps = { |
| 60 | + id: number; |
| 61 | +} & ErrorBoundaryProps; |
| 62 | + |
| 63 | +const ErrorBoundaryInternal = component<ErrorBoundaryInternalProps>( |
| 64 | + ({ id, fallback = null, renderFallback, onError, slot }) => { |
| 65 | + const [error, reset] = useError(id); |
28 | 66 |
|
29 | 67 | useEffect(() => { |
30 | 68 | detectIsFunction(onError) && onError(error); |
31 | 69 | }, [error]); |
32 | 70 |
|
33 | 71 | return error ? (detectIsFunction(renderFallback) ? renderFallback({ error, reset }) : fallback) : slot; |
34 | 72 | }, |
35 | | - { displayName: 'ErrorBoundary' }, |
| 73 | + { displayName: 'ErrorBoundaryInternal' }, |
36 | 74 | ); |
37 | 75 |
|
38 | 76 | type RenderFallbackOptions = { |
|
0 commit comments