|
1 | | -// @flow |
2 | 1 | import * as React from 'react'; |
3 | 2 | import AsyncError from './AsyncError'; |
4 | 3 | import { retryNumOfTimes } from '../../../utils/function'; |
5 | 4 |
|
6 | | -type Props = { |
7 | | - errorComponent?: React.ComponentType<any>, |
8 | | - fallback?: React.Element<any>, |
9 | | - loader: () => Promise<any>, |
10 | | -}; |
| 5 | +interface Props { |
| 6 | + errorComponent?: React.ComponentType<{ error: Error }>; |
| 7 | + fallback?: React.ReactElement; |
| 8 | + loader: () => Promise<{ default: React.ComponentType<unknown> }>; |
| 9 | +} |
11 | 10 |
|
12 | 11 | const DEFAULT_NUM_TIMES = 3; |
13 | 12 | const DEFAULT_INITIAL_DELAY = 500; |
14 | 13 | const DEFAULT_BACKOFF_FACTOR = 2; |
15 | 14 |
|
16 | | -const AsyncLoad = ({ errorComponent, fallback, loader }: Props = {}) => { |
| 15 | +const AsyncLoad = ({ errorComponent, fallback, loader }: Props) => { |
17 | 16 | const lazyRetry = () => |
18 | 17 | retryNumOfTimes( |
19 | | - (successCallback, errorCallback) => |
20 | | - loader() |
21 | | - .then(successCallback) |
22 | | - .catch(errorCallback), |
| 18 | + (successCallback: (value: unknown) => void, errorCallback: (reason: unknown) => void) => |
| 19 | + loader().then(successCallback).catch(errorCallback), |
23 | 20 | DEFAULT_NUM_TIMES, |
24 | 21 | DEFAULT_INITIAL_DELAY, |
25 | 22 | DEFAULT_BACKOFF_FACTOR, |
26 | 23 | ); |
27 | 24 | const LazyComponent = React.lazy(() => loader().catch(lazyRetry)); |
28 | 25 |
|
29 | | - return React.forwardRef<Object, React.Ref<any>>((props: Object, ref: React.Ref<any>) => ( |
| 26 | + return React.forwardRef((props: Record<string, unknown>, ref: React.Ref<unknown>) => ( |
30 | 27 | <AsyncError component={errorComponent}> |
31 | 28 | <React.Suspense fallback={fallback || null}> |
32 | 29 | <LazyComponent ref={ref} {...props} /> |
|
0 commit comments