-
Notifications
You must be signed in to change notification settings - Fork 47k
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] Improve naive fallback version used in non-DOM environments #13740
Conversation
Added some tests for the non-DOM version of Scheduler that is used as a fallback, e.g. Jest. The tests use Jest's fake timers API: - `jest.runAllTimers(ms)` flushes all scheduled work, as expected - `jest.advanceTimersByTime(ms)` flushes only callbacks that expire within the given milliseconds. These capabilities should be sufficient for most product tests. Because jest's fake timers do not override performance.now or Date.now, we assume time is constant. This means Scheduler's internal time will not be aligned with other code that reads from `performance.now`. For finer control, the user can override `window._sched` like we do in our tests. We will likely publish a Jest package that has this built in.
LogError: { FetchError: invalid json response body at http://react.zpao.com/builds/master/_commits/4d17c3f0516ddd5a7d23a460abbc3c3ddeab98a9/results.json reason: Unexpected token < in JSON at position 0
at /home/circleci/project/node_modules/node-fetch/lib/body.js:48:31
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:189:7)
name: 'FetchError',
message: 'invalid json response body at http://react.zpao.com/builds/master/_commits/4d17c3f0516ddd5a7d23a460abbc3c3ddeab98a9/results.json reason: Unexpected token < in JSON at position 0',
type: 'invalid-json' }
Generated by 🚫 dangerJS |
packages/scheduler/src/Scheduler.js
Outdated
} | ||
requestHostCallback = function(cb, ms) { | ||
if (_currentTime !== -1) { | ||
// Protect against re-entrancy. Jest's fake timer queue flushes timer |
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.
Maybe remove mentions of Jest? If it’s meant to be generic.
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 point
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 looks okay to me.
InteractivePriority = Scheduler.unstable_InteractivePriority; | ||
}); | ||
|
||
it('runAllTimers only flushes some callbacks', () => { |
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.
nit: Ambiguous wording
packages/scheduler/src/Scheduler.js
Outdated
// purposes, like with jest's fake timer API. | ||
var _callback = null; | ||
var _currentTime = -1; | ||
function flushCallback(didTimeout, ms) { |
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 function declaration fails the no-inner-declarations
lint rule.
3d90277
to
4d17c3f
Compare
Added some tests for the non-DOM version of Scheduler that is used as a fallback, e.g. Jest. The tests use Jest's fake timers API:
jest.runAllTimers()
flushes all scheduled work.jest.advanceTimersByTime(ms)
flushes only callbacks that expire within the given milliseconds.These capabilities should be sufficient for most product tests. Because jest's fake timers do not override
performance.now
orDate.now
, we assume time is constant. This means Scheduler's internal time will not be aligned with other code that reads fromperformance.now
. For finer control, the user can overridewindow._sched
like we do in our tests. We will likely publish a Jest package that has this built in.