Skip to content

Commit

Permalink
Inform DevTools of commit priority level (#15664)
Browse files Browse the repository at this point in the history
* Pass inferred priority level to DevTools commit hook in PROFILE mode
  • Loading branch information
Brian Vaughn authored May 20, 2019
1 parent 0bd9b5d commit 50b50c2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 26 deletions.
69 changes: 44 additions & 25 deletions packages/react-reconciler/src/ReactFiberDevToolsHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
* @flow
*/

import {enableProfilerTimer} from 'shared/ReactFeatureFlags';
import {requestCurrentTime} from './ReactFiberScheduler';
import {inferPriorityFromExpirationTime} from './ReactFiberExpirationTime';

import type {Fiber} from './ReactFiber';
import type {FiberRoot} from './ReactFiberRoot';
import type {ExpirationTime} from './ReactFiberExpirationTime';

import warningWithoutStack from 'shared/warningWithoutStack';

Expand All @@ -18,23 +23,6 @@ let onCommitFiberRoot = null;
let onCommitFiberUnmount = null;
let hasLoggedError = false;

function catchErrors(fn) {
return function(arg) {
try {
return fn(arg);
} catch (err) {
if (__DEV__ && !hasLoggedError) {
hasLoggedError = true;
warningWithoutStack(
false,
'React DevTools encountered an error: %s',
err,
);
}
}
};
}

export const isDevToolsPresent =
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ !== 'undefined';

Expand Down Expand Up @@ -65,12 +53,43 @@ export function injectInternals(internals: Object): boolean {
try {
const rendererID = hook.inject(internals);
// We have successfully injected, so now it is safe to set up hooks.
onCommitFiberRoot = catchErrors(root =>
hook.onCommitFiberRoot(rendererID, root),
);
onCommitFiberUnmount = catchErrors(fiber =>
hook.onCommitFiberUnmount(rendererID, fiber),
);
onCommitFiberRoot = (root, expirationTime) => {
try {
if (enableProfilerTimer) {
const currentTime = requestCurrentTime();
const priorityLevel = inferPriorityFromExpirationTime(
currentTime,
expirationTime,
);
hook.onCommitFiberRoot(rendererID, root, priorityLevel);
} else {
hook.onCommitFiberRoot(rendererID, root);
}
} catch (err) {
if (__DEV__ && !hasLoggedError) {
hasLoggedError = true;
warningWithoutStack(
false,
'React DevTools encountered an error: %s',
err,
);
}
}
};
onCommitFiberUnmount = fiber => {
try {
hook.onCommitFiberUnmount(rendererID, fiber);
} catch (err) {
if (__DEV__ && !hasLoggedError) {
hasLoggedError = true;
warningWithoutStack(
false,
'React DevTools encountered an error: %s',
err,
);
}
}
};
} catch (err) {
// Catch all errors because it is unsafe to throw during initialization.
if (__DEV__) {
Expand All @@ -85,9 +104,9 @@ export function injectInternals(internals: Object): boolean {
return true;
}

export function onCommitRoot(root: FiberRoot) {
export function onCommitRoot(root: FiberRoot, expirationTime: ExpirationTime) {
if (typeof onCommitFiberRoot === 'function') {
onCommitFiberRoot(root);
onCommitFiberRoot(root, expirationTime);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/react-reconciler/src/ReactFiberScheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -1560,7 +1560,7 @@ function commitRootImpl(root) {
legacyErrorBoundariesThatAlreadyFailed = null;
}

onCommitRoot(finishedWork.stateNode);
onCommitRoot(finishedWork.stateNode, expirationTime);

if (remainingExpirationTime === Sync) {
// Count the number of times the root synchronously re-renders without
Expand Down

0 comments on commit 50b50c2

Please sign in to comment.