[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 |
packages/scheduler/src/Scheduler.js
Outdated
} | ||
requestHostCallback = function(cb, ms) { | ||
if (_currentTime !== -1) { | ||
// Protect against re-entrancy. Jest's fake timer queue flushes timer |
gaearon
Sep 27, 2018
Member
Maybe remove mentions of Jest? If it’s meant to be generic.
Maybe remove mentions of Jest? If it’s meant to be generic.
acdlite
Sep 27, 2018
Author
Member
Good point
Good point
This looks okay to me. |
InteractivePriority = Scheduler.unstable_InteractivePriority; | ||
}); | ||
|
||
it('runAllTimers only flushes some callbacks', () => { |
bvaughn
Sep 27, 2018
Contributor
nit: Ambiguous wording
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) { |
bvaughn
Sep 27, 2018
Contributor
This function declaration fails the no-inner-declarations
lint rule.
This function declaration fails the no-inner-declarations
lint rule.
Merged
This was referenced Oct 24, 2018
This was referenced Nov 1, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
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.