-
Notifications
You must be signed in to change notification settings - Fork 49k
feat[devtools/extension]: show disclaimer when page doesnt run react and refactor react polling logic #27373
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
packages/react-devtools-extensions/src/main/reactPolling.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* global chrome */ | ||
|
||
class CouldNotFindReactOnThePageError extends Error { | ||
constructor() { | ||
super("Could not find React, or it hasn't been loaded yet"); | ||
|
||
// Maintains proper stack trace for where our error was thrown (only available on V8) | ||
if (Error.captureStackTrace) { | ||
Error.captureStackTrace(this, CouldNotFindReactOnThePageError); | ||
} | ||
|
||
this.name = 'CouldNotFindReactOnThePageError'; | ||
} | ||
} | ||
|
||
export function startReactPolling( | ||
onReactFound, | ||
attemptsThreshold, | ||
onCouldNotFindReactAfterReachingAttemptsThreshold, | ||
) { | ||
let status = 'idle'; | ||
|
||
function abort() { | ||
status = 'aborted'; | ||
} | ||
|
||
// This function will call onSuccess only if React was found and polling is not aborted, onError will be called for every other case | ||
function checkIfReactPresentInInspectedWindow(onSuccess, onError) { | ||
chrome.devtools.inspectedWindow.eval( | ||
'window.__REACT_DEVTOOLS_GLOBAL_HOOK__ && window.__REACT_DEVTOOLS_GLOBAL_HOOK__.renderers.size > 0', | ||
(pageHasReact, exceptionInfo) => { | ||
if (status === 'aborted') { | ||
onError( | ||
'Polling was aborted, user probably navigated to the other page', | ||
); | ||
return; | ||
} | ||
|
||
if (exceptionInfo) { | ||
const {code, description, isError, isException, value} = | ||
exceptionInfo; | ||
|
||
if (isException) { | ||
onError( | ||
`Received error while checking if react has loaded: ${value}`, | ||
); | ||
return; | ||
} | ||
|
||
if (isError) { | ||
onError( | ||
`Received error with code ${code} while checking if react has loaded: "${description}"`, | ||
); | ||
return; | ||
} | ||
} | ||
|
||
if (pageHasReact) { | ||
onSuccess(); | ||
return; | ||
} | ||
|
||
onError(new CouldNotFindReactOnThePageError()); | ||
}, | ||
); | ||
} | ||
|
||
// Just a Promise wrapper around `checkIfReactPresentInInspectedWindow` | ||
// returns a Promise, which will resolve only if React has been found on the page | ||
function poll(attempt) { | ||
return new Promise((resolve, reject) => { | ||
checkIfReactPresentInInspectedWindow(resolve, reject); | ||
}).catch(error => { | ||
if (error instanceof CouldNotFindReactOnThePageError) { | ||
if (attempt === attemptsThreshold) { | ||
onCouldNotFindReactAfterReachingAttemptsThreshold(); | ||
} | ||
|
||
// Start next attempt in 0.5s | ||
return new Promise(r => setTimeout(r, 500)).then(() => | ||
poll(attempt + 1), | ||
); | ||
} | ||
|
||
// Propagating every other Error | ||
throw error; | ||
}); | ||
} | ||
|
||
poll(1) | ||
.then(onReactFound) | ||
.catch(error => { | ||
// Log propagated errors only if polling was not aborted | ||
// Some errors are expected when user performs in-tab navigation and `.eval()` is still being executed | ||
if (status === 'aborted') { | ||
return; | ||
} | ||
|
||
console.error(error); | ||
}); | ||
|
||
return {abort}; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the old version has an early return here, why remove that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, this
proxy
port is opened from content script in isolated environment, which has access towindow
object, but not DOM.This condition is usually an edge case, it means that previous port was not disconnected or we were not notified that it was disconnected. In previous implementation we would disconnect new port, which is questionable, but it could save us from situation with many new connections spam.
I think better approach in this case is disconnecting previous port properly and registering new one, connecting it with devtools page (in browser DevTools) port.