2021-07-07 09:10:28 +02:00
|
|
|
import React from 'react';
|
2021-10-27 17:07:06 +02:00
|
|
|
import * as ACTIONS from 'constants/action_types';
|
2021-07-07 09:10:28 +02:00
|
|
|
|
2021-10-27 17:07:06 +02:00
|
|
|
const RETRY_DELAY_MS = 2000;
|
|
|
|
const RETRY_ATTEMPTS = 2;
|
2021-07-07 09:10:28 +02:00
|
|
|
|
2021-10-27 17:07:06 +02:00
|
|
|
function componentLoader(lazyComponent, attemptsLeft) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
lazyComponent()
|
|
|
|
.then(resolve)
|
|
|
|
.catch((error) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
if (attemptsLeft === 1) {
|
|
|
|
window.store.dispatch({ type: ACTIONS.RELOAD_REQUIRED });
|
|
|
|
console.error(error.message); // Spew the error so users can report to us if reloading doesn't help.
|
|
|
|
} else {
|
|
|
|
componentLoader(lazyComponent, attemptsLeft - 1).then(resolve, reject);
|
|
|
|
}
|
|
|
|
}, RETRY_DELAY_MS);
|
|
|
|
});
|
2021-07-07 09:10:28 +02:00
|
|
|
});
|
2021-10-27 17:07:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function lazyImport(componentImport) {
|
|
|
|
return React.lazy(() => componentLoader(componentImport, RETRY_ATTEMPTS));
|
|
|
|
}
|