Inform DevTools of commit priority level (#15664)
* Pass inferred priority level to DevTools commit hook in PROFILE mode
Brian Vaughn committed
May 20, 2019 at 08:37 UTC
50b50c26f6724c559a3550b903d2cc2eeffdf092
2 files changed
+45
-26
packages/react-reconciler/src/ReactFiberDevToolsHook.js
+44
-25
@@ -7,8 +7,13 @@
7
* @flow
8
*/
9
10
+import {enableProfilerTimer} from 'shared/ReactFeatureFlags';
11
+import {requestCurrentTime} from './ReactFiberScheduler';
12
+import {inferPriorityFromExpirationTime} from './ReactFiberExpirationTime';
13
+
14
import type {Fiber} from './ReactFiber';
15
import type {FiberRoot} from './ReactFiberRoot';
16
+import type {ExpirationTime} from './ReactFiberExpirationTime';
17
18
import warningWithoutStack from 'shared/warningWithoutStack';
19
@@ -18,23 +23,6 @@ let onCommitFiberRoot = null;
23
let onCommitFiberUnmount = null;
24
let hasLoggedError = false;
25
21
-function catchErrors(fn) {
22
- return function(arg) {
23
- try {
24
- return fn(arg);
25
- } catch (err) {
26
- if (__DEV__ && !hasLoggedError) {
27
- hasLoggedError = true;
28
- warningWithoutStack(
29
- false,
30
- 'React DevTools encountered an error: %s',
31
- err,
32
- );
33
- }
34
- }
35
- };
36
-}
37
-
26
export const isDevToolsPresent =
27
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ !== 'undefined';
28
@@ -65,12 +53,43 @@ export function injectInternals(internals: Object): boolean {
53
try {
54
const rendererID = hook.inject(internals);
55
// We have successfully injected, so now it is safe to set up hooks.
68
- onCommitFiberRoot = catchErrors(root =>
69
- hook.onCommitFiberRoot(rendererID, root),
70
- );
71
- onCommitFiberUnmount = catchErrors(fiber =>
72
- hook.onCommitFiberUnmount(rendererID, fiber),
73
- );
56
+ onCommitFiberRoot = (root, expirationTime) => {
57
+ try {
58
+ if (enableProfilerTimer) {
59
+ const currentTime = requestCurrentTime();
60
+ const priorityLevel = inferPriorityFromExpirationTime(
61
+ currentTime,
62
+ expirationTime,
63
+ );
64
+ hook.onCommitFiberRoot(rendererID, root, priorityLevel);
65
+ } else {
66
+ hook.onCommitFiberRoot(rendererID, root);
67
+ }
68
+ } catch (err) {
69
+ if (__DEV__ && !hasLoggedError) {
70
+ hasLoggedError = true;
71
+ warningWithoutStack(
72
+ false,
73
+ 'React DevTools encountered an error: %s',
74
+ err,
75
+ );
76
+ }
77
+ }
78
+ };
79
+ onCommitFiberUnmount = fiber => {
80
+ try {
81
+ hook.onCommitFiberUnmount(rendererID, fiber);
82
+ } catch (err) {
83
+ if (__DEV__ && !hasLoggedError) {
84
+ hasLoggedError = true;
85
+ warningWithoutStack(
86
+ false,
87
+ 'React DevTools encountered an error: %s',
88
+ err,
89
+ );
90
+ }
91
+ }
92
+ };
93
} catch (err) {
94
// Catch all errors because it is unsafe to throw during initialization.
95
if (__DEV__) {
@@ -85,9 +104,9 @@ export function injectInternals(internals: Object): boolean {
104
return true;
105
}
106
88
-export function onCommitRoot(root: FiberRoot) {
107
+export function onCommitRoot(root: FiberRoot, expirationTime: ExpirationTime) {
108
if (typeof onCommitFiberRoot === 'function') {
90
- onCommitFiberRoot(root);
109
+ onCommitFiberRoot(root, expirationTime);
110
}
111
}
112
packages/react-reconciler/src/ReactFiberScheduler.js
+1
-1
@@ -1560,7 +1560,7 @@ function commitRootImpl(root) {
1560
legacyErrorBoundariesThatAlreadyFailed = null;
1561
}
1562
1563
- onCommitRoot(finishedWork.stateNode);
1563
+ onCommitRoot(finishedWork.stateNode, expirationTime);
1564
1565
if (remainingExpirationTime === Sync) {
1566
// Count the number of times the root synchronously re-renders without