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
[Scheduler] Get current time from performance.now in non-DOM environments #19532
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit e35fbf0:
|
@@ -16,10 +16,24 @@ export let requestPaint; | |||
export let getCurrentTime; | |||
export let forceFrameRate; | |||
|
|||
const isWindowUndefined = typeof window === 'undefined'; | |||
|
|||
const performance = isWindowUndefined ? global.performance : window.performance; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this needed? Why not just use performance
as a global in the first place?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is based on a comment in the previous code.
// Capture local references to native APIs, in case a polyfill overrides them.
const performance = window.performance;
const Date = window.Date;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if all environments that don't have window
are guaranteed to have global
, or not. We don't already use global
in this file yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example what if this code is loaded in a worker. Would it crash?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would feel better about refactoring to read globals directly from the global scope (without a qualifier) and aliasing them. Kind of like it was before 3f2cafe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good thinking. I refactored the code. I had to refactor the browser test too, window
is not considered as global under test environment.
Makes sense. Thanks. |
Summary
Scheduler makes an assumption that
performance.now
does not exist in a non-DOM environment. React Native is such environment, however, latest RN versions do have support forperformance.now
(facebook/react-native@232517a).This PR changes
getCurrentTime
implementation to useperformance.now
if it is defined, and fallback toDate.now
if the function is missing.This change is required for React Profiler API to work properly in React Native. Without this change, the Profiler in RN uses
Date
which is not so precise.Test Plan
scheduler
and tested the change in a React Native project. Profiler API now takes time fromperformance.now
and the precision is greatly improved.