@@ -20,9 +20,8 @@ import {DidCapture} from './ReactSideEffectTags';
20
20
21
21
declare var __REACT_DEVTOOLS_GLOBAL_HOOK__: Object | void ;
22
22
23
- let onScheduleFiberRoot = null ;
24
- let onCommitFiberRoot = null ;
25
- let onCommitFiberUnmount = null ;
23
+ let rendererID = null ;
24
+ let injectedHook = null ;
26
25
let hasLoggedError = false ;
27
26
28
27
export const isDevToolsPresent =
@@ -52,66 +51,9 @@ export function injectInternals(internals: Object): boolean {
52
51
return true ;
53
52
}
54
53
try {
55
- const rendererID = hook . inject ( internals ) ;
54
+ rendererID = hook . inject ( internals ) ;
56
55
// We have successfully injected, so now it is safe to set up hooks.
57
- if ( __DEV__ ) {
58
- // Only used by Fast Refresh
59
- if ( typeof hook . onScheduleFiberRoot === 'function' ) {
60
- onScheduleFiberRoot = ( root , children ) => {
61
- try {
62
- hook . onScheduleFiberRoot ( rendererID , root , children ) ;
63
- } catch ( err ) {
64
- if ( __DEV__ && ! hasLoggedError ) {
65
- hasLoggedError = true ;
66
- console . error (
67
- 'React instrumentation encountered an error: %s' ,
68
- err ,
69
- ) ;
70
- }
71
- }
72
- } ;
73
- }
74
- }
75
- onCommitFiberRoot = ( root , expirationTime ) => {
76
- try {
77
- const didError = ( root . current . effectTag & DidCapture ) === DidCapture ;
78
- if ( enableProfilerTimer ) {
79
- const currentTime = getCurrentTime ( ) ;
80
- const priorityLevel = inferPriorityFromExpirationTime (
81
- currentTime ,
82
- expirationTime ,
83
- ) ;
84
- hook . onCommitFiberRoot ( rendererID , root , priorityLevel , didError ) ;
85
- } else {
86
- hook . onCommitFiberRoot ( rendererID , root , undefined , didError ) ;
87
- }
88
- } catch ( err ) {
89
- if ( __DEV__ ) {
90
- if ( ! hasLoggedError ) {
91
- hasLoggedError = true ;
92
- console . error (
93
- 'React instrumentation encountered an error: %s' ,
94
- err ,
95
- ) ;
96
- }
97
- }
98
- }
99
- } ;
100
- onCommitFiberUnmount = fiber => {
101
- try {
102
- hook . onCommitFiberUnmount ( rendererID , fiber ) ;
103
- } catch ( err ) {
104
- if ( __DEV__ ) {
105
- if ( ! hasLoggedError ) {
106
- hasLoggedError = true ;
107
- console . error (
108
- 'React instrumentation encountered an error: %s' ,
109
- err ,
110
- ) ;
111
- }
112
- }
113
- }
114
- } ;
56
+ injectedHook = hook ;
115
57
} catch ( err ) {
116
58
// Catch all errors because it is unsafe to throw during initialization.
117
59
if ( __DEV__ ) {
@@ -123,19 +65,64 @@ export function injectInternals(internals: Object): boolean {
123
65
}
124
66
125
67
export function onScheduleRoot ( root : FiberRoot , children : ReactNodeList ) {
126
- if ( typeof onScheduleFiberRoot === 'function' ) {
127
- onScheduleFiberRoot ( root , children ) ;
68
+ if ( __DEV__ ) {
69
+ if (
70
+ injectedHook &&
71
+ typeof injectedHook . onScheduleFiberRoot === 'function'
72
+ ) {
73
+ try {
74
+ injectedHook . onScheduleFiberRoot ( rendererID , root , children ) ;
75
+ } catch ( err ) {
76
+ if ( __DEV__ && ! hasLoggedError ) {
77
+ hasLoggedError = true ;
78
+ console . error ( 'React instrumentation encountered an error: %s' , err ) ;
79
+ }
80
+ }
81
+ }
128
82
}
129
83
}
130
84
131
85
export function onCommitRoot ( root : FiberRoot , expirationTime : ExpirationTime ) {
132
- if ( typeof onCommitFiberRoot === 'function' ) {
133
- onCommitFiberRoot ( root , expirationTime ) ;
86
+ if ( injectedHook && typeof injectedHook . onCommitFiberRoot === 'function' ) {
87
+ try {
88
+ const didError = ( root . current . effectTag & DidCapture ) === DidCapture ;
89
+ if ( enableProfilerTimer ) {
90
+ const currentTime = getCurrentTime ( ) ;
91
+ const priorityLevel = inferPriorityFromExpirationTime (
92
+ currentTime ,
93
+ expirationTime ,
94
+ ) ;
95
+ injectedHook . onCommitFiberRoot (
96
+ rendererID ,
97
+ root ,
98
+ priorityLevel ,
99
+ didError ,
100
+ ) ;
101
+ } else {
102
+ injectedHook . onCommitFiberRoot ( rendererID , root , undefined , didError ) ;
103
+ }
104
+ } catch ( err ) {
105
+ if ( __DEV__ ) {
106
+ if ( ! hasLoggedError ) {
107
+ hasLoggedError = true ;
108
+ console . error ( 'React instrumentation encountered an error: %s' , err ) ;
109
+ }
110
+ }
111
+ }
134
112
}
135
113
}
136
114
137
115
export function onCommitUnmount ( fiber : Fiber ) {
138
- if ( typeof onCommitFiberUnmount === 'function' ) {
139
- onCommitFiberUnmount ( fiber ) ;
116
+ if ( injectedHook && typeof injectedHook . onCommitFiberUnmount === 'function' ) {
117
+ try {
118
+ injectedHook . onCommitFiberUnmount ( rendererID , fiber ) ;
119
+ } catch ( err ) {
120
+ if ( __DEV__ ) {
121
+ if ( ! hasLoggedError ) {
122
+ hasLoggedError = true ;
123
+ console . error ( 'React instrumentation encountered an error: %s' , err ) ;
124
+ }
125
+ }
126
+ }
140
127
}
141
128
}
0 commit comments