Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inform DevTools of commit priority level #15664

Merged
merged 4 commits into from
May 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Andrew mentioned maybe we could use getCurrentPriorityLevel() here rather than inferring the priority level. I don't think the priority level a Fiber was scheduled with will necessarily match the current priority level though. (Maybe I misunderstood his suggestion.)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you call getCurrentPriorityLevel inside commitRoot (not commitRootImpl, since that's always run at ImmediatePriority — it has to be the wrapper) then it will match the priority of the commit, which is not necessarily the same thing as the priority at which something was scheduled because multiple priorities can get batched.

This approach seems fine, though.

It's possible we may eventually track the priority on the update object.

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