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