-
Notifications
You must be signed in to change notification settings - Fork 668
/
Copy pathget-component-calls.tsx
63 lines (51 loc) · 1.49 KB
/
get-component-calls.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
declare global {
interface Window {
__REACT_DEVTOOLS_GLOBAL_HOOK__?: any
}
}
let internals: any
function enhanceExistingHook(existingHook: any) {
const originalInject = existingHook.inject
existingHook.inject = (injectedInternals: any) => {
internals = injectedInternals
// Returning a number as React expects a renderer ID
return originalInject?.call(existingHook, injectedInternals) ?? 1
}
return existingHook
}
function createMinimalHook() {
return {
supportsFiber: true,
inject: (injectedInternals: any) => {
internals = injectedInternals
return 1 // Returning a number as React expects a renderer ID
},
onCommitFiberRoot: () => {},
onCommitFiberUnmount: () => {},
}
}
export async function getComponentCalls(cb: () => void | Promise<void>) {
const componentNames: Array<string> = []
if (!internals) {
throw new Error('🚨 React DevTools is not available')
}
internals.enableProfilerTimer = true
internals.enableProfilerCommitHooks = true
internals.injectProfilingHooks({
markComponentRenderStarted: (fiber: any) => {
componentNames.push(fiber.type.name || 'Anonymous')
},
})
await cb()
internals.enableProfilerTimer = false
internals.enableProfilerCommitHooks = false
internals.injectProfilingHooks(null)
return componentNames
}
if (window.__REACT_DEVTOOLS_GLOBAL_HOOK__) {
window.__REACT_DEVTOOLS_GLOBAL_HOOK__ = enhanceExistingHook(
window.__REACT_DEVTOOLS_GLOBAL_HOOK__,
)
} else {
window.__REACT_DEVTOOLS_GLOBAL_HOOK__ = createMinimalHook()
}