GeezerVison is showing github.com — DevTools: Add post-commit hook (#21183) · react/react@b38ac13
File treeExpand file treeCollapse file tree Open diff view settings react-devtools-shared/src react-devtools-shell/src/app/InteractionTracing
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +144Lines changed: 144 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +/** | | 2 | + * Copyright (c) Facebook, Inc. and its affiliates. | | 3 | + * | | 4 | + * This source code is licensed under the MIT license found in the | | 5 | + * LICENSE file in the root directory of this source tree. | | 6 | + * | | 7 | + * @flow | | 8 | + */ | | 9 | + | | 10 | +describe('profiling HostRoot', () => { | | 11 | + let React; | | 12 | + let ReactDOM; | | 13 | + let Scheduler; | | 14 | + let store: Store; | | 15 | + let utils; | | 16 | + let getEffectDurations; | | 17 | + | | 18 | + let effectDurations; | | 19 | + let passiveEffectDurations; | | 20 | + | | 21 | + beforeEach(() => { | | 22 | + utils = require('./utils'); | | 23 | + utils.beforeEachProfiling(); | | 24 | + | | 25 | + getEffectDurations = require('../backend/utils').getEffectDurations; | | 26 | + | | 27 | + store = global.store; | | 28 | + | | 29 | + React = require('react'); | | 30 | + ReactDOM = require('react-dom'); | | 31 | + Scheduler = require('scheduler'); | | 32 | + | | 33 | + effectDurations = []; | | 34 | + passiveEffectDurations = []; | | 35 | + | | 36 | + // This is the DevTools hook installed by the env.beforEach() | | 37 | + // The hook is installed as a read-only property on the window, | | 38 | + // so for our test purposes we can just override the commit hook. | | 39 | + const hook = global.__REACT_DEVTOOLS_GLOBAL_HOOK__; | | 40 | + hook.onPostCommitFiberRoot = function onPostCommitFiberRoot( | | 41 | + rendererID, | | 42 | + root, | | 43 | + ) { | | 44 | + const {effectDuration, passiveEffectDuration} = getEffectDurations(root); | | 45 | + effectDurations.push(effectDuration); | | 46 | + passiveEffectDurations.push(passiveEffectDuration); | | 47 | + }; | | 48 | + }); | | 49 | + | | 50 | + it('should expose passive and layout effect durations for render()', () => { | | 51 | + function App() { | | 52 | + React.useEffect(() => { | | 53 | + Scheduler.unstable_advanceTime(10); | | 54 | + }); | | 55 | + React.useLayoutEffect(() => { | | 56 | + Scheduler.unstable_advanceTime(100); | | 57 | + }); | | 58 | + return null; | | 59 | + } | | 60 | + | | 61 | + utils.act(() => store.profilerStore.startProfiling()); | | 62 | + utils.act(() => { | | 63 | + const container = document.createElement('div'); | | 64 | + ReactDOM.render(<App />, container); | | 65 | + }); | | 66 | + utils.act(() => store.profilerStore.stopProfiling()); | | 67 | + | | 68 | + expect(effectDurations).toHaveLength(1); | | 69 | + const effectDuration = effectDurations[0]; | | 70 | + expect(effectDuration === null || effectDuration === 100).toBe(true); | | 71 | + expect(passiveEffectDurations).toHaveLength(1); | | 72 | + const passiveEffectDuration = passiveEffectDurations[0]; | | 73 | + expect(passiveEffectDuration === null || passiveEffectDuration === 10).toBe( | | 74 | + true, | | 75 | + ); | | 76 | + }); | | 77 | + | | 78 | + it('should expose passive and layout effect durations for createRoot()', () => { | | 79 | + function App() { | | 80 | + React.useEffect(() => { | | 81 | + Scheduler.unstable_advanceTime(10); | | 82 | + }); | | 83 | + React.useLayoutEffect(() => { | | 84 | + Scheduler.unstable_advanceTime(100); | | 85 | + }); | | 86 | + return null; | | 87 | + } | | 88 | + | | 89 | + utils.act(() => store.profilerStore.startProfiling()); | | 90 | + utils.act(() => { | | 91 | + const container = document.createElement('div'); | | 92 | + const root = ReactDOM.unstable_createRoot(container); | | 93 | + root.render(<App />); | | 94 | + }); | | 95 | + utils.act(() => store.profilerStore.stopProfiling()); | | 96 | + | | 97 | + expect(effectDurations).toHaveLength(1); | | 98 | + const effectDuration = effectDurations[0]; | | 99 | + expect(effectDuration === null || effectDuration === 100).toBe(true); | | 100 | + expect(passiveEffectDurations).toHaveLength(1); | | 101 | + const passiveEffectDuration = passiveEffectDurations[0]; | | 102 | + expect(passiveEffectDuration === null || passiveEffectDuration === 10).toBe( | | 103 | + true, | | 104 | + ); | | 105 | + }); | | 106 | + | | 107 | + it('should properly reset passive and layout effect durations between commits', () => { | | 108 | + function App({shouldCascade}) { | | 109 | + const [, setState] = React.useState(false); | | 110 | + React.useEffect(() => { | | 111 | + Scheduler.unstable_advanceTime(10); | | 112 | + }); | | 113 | + React.useLayoutEffect(() => { | | 114 | + Scheduler.unstable_advanceTime(100); | | 115 | + }); | | 116 | + React.useLayoutEffect(() => { | | 117 | + if (shouldCascade) { | | 118 | + setState(true); | | 119 | + } | | 120 | + }, [shouldCascade]); | | 121 | + return null; | | 122 | + } | | 123 | + | | 124 | + const container = document.createElement('div'); | | 125 | + const root = ReactDOM.unstable_createRoot(container); | | 126 | + | | 127 | + utils.act(() => store.profilerStore.startProfiling()); | | 128 | + utils.act(() => root.render(<App />)); | | 129 | + utils.act(() => root.render(<App shouldCascade={true} />)); | | 130 | + utils.act(() => store.profilerStore.stopProfiling()); | | 131 | + | | 132 | + expect(effectDurations).toHaveLength(3); | | 133 | + expect(passiveEffectDurations).toHaveLength(3); | | 134 | + | | 135 | + for (let i = 0; i < effectDurations.length; i++) { | | 136 | + const effectDuration = effectDurations[i]; | | 137 | + expect(effectDuration === null || effectDuration === 100).toBe(true); | | 138 | + const passiveEffectDuration = passiveEffectDurations[i]; | | 139 | + expect( | | 140 | + passiveEffectDuration === null || passiveEffectDuration === 10, | | 141 | + ).toBe(true); | | 142 | + } | | 143 | + }); | | 144 | +}); |
Collapse file Copy file name to clipboardExpand all lines: packages/react-devtools-shared/src/backend/legacy/renderer.js +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1012,6 +1012,9 @@ export function attach( | 1012 | 1012 | const handleCommitFiberUnmount = () => { | 1013 | 1013 | throw new Error('handleCommitFiberUnmount not supported by this renderer'); | 1014 | 1014 | }; | | 1015 | + const handlePostCommitFiberRoot = () => { | | 1016 | + throw new Error('handlePostCommitFiberRoot not supported by this renderer'); | | 1017 | + }; | 1015 | 1018 | const overrideSuspense = () => { | 1016 | 1019 | throw new Error('overrideSuspense not supported by this renderer'); | 1017 | 1020 | }; | @@ -1082,6 +1085,7 @@ export function attach( | 1082 | 1085 | getProfilingData, | 1083 | 1086 | handleCommitFiberRoot, | 1084 | 1087 | handleCommitFiberUnmount, | | 1088 | + handlePostCommitFiberRoot, | 1085 | 1089 | inspectElement, | 1086 | 1090 | logElementToConsole, | 1087 | 1091 | overrideSuspense, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-devtools-shared/src/backend/renderer.js +25-40Lines changed: 25 additions & 40 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -42,6 +42,7 @@ import { | 42 | 42 | copyWithDelete, | 43 | 43 | copyWithRename, | 44 | 44 | copyWithSet, | | 45 | + getEffectDurations, | 45 | 46 | } from './utils'; | 46 | 47 | import { | 47 | 48 | __DEBUG__, | @@ -369,6 +370,7 @@ export function getInternalReactConstants( | 369 | 370 | LegacyHiddenComponent, | 370 | 371 | MemoComponent, | 371 | 372 | OffscreenComponent, | | 373 | + Profiler, | 372 | 374 | ScopeComponent, | 373 | 375 | SimpleMemoComponent, | 374 | 376 | SuspenseComponent, | @@ -442,6 +444,8 @@ export function getInternalReactConstants( | 442 | 444 | return 'Scope'; | 443 | 445 | case SuspenseListComponent: | 444 | 446 | return 'SuspenseList'; | | 447 | + case Profiler: | | 448 | + return 'Profiler'; | 445 | 449 | default: | 446 | 450 | const typeSymbol = getTypeSymbol(type); | 447 | 451 |
| @@ -2154,25 +2158,6 @@ export function attach( | 2154 | 2158 | // Checking root.memoizedInteractions handles multi-renderer edge-case- | 2155 | 2159 | // where some v16 renderers support profiling and others don't. | 2156 | 2160 | if (isProfiling && root.memoizedInteractions != null) { | 2157 | | - // Profiling durations are only available for certain builds. | 2158 | | - // If available, they'll be stored on the HostRoot. | 2159 | | - let effectDuration = null; | 2160 | | - let passiveEffectDuration = null; | 2161 | | - const hostRoot = root.current; | 2162 | | - if (hostRoot != null) { | 2163 | | - const stateNode = hostRoot.stateNode; | 2164 | | - if (stateNode != null) { | 2165 | | - effectDuration = | 2166 | | - stateNode.effectDuration != null | 2167 | | - ? stateNode.effectDuration | 2168 | | - : null; | 2169 | | - passiveEffectDuration = | 2170 | | - stateNode.passiveEffectDuration != null | 2171 | | - ? stateNode.passiveEffectDuration | 2172 | | - : null; | 2173 | | - } | 2174 | | - } | 2175 | | - | 2176 | 2161 | // If profiling is active, store commit time and duration, and the current interactions. | 2177 | 2162 | // The frontend may request this information after profiling has stopped. | 2178 | 2163 | currentCommitProfilingMetadata = { | @@ -2187,8 +2172,8 @@ export function attach( | 2187 | 2172 | ), | 2188 | 2173 | maxActualDuration: 0, | 2189 | 2174 | priorityLevel: null, | 2190 | | - effectDuration, | 2191 | | - passiveEffectDuration, | | 2175 | + effectDuration: null, | | 2176 | + passiveEffectDuration: null, | 2192 | 2177 | }; | 2193 | 2178 | } | 2194 | 2179 |
| @@ -2206,6 +2191,19 @@ export function attach( | 2206 | 2191 | recordUnmount(fiber, false); | 2207 | 2192 | } | 2208 | 2193 |
| | 2194 | + function handlePostCommitFiberRoot(root) { | | 2195 | + const isProfilingSupported = root.memoizedInteractions != null; | | 2196 | + if (isProfiling && isProfilingSupported) { | | 2197 | + if (currentCommitProfilingMetadata !== null) { | | 2198 | + const {effectDuration, passiveEffectDuration} = getEffectDurations( | | 2199 | + root, | | 2200 | + ); | | 2201 | + currentCommitProfilingMetadata.effectDuration = effectDuration; | | 2202 | + currentCommitProfilingMetadata.passiveEffectDuration = passiveEffectDuration; | | 2203 | + } | | 2204 | + } | | 2205 | + } | | 2206 | + | 2209 | 2207 | function handleCommitFiberRoot(root, priorityLevel) { | 2210 | 2208 | const current = root.current; | 2211 | 2209 | const alternate = current.alternate; | @@ -2227,23 +2225,6 @@ export function attach( | 2227 | 2225 | const isProfilingSupported = root.memoizedInteractions != null; | 2228 | 2226 |
| 2229 | 2227 | if (isProfiling && isProfilingSupported) { | 2230 | | - // Profiling durations are only available for certain builds. | 2231 | | - // If available, they'll be stored on the HostRoot. | 2232 | | - let effectDuration = null; | 2233 | | - let passiveEffectDuration = null; | 2234 | | - const hostRoot = root.current; | 2235 | | - if (hostRoot != null) { | 2236 | | - const stateNode = hostRoot.stateNode; | 2237 | | - if (stateNode != null) { | 2238 | | - effectDuration = | 2239 | | - stateNode.effectDuration != null ? stateNode.effectDuration : null; | 2240 | | - passiveEffectDuration = | 2241 | | - stateNode.passiveEffectDuration != null | 2242 | | - ? stateNode.passiveEffectDuration | 2243 | | - : null; | 2244 | | - } | 2245 | | - } | 2246 | | - | 2247 | 2228 | // If profiling is active, store commit time and duration, and the current interactions. | 2248 | 2229 | // The frontend may request this information after profiling has stopped. | 2249 | 2230 | currentCommitProfilingMetadata = { | @@ -2259,8 +2240,11 @@ export function attach( | 2259 | 2240 | maxActualDuration: 0, | 2260 | 2241 | priorityLevel: | 2261 | 2242 | priorityLevel == null ? null : formatPriorityLevel(priorityLevel), | 2262 | | - effectDuration, | 2263 | | - passiveEffectDuration, | | 2243 | + | | 2244 | + // Initialize to null; if new enough React version is running, | | 2245 | + // these values will be read during separate handlePostCommitFiberRoot() call. | | 2246 | + effectDuration: null, | | 2247 | + passiveEffectDuration: null, | 2264 | 2248 | }; | 2265 | 2249 | } | 2266 | 2250 |
| @@ -3856,6 +3840,7 @@ export function attach( | 3856 | 3840 | getProfilingData, | 3857 | 3841 | handleCommitFiberRoot, | 3858 | 3842 | handleCommitFiberUnmount, | | 3843 | + handlePostCommitFiberRoot, | 3859 | 3844 | inspectElement, | 3860 | 3845 | logElementToConsole, | 3861 | 3846 | prepareViewAttributeSource, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-devtools-shared/src/backend/types.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -326,6 +326,7 @@ export type RendererInterface = { | 326 | 326 | getPathForElement: (id: number) => Array<PathFrame> | null, | 327 | 327 | handleCommitFiberRoot: (fiber: Object, commitPriority?: number) => void, | 328 | 328 | handleCommitFiberUnmount: (fiber: Object) => void, | | 329 | + handlePostCommitFiberRoot: (fiber: Object) => void, | 329 | 330 | inspectElement: ( | 330 | 331 | requestID: number, | 331 | 332 | id: number, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-devtools-shared/src/backend/utils.js +20Lines changed: 20 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -118,6 +118,26 @@ export function copyWithSet( | 118 | 118 | return updated; | 119 | 119 | } | 120 | 120 |
| | 121 | +export function getEffectDurations(root: Object) { | | 122 | + // Profiling durations are only available for certain builds. | | 123 | + // If available, they'll be stored on the HostRoot. | | 124 | + let effectDuration = null; | | 125 | + let passiveEffectDuration = null; | | 126 | + const hostRoot = root.current; | | 127 | + if (hostRoot != null) { | | 128 | + const stateNode = hostRoot.stateNode; | | 129 | + if (stateNode != null) { | | 130 | + effectDuration = | | 131 | + stateNode.effectDuration != null ? stateNode.effectDuration : null; | | 132 | + passiveEffectDuration = | | 133 | + stateNode.passiveEffectDuration != null | | 134 | + ? stateNode.passiveEffectDuration | | 135 | + : null; | | 136 | + } | | 137 | + } | | 138 | + return {effectDuration, passiveEffectDuration}; | | 139 | +} | | 140 | + | 121 | 141 | export function serializeToString(data: any): string { | 122 | 142 | const cache = new Set(); | 123 | 143 | // Use a custom replacer function to protect against circular references. | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-devtools-shared/src/hook.js +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -287,6 +287,13 @@ export function installHook(target: any): DevToolsHook | null { | 287 | 287 | } | 288 | 288 | } | 289 | 289 |
| | 290 | + function onPostCommitFiberRoot(rendererID, root) { | | 291 | + const rendererInterface = rendererInterfaces.get(rendererID); | | 292 | + if (rendererInterface != null) { | | 293 | + rendererInterface.handlePostCommitFiberRoot(root); | | 294 | + } | | 295 | + } | | 296 | + | 290 | 297 | // TODO: More meaningful names for "rendererInterfaces" and "renderers". | 291 | 298 | const fiberRoots = {}; | 292 | 299 | const rendererInterfaces = new Map(); | @@ -315,6 +322,7 @@ export function installHook(target: any): DevToolsHook | null { | 315 | 322 | checkDCE, | 316 | 323 | onCommitFiberUnmount, | 317 | 324 | onCommitFiberRoot, | | 325 | + onPostCommitFiberRoot, | 318 | 326 | }; | 319 | 327 |
| 320 | 328 | Object.defineProperty( | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-devtools-shell/src/app/InteractionTracing/index.js +13-1Lines changed: 13 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -21,6 +21,14 @@ import { | 21 | 21 | unstable_wrap as wrap, | 22 | 22 | } from 'scheduler/tracing'; | 23 | 23 |
| | 24 | +function sleep(ms) { | | 25 | + const start = performance.now(); | | 26 | + let now; | | 27 | + do { | | 28 | + now = performance.now(); | | 29 | + } while (now - ms < start); | | 30 | +} | | 31 | + | 24 | 32 | export default function InteractionTracing() { | 25 | 33 | const [count, setCount] = useState(0); | 26 | 34 | const [shouldCascade, setShouldCascade] = useState(false); | @@ -75,7 +83,11 @@ export default function InteractionTracing() { | 75 | 83 | }, [count, shouldCascade]); | 76 | 84 |
| 77 | 85 | useLayoutEffect(() => { | 78 | | - Math.sqrt(100 * 100 * 100 * 100 * 100); | | 86 | + sleep(150); | | 87 | + }); | | 88 | + | | 89 | + useEffect(() => { | | 90 | + sleep(300); | 79 | 91 | }); | 80 | 92 |
| 81 | 93 | return ( | |
|