Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: initialise socket with exponential retry #446

Merged
merged 1 commit into from
Jul 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions client/ErrorOverlayEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { handleError, handleUnhandledRejection } from './utils/errorEventHandlers.js';
import formatWebpackErrors from './utils/formatWebpackErrors.js';
import runWithPatchedUrl from './utils/patchUrl.js';
import runWithRetry from './utils/retry.js';

// Setup error states
let isHotReload = false;
Expand Down Expand Up @@ -75,8 +76,11 @@ if (process.env.NODE_ENV !== 'production' && typeof window !== 'undefined') {
runWithPatchedUrl(function setupOverlay() {
// Only register if no other overlay have been registered
if (!window.__reactRefreshOverlayInjected && __react_refresh_socket__) {
// Registers handlers for compile errors
__react_refresh_socket__.init(compileMessageHandler, __resourceQuery);
// Registers handlers for compile errors with retry -
// This is to prevent mismatching injection order causing errors to be thrown
runWithRetry(function initSocket() {
__react_refresh_socket__.init(compileMessageHandler, __resourceQuery);
}, 3);
// Registers handlers for runtime errors
handleError(function handleError(error) {
hasRuntimeErrors = true;
Expand Down
20 changes: 20 additions & 0 deletions client/utils/retry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function runWithRetry(callback, maxRetries) {
function executeWithRetryAndTimeout(currentCount) {
try {
if (currentCount > maxRetries - 1) {
console.warn('[React Refresh] Failed set up the socket connection.');
return;
}

callback();
} catch (err) {
setTimeout(function () {
executeWithRetryAndTimeout(currentCount + 1);
}, Math.pow(10, currentCount));
}
}

executeWithRetryAndTimeout(0);
}

export default runWithRetry;