Skip to content

Commit

Permalink
Syntax errors should dismiss redboxes
Browse files Browse the repository at this point in the history
Summary: If you make a syntax error while there is a redbox while Fast Refresh is on, we should dismiss that redbox. Otherwise there is no way for you to tell why your code is not working.

Reviewed By: rickhanlonii

Differential Revision: D15970337

fbshipit-source-id: 1ca6c9a1b2269d198ae726d3b64e5c51506503db
  • Loading branch information
gaearon authored and facebook-github-bot committed Jun 24, 2019
1 parent 0a17699 commit 7d2a95d
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions Libraries/Utilities/HMRClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ Error: ${e.message}`;
});

// This is intentionally called lazily, as these values change.
function shouldProvideVisualFeedback() {
function isFastRefreshActive() {
return (
// Until we get "connection-done", messages aren't real edits.
didFinishInitialUpdate &&
Expand All @@ -151,27 +151,31 @@ Error: ${e.message}`;
);
}

function dismissRedbox() {
if (
Platform.OS === 'ios' &&
NativeRedBox != null &&
NativeRedBox.dismiss != null
) {
NativeRedBox.dismiss();
} else {
const NativeExceptionsManager = require('../Core/NativeExceptionsManager')
.default;
NativeExceptionsManager &&
NativeExceptionsManager.dismissRedbox &&
NativeExceptionsManager.dismissRedbox();
}
}

hmrClient.on('update-start', () => {
if (shouldProvideVisualFeedback()) {
if (isFastRefreshActive()) {
HMRLoadingView.showMessage('Refreshing...');
}
});

hmrClient.on('update', () => {
if (shouldProvideVisualFeedback()) {
if (
Platform.OS === 'ios' &&
NativeRedBox != null &&
NativeRedBox.dismiss != null
) {
NativeRedBox.dismiss();
} else {
const NativeExceptionsManager = require('../Core/NativeExceptionsManager')
.default;
NativeExceptionsManager &&
NativeExceptionsManager.dismissRedbox &&
NativeExceptionsManager.dismissRedbox();
}
if (isFastRefreshActive()) {
dismissRedbox();
}
});

Expand All @@ -192,7 +196,10 @@ Error: ${e.message}`;
setHMRUnavailableReason(
'The Metro server and the client are out of sync. Fast Refresh will be disabled until you reload the application.',
);
} else {
} else if (isFastRefreshActive()) {
// Even if there is already a redbox, syntax errors are more important.
// Otherwise you risk seeing a stale runtime error while a syntax error is more recent.
dismissRedbox();
throw new Error(`${data.type} ${data.message}`);
}
});
Expand Down

0 comments on commit 7d2a95d

Please sign in to comment.