-
Notifications
You must be signed in to change notification settings - Fork 49.8k
[DevTools] Use source maps to infer name asynchronously #34212
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
+80
−15
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
78 changes: 78 additions & 0 deletions
78
packages/react-devtools-shared/src/devtools/views/useInferredName.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,78 @@ | ||
| import {use, useContext, useDeferredValue} from 'react'; | ||
|
|
||
| import type {ReactCallSite} from 'shared/ReactTypes'; | ||
|
|
||
| import type {SourceMappedLocation} from 'react-devtools-shared/src/symbolicateSource'; | ||
|
|
||
| import type {SerializedAsyncInfo} from 'react-devtools-shared/src/frontend/types'; | ||
|
|
||
| import FetchFileWithCachingContext from './Components/FetchFileWithCachingContext'; | ||
|
|
||
| import {symbolicateSourceWithCache} from 'react-devtools-shared/src/symbolicateSource'; | ||
|
|
||
| export default function useInferredName( | ||
| asyncInfo: SerializedAsyncInfo, | ||
| ): string { | ||
| const fetchFileWithCaching = useContext(FetchFileWithCachingContext); | ||
| const name = asyncInfo.awaited.name; | ||
| let inferNameFromStack = null; | ||
| if (!name || name === 'Promise') { | ||
| // If all we have is a generic name, we can try to infer a better name from | ||
| // the stack. We only do this if the stack has more than one frame since | ||
| // otherwise it's likely to just be the name of the component which isn't better. | ||
| const bestStack = asyncInfo.awaited.stack || asyncInfo.stack; | ||
| if (bestStack !== null && bestStack.length > 1) { | ||
| inferNameFromStack = bestStack; | ||
| } | ||
| } | ||
| // Start by not source mapping and just taking the first name and upgrade to | ||
| // the better name asynchronously if we find one. Most of the time it'll just be | ||
| // the top of the stack. | ||
| const shouldSourceMap = useDeferredValue(inferNameFromStack !== null, false); | ||
| if (inferNameFromStack !== null) { | ||
| if (shouldSourceMap) { | ||
| let bestMatch = ''; | ||
| for (let i = 0; i < inferNameFromStack.length; i++) { | ||
| const callSite: ReactCallSite = inferNameFromStack[i]; | ||
| const [virtualFunctionName, virtualURL, virtualLine, virtualColumn] = | ||
| callSite; | ||
| const symbolicatedCallSite: null | SourceMappedLocation = | ||
| fetchFileWithCaching !== null | ||
| ? use( | ||
| symbolicateSourceWithCache( | ||
| fetchFileWithCaching, | ||
| virtualURL, | ||
| virtualLine, | ||
| virtualColumn, | ||
| ), | ||
| ) | ||
| : null; | ||
| if (symbolicatedCallSite === null) { | ||
| // If we can't source map, we treat it as first party code. We called whatever was | ||
| // the previous callsite. | ||
| if (bestMatch === '') { | ||
| return virtualFunctionName || name; | ||
| } else { | ||
| return bestMatch; | ||
| } | ||
| } else if (!symbolicatedCallSite.ignored) { | ||
| if (bestMatch === '') { | ||
| // If we had no good stack frames for internal calls, just use the last | ||
| // first party function name. | ||
| return symbolicatedCallSite[0] || virtualFunctionName || name; | ||
| } else { | ||
| return bestMatch; | ||
| } | ||
| } else { | ||
| // This line was ignore listed, it might be the one we called into from first party. | ||
| bestMatch = symbolicatedCallSite[0] || virtualFunctionName; | ||
| } | ||
| } | ||
| return name; | ||
| } else { | ||
| return inferNameFromStack[0][0]; | ||
| } | ||
| } else { | ||
| return name; | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Should we seed these Promises before? This looks like a waterfall at a glance. Or is this intentional that we don't want to sourcemap eagerly in case the very first stack is already not ignored and we'd end up throwing away the later results?