Skip to content

Commit

Permalink
fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
pasyukevich committed Jul 18, 2024
1 parent 3ffa6de commit c3cbd78
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5140,6 +5140,7 @@ const CONST = {
SESSION_STORAGE_KEYS: {
INITIAL_URL: 'INITIAL_URL',
ACTIVE_WORKSPACE_ID: 'ACTIVE_WORKSPACE_ID',
RETRY_LAZY_REFRESHED: 'RETRY_LAZY_REFRESHED',
},

RESERVATION_TYPE: {
Expand Down
5 changes: 2 additions & 3 deletions src/libs/Navigation/AppNavigator/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import React, {lazy, memo, Suspense, useContext, useEffect} from 'react';
import {NativeModules} from 'react-native';
import {InitialURLContext} from '@components/InitialURLContextProvider';
import Navigation from '@libs/Navigation/Navigation';
import type {EmptyObject} from '@src/types/utils/EmptyObject';
import lazyRetry from '@src/utils/lazyRetry';

const AuthScreens = lazy(() => lazyRetry<EmptyObject>(() => import('./AuthScreens')));
const PublicScreens = lazy(() => lazyRetry<EmptyObject>(() => import('./PublicScreens')));
const AuthScreens = lazy(() => lazyRetry(() => import('./AuthScreens')));
const PublicScreens = lazy(() => lazyRetry(() => import('./PublicScreens')));

type AppNavigatorProps = {
/** If we have an authToken this is true */
Expand Down
21 changes: 13 additions & 8 deletions src/utils/lazyRetry.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
import type {ComponentType} from 'react';
import CONST from '@src/CONST';

type ComponentImport<T> = () => Promise<{default: ComponentType<T>}>;
type Import<T> = Promise<{default: T}>;
type ComponentImport<T> = () => Import<T>;

/**
* Attempts to lazily import a React component with a retry mechanism on failure.
* If the initial import fails with a `ChunkLoadError`, the function will refresh the page once and retry the import.
* If the initial import fails the function will refresh the page once and retry the import.
* If the import fails again after the refresh, the error is propagated.
*
* @param componentImport - A function that returns a promise resolving to a lazily imported React component.
* @returns A promise that resolves to the imported component or rejects with an error after a retry attempt.
*/
const lazyRetry = function <T>(componentImport: ComponentImport<T>): Promise<{default: ComponentType<T>}> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const lazyRetry = function <T extends ComponentType<any>>(componentImport: ComponentImport<T>): Import<T> {
return new Promise((resolve, reject) => {
// Retrieve the retry status from sessionStorage, defaulting to 'false' if not set
const hasRefreshed: unknown = JSON.parse(sessionStorage.getItem('retry-lazy-refreshed') ?? 'false');
const hasRefreshed = JSON.parse(sessionStorage.getItem(CONST.SESSION_STORAGE_KEYS.RETRY_LAZY_REFRESHED) ?? 'false') as boolean;

componentImport()
.then((component) => {
// Reset the retry status to 'false' on successful import
sessionStorage.setItem('retry-lazy-refreshed', 'false'); // success so reset the refresh
sessionStorage.setItem(CONST.SESSION_STORAGE_KEYS.RETRY_LAZY_REFRESHED, 'false'); // success so reset the refresh
resolve(component);
})
.catch((error) => {
.catch((component: ComponentImport<T>) => {
if (!hasRefreshed) {
console.error('Failed to lazily import a React component, refreshing the page in order to retry the operation.', component);
// Set the retry status to 'true' and refresh the page
sessionStorage.setItem('retry-lazy-refreshed', 'true');
sessionStorage.setItem(CONST.SESSION_STORAGE_KEYS.RETRY_LAZY_REFRESHED, 'true');
window.location.reload(); // Refresh the page to retry the import
} else {
console.error('Failed to lazily import a React component after the retry operation!', component);
// If the import fails again reject with the error to trigger default error handling
reject(error);
reject(component);
}
});
});
Expand Down

0 comments on commit c3cbd78

Please sign in to comment.