Skip to content

Commit c4fd2a6

Browse files
chore(js-ts): Convert AsyncLoad to TypeScript
Co-Authored-By: Joseph Gross <joseph@cognition.ai> (cherry picked from commit 10af628585f336fafbf1292e160dc9528a73852a)
1 parent ad19445 commit c4fd2a6

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

src/elements/common/async-load/AsyncLoad.js renamed to src/elements/common/async-load/AsyncLoad.tsx

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,29 @@
1-
// @flow
21
import * as React from 'react';
32
import AsyncError from './AsyncError';
43
import { retryNumOfTimes } from '../../../utils/function';
54

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+
}
1110

1211
const DEFAULT_NUM_TIMES = 3;
1312
const DEFAULT_INITIAL_DELAY = 500;
1413
const DEFAULT_BACKOFF_FACTOR = 2;
1514

16-
const AsyncLoad = ({ errorComponent, fallback, loader }: Props = {}) => {
15+
const AsyncLoad = ({ errorComponent, fallback, loader }: Props) => {
1716
const lazyRetry = () =>
1817
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),
2320
DEFAULT_NUM_TIMES,
2421
DEFAULT_INITIAL_DELAY,
2522
DEFAULT_BACKOFF_FACTOR,
2623
);
2724
const LazyComponent = React.lazy(() => loader().catch(lazyRetry));
2825

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>) => (
3027
<AsyncError component={errorComponent}>
3128
<React.Suspense fallback={fallback || null}>
3229
<LazyComponent ref={ref} {...props} />

0 commit comments

Comments
 (0)