How to use resumeCurrentLifeCycleTimer method in Playwright Internal

Best JavaScript code snippet using playwright-internal

Run Playwright Internal automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

65b6bd2ffbe6e4c46a631afcc31212818c6d58ReactDebugTool.js

Source: 65b6bd2ffbe6e4c46a631afcc31212818c6d58ReactDebugTool.js Github

copy
1
2
3'use strict';
4
5var ReactInvalidSetStateWarningHook = require('ReactInvalidSetStateWarningHook');
6var ReactHostOperationHistoryHook = require('ReactHostOperationHistoryHook');
7var ReactComponentTreeHook = require('react/lib/ReactComponentTreeHook');
8var ExecutionEnvironment = require('fbjs/lib/ExecutionEnvironment');
9
10var performanceNow = require('fbjs/lib/performanceNow');
11var warning = require('fbjs/lib/warning');
12
13var ReactDebugTool = null;
14
15if (__DEV__) {
16  var hooks = [];
17  var didHookThrowForEvent = {};
18
19  var callHook = function callHook(event, fn, context, arg1, arg2, arg3, arg4, arg5) {
20    try {
21      fn.call(context, arg1, arg2, arg3, arg4, arg5);
22    } catch (e) {
23      warning(didHookThrowForEvent[event], 'Exception thrown by hook while handling %s: %s', event, e + '\n' + e.stack);
24      didHookThrowForEvent[event] = true;
25    }
26  };
27
28  var emitEvent = function emitEvent(event, arg1, arg2, arg3, arg4, arg5) {
29    for (var i = 0; i < hooks.length; i++) {
30      var hook = hooks[i];
31      var fn = hook[event];
32      if (fn) {
33        callHook(event, fn, hook, arg1, arg2, arg3, arg4, arg5);
34      }
35    }
36  };
37
38  var _isProfiling = false;
39  var flushHistory = [];
40  var lifeCycleTimerStack = [];
41  var currentFlushNesting = 0;
42  var currentFlushMeasurements = [];
43  var currentFlushStartTime = 0;
44  var currentTimerDebugID = null;
45  var currentTimerStartTime = 0;
46  var currentTimerNestedFlushDuration = 0;
47  var currentTimerType = null;
48
49  var lifeCycleTimerHasWarned = false;
50
51  var clearHistory = function clearHistory() {
52    ReactComponentTreeHook.purgeUnmountedComponents();
53    ReactHostOperationHistoryHook.clearHistory();
54  };
55
56  var getTreeSnapshot = function getTreeSnapshot(registeredIDs) {
57    return registeredIDs.reduce(function (tree, id) {
58      var ownerID = ReactComponentTreeHook.getOwnerID(id);
59      var parentID = ReactComponentTreeHook.getParentID(id);
60      tree[id] = {
61        displayName: ReactComponentTreeHook.getDisplayName(id),
62        text: ReactComponentTreeHook.getText(id),
63        updateCount: ReactComponentTreeHook.getUpdateCount(id),
64        childIDs: ReactComponentTreeHook.getChildIDs(id),
65
66        ownerID: ownerID || parentID && ReactComponentTreeHook.getOwnerID(parentID) || 0,
67        parentID: parentID
68      };
69      return tree;
70    }, {});
71  };
72
73  var resetMeasurements = function resetMeasurements() {
74    var previousStartTime = currentFlushStartTime;
75    var previousMeasurements = currentFlushMeasurements;
76    var previousOperations = ReactHostOperationHistoryHook.getHistory();
77
78    if (currentFlushNesting === 0) {
79      currentFlushStartTime = 0;
80      currentFlushMeasurements = [];
81      clearHistory();
82      return;
83    }
84
85    if (previousMeasurements.length || previousOperations.length) {
86      var registeredIDs = ReactComponentTreeHook.getRegisteredIDs();
87      flushHistory.push({
88        duration: performanceNow() - previousStartTime,
89        measurements: previousMeasurements || [],
90        operations: previousOperations || [],
91        treeSnapshot: getTreeSnapshot(registeredIDs)
92      });
93    }
94
95    clearHistory();
96    currentFlushStartTime = performanceNow();
97    currentFlushMeasurements = [];
98  };
99
100  var checkDebugID = function checkDebugID(debugID) {
101    var allowRoot = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
102
103    if (allowRoot && debugID === 0) {
104      return;
105    }
106    if (!debugID) {
107      warning(false, 'ReactDebugTool: debugID may not be empty.');
108    }
109  };
110
111  var beginLifeCycleTimer = function beginLifeCycleTimer(debugID, timerType) {
112    if (currentFlushNesting === 0) {
113      return;
114    }
115    if (currentTimerType && !lifeCycleTimerHasWarned) {
116      warning(false, 'There is an internal error in the React performance measurement code.' + '\n\nDid not expect %s timer to start while %s timer is still in ' + 'progress for %s instance.', timerType, currentTimerType || 'no', debugID === currentTimerDebugID ? 'the same' : 'another');
117      lifeCycleTimerHasWarned = true;
118    }
119    currentTimerStartTime = performanceNow();
120    currentTimerNestedFlushDuration = 0;
121    currentTimerDebugID = debugID;
122    currentTimerType = timerType;
123  };
124
125  var endLifeCycleTimer = function endLifeCycleTimer(debugID, timerType) {
126    if (currentFlushNesting === 0) {
127      return;
128    }
129    if (currentTimerType !== timerType && !lifeCycleTimerHasWarned) {
130      warning(false, 'There is an internal error in the React performance measurement code. ' + 'We did not expect %s timer to stop while %s timer is still in ' + 'progress for %s instance. Please report this as a bug in React.', timerType, currentTimerType || 'no', debugID === currentTimerDebugID ? 'the same' : 'another');
131      lifeCycleTimerHasWarned = true;
132    }
133    if (_isProfiling) {
134      currentFlushMeasurements.push({
135        timerType: timerType,
136        instanceID: debugID,
137        duration: performanceNow() - currentTimerStartTime - currentTimerNestedFlushDuration
138      });
139    }
140    currentTimerStartTime = 0;
141    currentTimerNestedFlushDuration = 0;
142    currentTimerDebugID = null;
143    currentTimerType = null;
144  };
145
146  var pauseCurrentLifeCycleTimer = function pauseCurrentLifeCycleTimer() {
147    var currentTimer = {
148      startTime: currentTimerStartTime,
149      nestedFlushStartTime: performanceNow(),
150      debugID: currentTimerDebugID,
151      timerType: currentTimerType
152    };
153    lifeCycleTimerStack.push(currentTimer);
154    currentTimerStartTime = 0;
155    currentTimerNestedFlushDuration = 0;
156    currentTimerDebugID = null;
157    currentTimerType = null;
158  };
159
160  var resumeCurrentLifeCycleTimer = function resumeCurrentLifeCycleTimer() {
161    var _lifeCycleTimerStack$ = lifeCycleTimerStack.pop(),
162        startTime = _lifeCycleTimerStack$.startTime,
163        nestedFlushStartTime = _lifeCycleTimerStack$.nestedFlushStartTime,
164        debugID = _lifeCycleTimerStack$.debugID,
165        timerType = _lifeCycleTimerStack$.timerType;
166
167    var nestedFlushDuration = performanceNow() - nestedFlushStartTime;
168    currentTimerStartTime = startTime;
169    currentTimerNestedFlushDuration += nestedFlushDuration;
170    currentTimerDebugID = debugID;
171    currentTimerType = timerType;
172  };
173
174  var lastMarkTimeStamp = 0;
175  var canUsePerformanceMeasure = typeof performance !== 'undefined' && typeof performance.mark === 'function' && typeof performance.clearMarks === 'function' && typeof performance.measure === 'function' && typeof performance.clearMeasures === 'function';
176
177  var shouldMark = function shouldMark(debugID) {
178    if (!_isProfiling || !canUsePerformanceMeasure) {
179      return false;
180    }
181    var element = ReactComponentTreeHook.getElement(debugID);
182    if (element == null || typeof element !== 'object') {
183      return false;
184    }
185    var isHostElement = typeof element.type === 'string';
186    if (isHostElement) {
187      return false;
188    }
189    return true;
190  };
191
192  var markBegin = function markBegin(debugID, markType) {
193    if (!shouldMark(debugID)) {
194      return;
195    }
196
197    var markName = debugID + '::' + markType;
198    lastMarkTimeStamp = performanceNow();
199    performance.mark(markName);
200  };
201
202  var markEnd = function markEnd(debugID, markType) {
203    if (!shouldMark(debugID)) {
204      return;
205    }
206
207    var markName = debugID + '::' + markType;
208    var displayName = ReactComponentTreeHook.getDisplayName(debugID) || 'Unknown';
209
210    var timeStamp = performanceNow();
211    if (timeStamp - lastMarkTimeStamp > 0.1) {
212      var measurementName = displayName + ' [' + markType + ']';
213      performance.measure(measurementName, markName);
214    }
215
216    performance.clearMarks(markName);
217    performance.clearMeasures(measurementName);
218  };
219
220  ReactDebugTool = {
221    addHook: function addHook(hook) {
222      hooks.push(hook);
223    },
224    removeHook: function removeHook(hook) {
225      for (var i = 0; i < hooks.length; i++) {
226        if (hooks[i] === hook) {
227          hooks.splice(i, 1);
228          i--;
229        }
230      }
231    },
232    isProfiling: function isProfiling() {
233      return _isProfiling;
234    },
235    beginProfiling: function beginProfiling() {
236      if (_isProfiling) {
237        return;
238      }
239
240      _isProfiling = true;
241      flushHistory.length = 0;
242      resetMeasurements();
243      ReactDebugTool.addHook(ReactHostOperationHistoryHook);
244    },
245    endProfiling: function endProfiling() {
246      if (!_isProfiling) {
247        return;
248      }
249
250      _isProfiling = false;
251      resetMeasurements();
252      ReactDebugTool.removeHook(ReactHostOperationHistoryHook);
253    },
254    getFlushHistory: function getFlushHistory() {
255      return flushHistory;
256    },
257    onBeginFlush: function onBeginFlush() {
258      currentFlushNesting++;
259      resetMeasurements();
260      pauseCurrentLifeCycleTimer();
261      emitEvent('onBeginFlush');
262    },
263    onEndFlush: function onEndFlush() {
264      resetMeasurements();
265      currentFlushNesting--;
266      resumeCurrentLifeCycleTimer();
267      emitEvent('onEndFlush');
268    },
269    onBeginLifeCycleTimer: function onBeginLifeCycleTimer(debugID, timerType) {
270      checkDebugID(debugID);
271      emitEvent('onBeginLifeCycleTimer', debugID, timerType);
272      markBegin(debugID, timerType);
273      beginLifeCycleTimer(debugID, timerType);
274    },
275    onEndLifeCycleTimer: function onEndLifeCycleTimer(debugID, timerType) {
276      checkDebugID(debugID);
277      endLifeCycleTimer(debugID, timerType);
278      markEnd(debugID, timerType);
279      emitEvent('onEndLifeCycleTimer', debugID, timerType);
280    },
281    onBeginProcessingChildContext: function onBeginProcessingChildContext() {
282      emitEvent('onBeginProcessingChildContext');
283    },
284    onEndProcessingChildContext: function onEndProcessingChildContext() {
285      emitEvent('onEndProcessingChildContext');
286    },
287    onHostOperation: function onHostOperation(operation) {
288      checkDebugID(operation.instanceID);
289      emitEvent('onHostOperation', operation);
290    },
291    onSetState: function onSetState() {
292      emitEvent('onSetState');
293    },
294    onSetChildren: function onSetChildren(debugID, childDebugIDs) {
295      checkDebugID(debugID);
296      childDebugIDs.forEach(checkDebugID);
297      emitEvent('onSetChildren', debugID, childDebugIDs);
298    },
299    onBeforeMountComponent: function onBeforeMountComponent(debugID, element, parentDebugID) {
300      checkDebugID(debugID);
301      checkDebugID(parentDebugID, true);
302      emitEvent('onBeforeMountComponent', debugID, element, parentDebugID);
303      markBegin(debugID, 'mount');
304    },
305    onMountComponent: function onMountComponent(debugID) {
306      checkDebugID(debugID);
307      markEnd(debugID, 'mount');
308      emitEvent('onMountComponent', debugID);
309    },
310    onBeforeUpdateComponent: function onBeforeUpdateComponent(debugID, element) {
311      checkDebugID(debugID);
312      emitEvent('onBeforeUpdateComponent', debugID, element);
313      markBegin(debugID, 'update');
314    },
315    onUpdateComponent: function onUpdateComponent(debugID) {
316      checkDebugID(debugID);
317      markEnd(debugID, 'update');
318      emitEvent('onUpdateComponent', debugID);
319    },
320    onBeforeUnmountComponent: function onBeforeUnmountComponent(debugID) {
321      checkDebugID(debugID);
322      emitEvent('onBeforeUnmountComponent', debugID);
323      markBegin(debugID, 'unmount');
324    },
325    onUnmountComponent: function onUnmountComponent(debugID) {
326      checkDebugID(debugID);
327      markEnd(debugID, 'unmount');
328      emitEvent('onUnmountComponent', debugID);
329    },
330    onTestEvent: function onTestEvent() {
331      emitEvent('onTestEvent');
332    }
333  };
334
335  ReactDebugTool.addHook(ReactInvalidSetStateWarningHook);
336  ReactDebugTool.addHook(ReactComponentTreeHook);
337  var url = ExecutionEnvironment.canUseDOM && window.location.href || '';
338  if (/[?&]react_perf\b/.test(url)) {
339    ReactDebugTool.beginProfiling();
340  }
341}
342
343module.exports = ReactDebugTool;
Full Screen

