Skip to content

Commit 91319c5

Browse files
committed
fix: resolve own todos, code cleanup
1 parent 39052d4 commit 91319c5

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/libs/Navigation/AppNavigator/index.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import React, {lazy, memo, Suspense, useEffect, useState} from 'react';
22
import {preload, register} from 'react-native-bundle-splitter';
3-
import lazyRetry from '@src/utils/lazyRetry';
3+
import lazyRetry, {retryImport} from '@src/utils/lazyRetry';
44

5-
// const AuthScreens = lazy(() => lazyRetry(() => import('./AuthScreens')));
65
const PublicScreens = lazy(() => lazyRetry(() => import('./PublicScreens')));
76

7+
const AUTH_SCREENS = 'AuthScreens';
88
const AuthScreens = register({
9-
name: 'AuthScreens',
10-
// TODO: add retry logic
11-
loader: () => import('./AuthScreens'),
9+
name: AUTH_SCREENS,
10+
loader: () => retryImport(() => import('./AuthScreens')),
1211
});
1312

1413
type AppNavigatorProps = {
@@ -20,10 +19,10 @@ function AppNavigator({authenticated}: AppNavigatorProps) {
2019
const [canNavigateToProtectedRoutes, setNavigateToProtectedRoutes] = useState(false);
2120

2221
useEffect(() => {
23-
// Preload Auth Screens to be sure navigator can be mounted synchronously to avoid problems
24-
// described in https://github.com/Expensify/App/issues/44600
22+
// Preload Auth Screens in advance to be sure that navigator can be mounted synchronously
23+
// to avoid problems described in https://github.com/Expensify/App/issues/44600
2524
preload()
26-
.component('AuthScreens')
25+
.component(AUTH_SCREENS)
2726
.then(() => {
2827
setNavigateToProtectedRoutes(true);
2928
});

src/utils/lazyRetry.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ type Import<T> = Promise<{default: T}>;
55
type ComponentImport<T> = () => Import<T>;
66

77
/**
8+
* Common retry mechanism for importing components.
9+
*
810
* Attempts to lazily import a React component with a retry mechanism on failure.
911
* If the initial import fails the function will refresh the page once and retry the import.
1012
* If the import fails again after the refresh, the error is propagated.
1113
*
1214
* @param componentImport - A function that returns a promise resolving to a lazily imported React component.
1315
* @returns A promise that resolves to the imported component or rejects with an error after a retry attempt.
1416
*/
15-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
16-
const lazyRetry = function <T extends ComponentType<any>>(componentImport: ComponentImport<T>): Import<T> {
17+
function retryImport<T>(componentImport: ComponentImport<T>): Import<T> {
1718
return new Promise((resolve, reject) => {
1819
// Retrieve the retry status from sessionStorage, defaulting to 'false' if not set
1920
const hasRefreshed = JSON.parse(sessionStorage.getItem(CONST.SESSION_STORAGE_KEYS.RETRY_LAZY_REFRESHED) ?? 'false') as boolean;
@@ -37,6 +38,12 @@ const lazyRetry = function <T extends ComponentType<any>>(componentImport: Compo
3738
}
3839
});
3940
});
41+
}
42+
43+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
44+
const lazyRetry = function <T extends ComponentType<any>>(componentImport: ComponentImport<T>): Import<T> {
45+
return retryImport(componentImport);
4046
};
4147

4248
export default lazyRetry;
49+
export {retryImport};

0 commit comments

Comments
 (0)