Skip to content

Commit

Permalink
Inform DevTools of commit priority level
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Vaughn committed May 15, 2019
1 parent af19e2e commit a925a01
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 27 deletions.
59 changes: 34 additions & 25 deletions packages/react-reconciler/src/ReactFiberDevToolsHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import type {Fiber} from './ReactFiber';
import type {FiberRoot} from './ReactFiberRoot';
import type {ReactPriorityLevel} from './SchedulerWithReactIntegration';

import warningWithoutStack from 'shared/warningWithoutStack';

Expand All @@ -18,23 +19,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 +49,34 @@ 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, priorityLevel) => {
try {
hook.onCommitFiberRoot(rendererID, root, priorityLevel);
} 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 +91,12 @@ export function injectInternals(internals: Object): boolean {
return true;
}

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

Expand Down
8 changes: 6 additions & 2 deletions packages/react-reconciler/src/ReactFiberScheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -1467,8 +1467,8 @@ function commitRootImpl(root) {

// Check if there's remaining work on this root
const remainingExpirationTime = root.firstPendingTime;
const currentTime = requestCurrentTime();
if (remainingExpirationTime !== NoWork) {
const currentTime = requestCurrentTime();
const priorityLevel = inferPriorityFromExpirationTime(
currentTime,
remainingExpirationTime,
Expand All @@ -1480,7 +1480,11 @@ function commitRootImpl(root) {
legacyErrorBoundariesThatAlreadyFailed = null;
}

onCommitRoot(finishedWork.stateNode);
const priorityLevel = inferPriorityFromExpirationTime(
currentTime,
expirationTime,
);
onCommitRoot(finishedWork.stateNode, priorityLevel);

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

0 comments on commit a925a01

Please sign in to comment.