747110cb83c5d9948fa42e36cd4e7f19ad369eReactDebugTool.js

Source: 747110cb83c5d9948fa42e36cd4e7f19ad369eReactDebugTool.js Github

copy
1
2
3'use strict';
4
5var ReactInvalidSetStateWarningHook = require('ReactInvalidSetStateWarningHook');
6var ReactHostOperationHistoryHook = require('ReactHostOperationHistoryHook');
7var ReactComponentTreeHook = require('react/lib/ReactComponentTreeHook');
8var ExecutionEnvironment = require('fbjs/lib/ExecutionEnvironment');
9
10var performanceNow = require('fbjs/lib/performanceNow');
11var warning = require('fbjs/lib/warning');
12
13var ReactDebugTool = null;
14
15if (__DEV__) {
16  var hooks = [];
17  var didHookThrowForEvent = {};
18
19  var callHook = function callHook(event, fn, context, arg1, arg2, arg3, arg4, arg5) {
20    try {
21      fn.call(context, arg1, arg2, arg3, arg4, arg5);
22    } catch (e) {
23      warning(didHookThrowForEvent[event], 'Exception thrown by hook while handling %s: %s', event, e + '\n' + e.stack);
24      didHookThrowForEvent[event] = true;
25    }
26  };
27
28  var emitEvent = function emitEvent(event, arg1, arg2, arg3, arg4, arg5) {
29    for (var i = 0; i < hooks.length; i++) {
30      var hook = hooks[i];
31      var fn = hook[event];
32      if (fn) {
33        callHook(event, fn, hook, arg1, arg2, arg3, arg4, arg5);
34      }
35    }
36  };
37
38  var _isProfiling = false;
39  var flushHistory = [];
40  var lifeCycleTimerStack = [];
41  var currentFlushNesting = 0;
42  var currentFlushMeasurements = [];
43  var currentFlushStartTime = 0;
44  var currentTimerDebugID = null;
45  var currentTimerStartTime = 0;
46  var currentTimerNestedFlushDuration = 0;
47  var currentTimerType = null;
48
49  var lifeCycleTimerHasWarned = false;
50
51  var clearHistory = function clearHistory() {
52    ReactComponentTreeHook.purgeUnmountedComponents();
53    ReactHostOperationHistoryHook.clearHistory();
54  };
55
56  var getTreeSnapshot = function getTreeSnapshot(registeredIDs) {
57    return registeredIDs.reduce(function (tree, id) {
58      var ownerID = ReactComponentTreeHook.getOwnerID(id);
59      var parentID = ReactComponentTreeHook.getParentID(id);
60      tree[id] = {
61        displayName: ReactComponentTreeHook.getDisplayName(id),
62        text: ReactComponentTreeHook.getText(id),
63        updateCount: ReactComponentTreeHook.getUpdateCount(id),
64        childIDs: ReactComponentTreeHook.getChildIDs(id),
65
66        ownerID: ownerID || parentID && ReactComponentTreeHook.getOwnerID(parentID) || 0,
67        parentID: parentID
68      };
69      return tree;
70    }, {});
71  };
72
73  var resetMeasurements = function resetMeasurements() {
74    var previousStartTime = currentFlushStartTime;
75    var previousMeasurements = currentFlushMeasurements;
76    var previousOperations = ReactHostOperationHistoryHook.getHistory();
77
78    if (currentFlushNesting === 0) {
79      currentFlushStartTime = 0;
80      currentFlushMeasurements = [];
81      clearHistory();
82      return;
83    }
84
85    if (previousMeasurements.length || previousOperations.length) {
86      var registeredIDs = ReactComponentTreeHook.getRegisteredIDs();
87      flushHistory.push({
88        duration: performanceNow() - previousStartTime,
89        measurements: previousMeasurements || [],
90        operations: previousOperations || [],
91        treeSnapshot: getTreeSnapshot(registeredIDs)
92      });
93    }
94
95    clearHistory();
96    currentFlushStartTime = performanceNow();
97    currentFlushMeasurements = [];
98  };
99
100  var checkDebugID = function checkDebugID(debugID) {
101    var allowRoot = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
102
103    if (allowRoot && debugID === 0) {
104      return;
105    }
106    if (!debugID) {
107      warning(false, 'ReactDebugTool: debugID may not be empty.');
108    }
109  };
110
111  var beginLifeCycleTimer = function beginLifeCycleTimer(debugID, timerType) {
112    if (currentFlushNesting === 0) {
113      return;
114    }
115    if (currentTimerType && !lifeCycleTimerHasWarned) {
116      warning(false, 'There is an internal error in the React performance measurement code.' + '\n\nDid not expect %s timer to start while %s timer is still in ' + 'progress for %s instance.', timerType, currentTimerType || 'no', debugID === currentTimerDebugID ? 'the same' : 'another');
117      lifeCycleTimerHasWarned = true;
118    }
119    currentTimerStartTime = performanceNow();
120    currentTimerNestedFlushDuration = 0;
121    currentTimerDebugID = debugID;
122    currentTimerType = timerType;
123  };
124
125  var endLifeCycleTimer = function endLifeCycleTimer(debugID, timerType) {
126    if (currentFlushNesting === 0) {
127      return;
128    }
129    if (currentTimerType !== timerType && !lifeCycleTimerHasWarned) {
130      warning(false, 'There is an internal error in the React performance measurement code. ' + 'We did not expect %s timer to stop while %s timer is still in ' + 'progress for %s instance. Please report this as a bug in React.', timerType, currentTimerType || 'no', debugID === currentTimerDebugID ? 'the same' : 'another');
131      lifeCycleTimerHasWarned = true;
132    }
133    if (_isProfiling) {
134      currentFlushMeasurements.push({
135        timerType: timerType,
136        instanceID: debugID,
137        duration: performanceNow() - currentTimerStartTime - currentTimerNestedFlushDuration
138      });
139    }
140    currentTimerStartTime = 0;
141    currentTimerNestedFlushDuration = 0;
142    currentTimerDebugID = null;
143    currentTimerType = null;
144  };
145
146  var pauseCurrentLifeCycleTimer = function pauseCurrentLifeCycleTimer() {
147    var currentTimer = {
148      startTime: currentTimerStartTime,
149      nestedFlushStartTime: performanceNow(),
150      debugID: currentTimerDebugID,
151      timerType: currentTimerType
152    };
153    lifeCycleTimerStack.push(currentTimer);
154    currentTimerStartTime = 0;
155    currentTimerNestedFlushDuration = 0;
156    currentTimerDebugID = null;
157    currentTimerType = null;
158  };
159
160  var resumeCurrentLifeCycleTimer = function resumeCurrentLifeCycleTimer() {
161    var _lifeCycleTimerStack$ = lifeCycleTimerStack.pop(),
162        startTime = _lifeCycleTimerStack$.startTime,
163        nestedFlushStartTime = _lifeCycleTimerStack$.nestedFlushStartTime,
164        debugID = _lifeCycleTimerStack$.debugID,
165        timerType = _lifeCycleTimerStack$.timerType;
166
167    var nestedFlushDuration = performanceNow() - nestedFlushStartTime;
168    currentTimerStartTime = startTime;
169    currentTimerNestedFlushDuration += nestedFlushDuration;
170    currentTimerDebugID = debugID;
171    currentTimerType = timerType;
172  };
173
174  var lastMarkTimeStamp = 0;
175  var canUsePerformanceMeasure = typeof performance !== 'undefined' && typeof performance.mark === 'function' && typeof performance.clearMarks === 'function' && typeof performance.measure === 'function' && typeof performance.clearMeasures === 'function';
176
177  var shouldMark = function shouldMark(debugID) {
178    if (!_isProfiling || !canUsePerformanceMeasure) {
179      return false;
180    }
181    var element = ReactComponentTreeHook.getElement(debugID);
182    if (element == null || typeof element !== 'object') {
183      return false;
184    }
185    var isHostElement = typeof element.type === 'string';
186    if (isHostElement) {
187      return false;
188    }
189    return true;
190  };
191
192  var markBegin = function markBegin(debugID, markType) {
193    if (!shouldMark(debugID)) {
194      return;
195    }
196
197    var markName = debugID + '::' + markType;
198    lastMarkTimeStamp = performanceNow();
199    performance.mark(markName);
200  };
201
202  var markEnd = function markEnd(debugID, markType) {
203    if (!shouldMark(debugID)) {
204      return;
205    }
206
207    var markName = debugID + '::' + markType;
208    var displayName = ReactComponentTreeHook.getDisplayName(debugID) || 'Unknown';
209
210    var timeStamp = performanceNow();
211    if (timeStamp - lastMarkTimeStamp > 0.1) {
212      var measurementName = displayName + ' [' + markType + ']';
213      performance.measure(measurementName, markName);
214    }
215
216    performance.clearMarks(markName);
217    performance.clearMeasures(measurementName);
218  };
219
220  ReactDebugTool = {
221    addHook: function addHook(hook) {
222      hooks.push(hook);
223    },
224    removeHook: function removeHook(hook) {
225      for (var i = 0; i < hooks.length; i++) {
226        if (hooks[i] === hook) {
227          hooks.splice(i, 1);
228          i--;
229        }
230      }
231    },
232    isProfiling: function isProfiling() {
233      return _isProfiling;
234    },
235    beginProfiling: function beginProfiling() {
236      if (_isProfiling) {
237        return;
238      }
239
240      _isProfiling = true;
241      flushHistory.length = 0;
242      resetMeasurements();
243      ReactDebugTool.addHook(ReactHostOperationHistoryHook);
244    },
245    endProfiling: function endProfiling() {
246      if (!_isProfiling) {
247        return;
248      }
249
250      _isProfiling = false;
251      resetMeasurements();
252      ReactDebugTool.removeHook(ReactHostOperationHistoryHook);
253    },
254    getFlushHistory: function getFlushHistory() {
255      return flushHistory;
256    },
257    onBeginFlush: function onBeginFlush() {
258      currentFlushNesting++;
259      resetMeasurements();
260      pauseCurrentLifeCycleTimer();
261      emitEvent('onBeginFlush');
262    },
263    onEndFlush: function onEndFlush() {
264      resetMeasurements();
265      currentFlushNesting--;
266      resumeCurrentLifeCycleTimer();
267      emitEvent('onEndFlush');
268    },
269    onBeginLifeCycleTimer: function onBeginLifeCycleTimer(debugID, timerType) {
270      checkDebugID(debugID);
271      emitEvent('onBeginLifeCycleTimer', debugID, timerType);
272      markBegin(debugID, timerType);
273      beginLifeCycleTimer(debugID, timerType);
274    },
275    onEndLifeCycleTimer: function onEndLifeCycleTimer(debugID, timerType) {
276      checkDebugID(debugID);
277      endLifeCycleTimer(debugID, timerType);
278      markEnd(debugID, timerType);
279      emitEvent('onEndLifeCycleTimer', debugID, timerType);
280    },
281    onBeginProcessingChildContext: function onBeginProcessingChildContext() {
282      emitEvent('onBeginProcessingChildContext');
283    },
284    onEndProcessingChildContext: function onEndProcessingChildContext() {
285      emitEvent('onEndProcessingChildContext');
286    },
287    onHostOperation: function onHostOperation(operation) {
288      checkDebugID(operation.instanceID);
289      emitEvent('onHostOperation', operation);
290    },
291    onSetState: function onSetState() {
292      emitEvent('onSetState');
293    },
294    onSetChildren: function onSetChildren(debugID, childDebugIDs) {
295      checkDebugID(debugID);
296      childDebugIDs.forEach(checkDebugID);
297      emitEvent('onSetChildren', debugID, childDebugIDs);
298    },
299    onBeforeMountComponent: function onBeforeMountComponent(debugID, element, parentDebugID) {
300      checkDebugID(debugID);
301      checkDebugID(parentDebugID, true);
302      emitEvent('onBeforeMountComponent', debugID, element, parentDebugID);
303      markBegin(debugID, 'mount');
304    },
305    onMountComponent: function onMountComponent(debugID) {
306      checkDebugID(debugID);
307      markEnd(debugID, 'mount');
308      emitEvent('onMountComponent', debugID);
309    },
310    onBeforeUpdateComponent: function onBeforeUpdateComponent(debugID, element) {
311      checkDebugID(debugID);
312      emitEvent('onBeforeUpdateComponent', debugID, element);
313      markBegin(debugID, 'update');
314    },
315    onUpdateComponent: function onUpdateComponent(debugID) {
316      checkDebugID(debugID);
317      markEnd(debugID, 'update');
318      emitEvent('onUpdateComponent', debugID);
319    },
320    onBeforeUnmountComponent: function onBeforeUnmountComponent(debugID) {
321      checkDebugID(debugID);
322      emitEvent('onBeforeUnmountComponent', debugID);
323      markBegin(debugID, 'unmount');
324    },
325    onUnmountComponent: function onUnmountComponent(debugID) {
326      checkDebugID(debugID);
327      markEnd(debugID, 'unmount');
328      emitEvent('onUnmountComponent', debugID);
329    },
330    onTestEvent: function onTestEvent() {
331      emitEvent('onTestEvent');
332    }
333  };
334
335  ReactDebugTool.addHook(ReactInvalidSetStateWarningHook);
336  ReactDebugTool.addHook(ReactComponentTreeHook);
337  var url = ExecutionEnvironment.canUseDOM && window.location.href || '';
338  if (/[?&]react_perf\b/.test(url)) {
339    ReactDebugTool.beginProfiling();
340  }
341}
342
343module.exports = ReactDebugTool;
Full Screen

