@@ -41,11 +41,13 @@ function emitEvent(handlerFunctionName, arg1, arg2, arg3, arg4, arg5) {
41
41
42
42
var isProfiling = false ;
43
43
var flushHistory = [ ] ;
44
+ var lifeCycleTimerStack = [ ] ;
44
45
var currentFlushNesting = 0 ;
45
46
var currentFlushMeasurements = null ;
46
47
var currentFlushStartTime = null ;
47
48
var currentTimerDebugID = null ;
48
49
var currentTimerStartTime = null ;
50
+ var currentTimerNestedFlushDuration = null ;
49
51
var currentTimerType = null ;
50
52
51
53
function clearHistory ( ) {
@@ -103,6 +105,72 @@ function checkDebugID(debugID) {
103
105
warning ( debugID , 'ReactDebugTool: debugID may not be empty.' ) ;
104
106
}
105
107
108
+ function beginLifeCycleTimer ( debugID , timerType ) {
109
+ if ( ! isProfiling || currentFlushNesting === 0 ) {
110
+ return ;
111
+ }
112
+ warning (
113
+ ! currentTimerType ,
114
+ 'There is an internal error in the React performance measurement code. ' +
115
+ 'Did not expect %s timer to start while %s timer is still in ' +
116
+ 'progress for %s instance.' ,
117
+ timerType ,
118
+ currentTimerType || 'no' ,
119
+ ( debugID === currentTimerDebugID ) ? 'the same' : 'another'
120
+ ) ;
121
+ currentTimerStartTime = performanceNow ( ) ;
122
+ currentTimerNestedFlushDuration = 0 ;
123
+ currentTimerDebugID = debugID ;
124
+ currentTimerType = timerType ;
125
+ }
126
+
127
+ function endLifeCycleTimer ( debugID , timerType ) {
128
+ if ( ! isProfiling || currentFlushNesting === 0 ) {
129
+ return ;
130
+ }
131
+ warning (
132
+ currentTimerType === timerType ,
133
+ 'There is an internal error in the React performance measurement code. ' +
134
+ 'We did not expect %s timer to stop while %s timer is still in ' +
135
+ 'progress for %s instance. Please report this as a bug in React.' ,
136
+ timerType ,
137
+ currentTimerType || 'no' ,
138
+ ( debugID === currentTimerDebugID ) ? 'the same' : 'another'
139
+ ) ;
140
+ currentFlushMeasurements . push ( {
141
+ timerType,
142
+ instanceID : debugID ,
143
+ duration : performanceNow ( ) - currentTimerStartTime - currentTimerNestedFlushDuration ,
144
+ } ) ;
145
+ currentTimerStartTime = null ;
146
+ currentTimerNestedFlushDuration = null ;
147
+ currentTimerDebugID = null ;
148
+ currentTimerType = null ;
149
+ }
150
+
151
+ function pauseCurrentLifeCycleTimer ( ) {
152
+ var currentTimer = {
153
+ startTime : currentTimerStartTime ,
154
+ nestedFlushStartTime : performanceNow ( ) ,
155
+ debugID : currentTimerDebugID ,
156
+ timerType : currentTimerType ,
157
+ } ;
158
+ lifeCycleTimerStack . push ( currentTimer ) ;
159
+ currentTimerStartTime = null ;
160
+ currentTimerNestedFlushDuration = null ;
161
+ currentTimerDebugID = null ;
162
+ currentTimerType = null ;
163
+ }
164
+
165
+ function resumeCurrentLifeCycleTimer ( ) {
166
+ var { startTime, nestedFlushStartTime, debugID, timerType} = lifeCycleTimerStack . pop ( ) ;
167
+ var nestedFlushDuration = performanceNow ( ) - nestedFlushStartTime ;
168
+ currentTimerStartTime = startTime ;
169
+ currentTimerNestedFlushDuration += nestedFlushDuration ;
170
+ currentTimerDebugID = debugID ;
171
+ currentTimerType = timerType ;
172
+ }
173
+
106
174
var ReactDebugTool = {
107
175
addDevtool ( devtool ) {
108
176
eventHandlers . push ( devtool ) ;
@@ -148,58 +216,29 @@ var ReactDebugTool = {
148
216
if ( __DEV__ ) {
149
217
currentFlushNesting ++ ;
150
218
resetMeasurements ( ) ;
219
+ pauseCurrentLifeCycleTimer ( ) ;
151
220
}
152
221
emitEvent ( 'onBeginFlush' ) ;
153
222
} ,
154
223
onEndFlush ( ) {
155
224
if ( __DEV__ ) {
156
225
resetMeasurements ( ) ;
157
226
currentFlushNesting -- ;
227
+ resumeCurrentLifeCycleTimer ( ) ;
158
228
}
159
229
emitEvent ( 'onEndFlush' ) ;
160
230
} ,
161
231
onBeginLifeCycleTimer ( debugID , timerType ) {
162
232
checkDebugID ( debugID ) ;
163
233
emitEvent ( 'onBeginLifeCycleTimer' , debugID , timerType ) ;
164
234
if ( __DEV__ ) {
165
- if ( isProfiling && currentFlushNesting > 0 ) {
166
- warning (
167
- ! currentTimerType ,
168
- 'There is an internal error in the React performance measurement code. ' +
169
- 'Did not expect %s timer to start while %s timer is still in ' +
170
- 'progress for %s instance.' ,
171
- timerType ,
172
- currentTimerType || 'no' ,
173
- ( debugID === currentTimerDebugID ) ? 'the same' : 'another'
174
- ) ;
175
- currentTimerStartTime = performanceNow ( ) ;
176
- currentTimerDebugID = debugID ;
177
- currentTimerType = timerType ;
178
- }
235
+ beginLifeCycleTimer ( debugID , timerType ) ;
179
236
}
180
237
} ,
181
238
onEndLifeCycleTimer ( debugID , timerType ) {
182
239
checkDebugID ( debugID ) ;
183
240
if ( __DEV__ ) {
184
- if ( isProfiling && currentFlushNesting > 0 ) {
185
- warning (
186
- currentTimerType === timerType ,
187
- 'There is an internal error in the React performance measurement code. ' +
188
- 'We did not expect %s timer to stop while %s timer is still in ' +
189
- 'progress for %s instance. Please report this as a bug in React.' ,
190
- timerType ,
191
- currentTimerType || 'no' ,
192
- ( debugID === currentTimerDebugID ) ? 'the same' : 'another'
193
- ) ;
194
- currentFlushMeasurements . push ( {
195
- timerType,
196
- instanceID : debugID ,
197
- duration : performanceNow ( ) - currentTimerStartTime ,
198
- } ) ;
199
- currentTimerStartTime = null ;
200
- currentTimerDebugID = null ;
201
- currentTimerType = null ;
202
- }
241
+ endLifeCycleTimer ( debugID , timerType ) ;
203
242
}
204
243
emitEvent ( 'onEndLifeCycleTimer' , debugID , timerType ) ;
205
244
} ,
0 commit comments