72fa9dReactDebugTool.js

Source: 72fa9dReactDebugTool.js Github

copy
1
2
3'use strict';
4
5var ReactInvalidSetStateWarningHook = require('ReactInvalidSetStateWarningHook');
6var ReactHostOperationHistoryHook = require('ReactHostOperationHistoryHook');
7var ReactComponentTreeHook = require('react/lib/ReactComponentTreeHook');
8var ExecutionEnvironment = require('fbjs/lib/ExecutionEnvironment');
9
10var performanceNow = require('fbjs/lib/performanceNow');
11var warning = require('fbjs/lib/warning');
12
13var ReactDebugTool = null;
14
15if (__DEV__) {
16  var hooks = [];
17  var didHookThrowForEvent = {};
18
19  var callHook = function callHook(event, fn, context, arg1, arg2, arg3, arg4, arg5) {
20    try {
21      fn.call(context, arg1, arg2, arg3, arg4, arg5);
22    } catch (e) {
23      warning(didHookThrowForEvent[event], 'Exception thrown by hook while handling %s: %s', event, e + '\n' + e.stack);
24      didHookThrowForEvent[event] = true;
25    }
26  };
27
28  var emitEvent = function emitEvent(event, arg1, arg2, arg3, arg4, arg5) {
29    for (var i = 0; i < hooks.length; i++) {
30      var hook = hooks[i];
31      var fn = hook[event];
32      if (fn) {
33        callHook(event, fn, hook, arg1, arg2, arg3, arg4, arg5);
34      }
35    }
36  };
37
38  var _isProfiling = false;
39  var flushHistory = [];
40  var lifeCycleTimerStack = [];
41  var currentFlushNesting = 0;
42  var currentFlushMeasurements = [];
43  var currentFlushStartTime = 0;
44  var currentTimerDebugID = null;
45  var currentTimerStartTime = 0;
46  var currentTimerNestedFlushDuration = 0;
47  var currentTimerType = null;
48
49  var lifeCycleTimerHasWarned = false;
50
51  var clearHistory = function clearHistory() {
52    ReactComponentTreeHook.purgeUnmountedComponents();
53    ReactHostOperationHistoryHook.clearHistory();
54  };
55
56  var getTreeSnapshot = function getTreeSnapshot(registeredIDs) {
57    return registeredIDs.reduce(function (tree, id) {
58      var ownerID = ReactComponentTreeHook.getOwnerID(id);
59      var parentID = ReactComponentTreeHook.getParentID(id);
60      tree[id] = {
61        displayName: ReactComponentTreeHook.getDisplayName(id),
62        text: ReactComponentTreeHook.getText(id),
63        updateCount: ReactComponentTreeHook.getUpdateCount(id),
64        childIDs: ReactComponentTreeHook.getChildIDs(id),
65
66        ownerID: ownerID || parentID && ReactComponentTreeHook.getOwnerID(parentID) || 0,
67        parentID: parentID
68      };
69      return tree;
70    }, {});
71  };
72
73  var resetMeasurements = function resetMeasurements() {
74    var previousStartTime = currentFlushStartTime;
75    var previousMeasurements = currentFlushMeasurements;
76    var previousOperations = ReactHostOperationHistoryHook.getHistory();
77
78    if (currentFlushNesting === 0) {
79      currentFlushStartTime = 0;
80      currentFlushMeasurements = [];
81      clearHistory();
82      return;
83    }
84
85    if (previousMeasurements.length || previousOperations.length) {
86      var registeredIDs = ReactComponentTreeHook.getRegisteredIDs();
87      flushHistory.push({
88        duration: performanceNow() - previousStartTime,
89        measurements: previousMeasurements || [],
90        operations: previousOperations || [],
91        treeSnapshot: getTreeSnapshot(registeredIDs)
92      });
93    }
94
95    clearHistory();
96    currentFlushStartTime = performanceNow();
97    currentFlushMeasurements = [];
98  };
99
100  var checkDebugID = function checkDebugID(debugID) {
101    var allowRoot = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
102
103    if (allowRoot && debugID === 0) {
104      return;
105    }
106    if (!debugID) {
107      warning(false, 'ReactDebugTool: debugID may not be empty.');
108    }
109  };
110
111  var beginLifeCycleTimer = function beginLifeCycleTimer(debugID, timerType) {
112    if (currentFlushNesting === 0) {
113      return;
114    }
115    if (currentTimerType && !lifeCycleTimerHasWarned) {
116      warning(false, 'There is an internal error in the React performance measurement code.' + '\n\nDid not expect %s timer to start while %s timer is still in ' + 'progress for %s instance.', timerType, currentTimerType || 'no', debugID === currentTimerDebugID ? 'the same' : 'another');
117      lifeCycleTimerHasWarned = true;
118    }
119    currentTimerStartTime = performanceNow();
120    currentTimerNestedFlushDuration = 0;
121    currentTimerDebugID = debugID;
122    currentTimerType = timerType;
123  };
124
125  var endLifeCycleTimer = function endLifeCycleTimer(debugID, timerType) {
126    if (currentFlushNesting === 0) {
127      return;
128    }
129    if (currentTimerType !== timerType && !lifeCycleTimerHasWarned) {
130      warning(false, 'There is an internal error in the React performance measurement code. ' + 'We did not expect %s timer to stop while %s timer is still in ' + 'progress for %s instance. Please report this as a bug in React.', timerType, currentTimerType || 'no', debugID === currentTimerDebugID ? 'the same' : 'another');
131      lifeCycleTimerHasWarned = true;
132    }
133    if (_isProfiling) {
134      currentFlushMeasurements.push({
135        timerType: timerType,
136        instanceID: debugID,
137        duration: performanceNow() - currentTimerStartTime - currentTimerNestedFlushDuration
138      });
139    }
140    currentTimerStartTime = 0;
141    currentTimerNestedFlushDuration = 0;
142    currentTimerDebugID = null;
143    currentTimerType = null;
144  };
145
146  var pauseCurrentLifeCycleTimer = function pauseCurrentLifeCycleTimer() {
147    var currentTimer = {
148      startTime: currentTimerStartTime,
149      nestedFlushStartTime: performanceNow(),
150      debugID: currentTimerDebugID,
151      timerType: currentTimerType
152    };
153    lifeCycleTimerStack.push(currentTimer);
154    currentTimerStartTime = 0;
155    currentTimerNestedFlushDuration = 0;
156    currentTimerDebugID = null;
157    currentTimerType = null;
158  };
159
160  var resumeCurrentLifeCycleTimer = function resumeCurrentLifeCycleTimer() {
161    var _lifeCycleTimerStack$ = lifeCycleTimerStack.pop(),
162        startTime = _lifeCycleTimerStack$.startTime,
163        nestedFlushStartTime = _lifeCycleTimerStack$.nestedFlushStartTime,
164        debugID = _lifeCycleTimerStack$.debugID,
165        timerType = _lifeCycleTimerStack$.timerType;
166
167    var nestedFlushDuration = performanceNow() - nestedFlushStartTime;
168    currentTimerStartTime = startTime;
169    currentTimerNestedFlushDuration += nestedFlushDuration;
170    currentTimerDebugID = debugID;
171    currentTimerType = timerType;
172  };
173
174  var lastMarkTimeStamp = 0;
175  var canUsePerformanceMeasure = typeof performance !== 'undefined' && typeof performance.mark === 'function' && typeof performance.clearMarks === 'function' && typeof performance.measure === 'function' && typeof performance.clearMeasures === 'function';
176
177  var shouldMark = function shouldMark(debugID) {
178    if (!_isProfiling || !canUsePerformanceMeasure) {
179      return false;
180    }
181    var element = ReactComponentTreeHook.getElement(debugID);
182    if (element == null || typeof element !== 'object') {
183      return false;
184    }
185    var isHostElement = typeof element.type === 'string';
186    if (isHostElement) {
187      return false;
188    }
189    return true;
190  };
191
192  var markBegin = function markBegin(debugID, markType) {
193    if (!shouldMark(debugID)) {
194      return;
195    }
196
197    var markName = debugID + '::' + markType;
198    lastMarkTimeStamp = performanceNow();
199    performance.mark(markName);
200  };
201
202  var markEnd = function markEnd(debugID, markType) {
203    if (!shouldMark(debugID)) {
204      return;
205    }
206
207    var markName = debugID + '::' + markType;
208    var displayName = ReactComponentTreeHook.getDisplayName(debugID) || 'Unknown';
209
210    var timeStamp = performanceNow();
211    if (timeStamp - lastMarkTimeStamp > 0.1) {
212      var measurementName = displayName + ' [' + markType + ']';
213      performance.measure(measurementName, markName);
214    }
215
216    performance.clearMarks(markName);
217    performance.clearMeasures(measurementName);
218  };
219
220  ReactDebugTool = {
221    addHook: function addHook(hook) {
222      hooks.push(hook);
223    },
224    removeHook: function removeHook(hook) {
225      for (var i = 0; i < hooks.length; i++) {
226        if (hooks[i] === hook) {
227          hooks.splice(i, 1);
228          i--;
229        }
230      }
231    },
232    isProfiling: function isProfiling() {
233      return _isProfiling;
234    },
235    beginProfiling: function beginProfiling() {
236      if (_isProfiling) {
237        return;
238      }
239
240      _isProfiling = true;
241      flushHistory.length = 0;
242      resetMeasurements();
243      ReactDebugTool.addHook(ReactHostOperationHistoryHook);
244    },
245    endProfiling: function endProfiling() {
246      if (!_isProfiling) {
247        return;
248      }
249
250      _isProfiling = false;
251      resetMeasurements();
252      ReactDebugTool.removeHook(ReactHostOperationHistoryHook);
253    },
254    getFlushHistory: function getFlushHistory() {
255      return flushHistory;
256    },
257    onBeginFlush: function onBeginFlush() {
258      currentFlushNesting++;
259      resetMeasurements();
260      pauseCurrentLifeCycleTimer();
261      emitEvent('onBeginFlush');
262    },
263    onEndFlush: function onEndFlush() {
264      resetMeasurements();
265      currentFlushNesting--;
266      resumeCurrentLifeCycleTimer();
267      emitEvent('onEndFlush');
268    },
269    onBeginLifeCycleTimer: function onBeginLifeCycleTimer(debugID, timerType) {
270      checkDebugID(debugID);
271      emitEvent('onBeginLifeCycleTimer', debugID, timerType);
272      markBegin(debugID, timerType);
273      beginLifeCycleTimer(debugID, timerType);
274    },
275    onEndLifeCycleTimer: function onEndLifeCycleTimer(debugID, timerType) {
276      checkDebugID(debugID);
277      endLifeCycleTimer(debugID, timerType);
278      markEnd(debugID, timerType);
279      emitEvent('onEndLifeCycleTimer', debugID, timerType);
280    },
281    onBeginProcessingChildContext: function onBeginProcessingChildContext() {
282      emitEvent('onBeginProcessingChildContext');
283    },
284    onEndProcessingChildContext: function onEndProcessingChildContext() {
285      emitEvent('onEndProcessingChildContext');
286    },
287    onHostOperation: function onHostOperation(operation) {
288      checkDebugID(operation.instanceID);
289      emitEvent('onHostOperation', operation);
290    },
291    onSetState: function onSetState() {
292      emitEvent('onSetState');
293    },
294    onSetChildren: function onSetChildren(debugID, childDebugIDs) {
295      checkDebugID(debugID);
296      childDebugIDs.forEach(checkDebugID);
297      emitEvent('onSetChildren', debugID, childDebugIDs);
298    },
299    onBeforeMountComponent: function onBeforeMountComponent(debugID, element, parentDebugID) {
300      checkDebugID(debugID);
301      checkDebugID(parentDebugID, true);
302      emitEvent('onBeforeMountComponent', debugID, element, parentDebugID);
303      markBegin(debugID, 'mount');
304    },
305    onMountComponent: function onMountComponent(debugID) {
306      checkDebugID(debugID);
307      markEnd(debugID, 'mount');
308      emitEvent('onMountComponent', debugID);
309    },
310    onBeforeUpdateComponent: function onBeforeUpdateComponent(debugID, element) {
311      checkDebugID(debugID);
312      emitEvent('onBeforeUpdateComponent', debugID, element);
313      markBegin(debugID, 'update');
314    },
315    onUpdateComponent: function onUpdateComponent(debugID) {
316      checkDebugID(debugID);
317      markEnd(debugID, 'update');
318      emitEvent('onUpdateComponent', debugID);
319    },
320    onBeforeUnmountComponent: function onBeforeUnmountComponent(debugID) {
321      checkDebugID(debugID);
322      emitEvent('onBeforeUnmountComponent', debugID);
323      markBegin(debugID, 'unmount');
324    },
325    onUnmountComponent: function onUnmountComponent(debugID) {
326      checkDebugID(debugID);
327      markEnd(debugID, 'unmount');
328      emitEvent('onUnmountComponent', debugID);
329    },
330    onTestEvent: function onTestEvent() {
331      emitEvent('onTestEvent');
332    }
333  };
334
335  ReactDebugTool.addHook(ReactInvalidSetStateWarningHook);
336  ReactDebugTool.addHook(ReactComponentTreeHook);
337  var url = ExecutionEnvironment.canUseDOM && window.location.href || '';
338  if (/[?&]react_perf\b/.test(url)) {
339    ReactDebugTool.beginProfiling();
340  }
341}
342
343module.exports = ReactDebugTool;
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Playwright Internal on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)