diff --git a/packages/scheduler/src/__tests__/Scheduler-test.js b/packages/scheduler/src/__tests__/Scheduler-test.js index 5af59a24a24f5..9e0813e461e61 100644 --- a/packages/scheduler/src/__tests__/Scheduler-test.js +++ b/packages/scheduler/src/__tests__/Scheduler-test.js @@ -183,7 +183,7 @@ describe('SchedulerBrowser', () => { it('task with continuation', () => { scheduleCallback(NormalPriority, () => { runtime.log('Task'); - // Request paint so that we yield at the end of the frame interval + // Request paint so that we yield immediately requestPaint(); while (!Scheduler.unstable_shouldYield()) { runtime.advanceTime(1); @@ -199,7 +199,7 @@ describe('SchedulerBrowser', () => { runtime.assertLog([ 'Message Event', 'Task', - gate(flags => (flags.www ? 'Yield at 10ms' : 'Yield at 5ms')), + 'Yield at 0ms', 'Post Message', ]); diff --git a/packages/scheduler/src/forks/Scheduler.js b/packages/scheduler/src/forks/Scheduler.js index a785bec95320c..0e85e0a99b7b0 100644 --- a/packages/scheduler/src/forks/Scheduler.js +++ b/packages/scheduler/src/forks/Scheduler.js @@ -93,6 +93,8 @@ var isPerformingWork = false; var isHostCallbackScheduled = false; var isHostTimeoutScheduled = false; +var needsPaint = false; + // Capture local references to native APIs, in case a polyfill overrides them. const localSetTimeout = typeof setTimeout === 'function' ? setTimeout : null; const localClearTimeout = @@ -456,6 +458,10 @@ let frameInterval = frameYieldMs; let startTime = -1; function shouldYieldToHost(): boolean { + if (needsPaint) { + // Yield now. + return true; + } const timeElapsed = getCurrentTime() - startTime; if (timeElapsed < frameInterval) { // The main thread has only been blocked for a really short amount of time; @@ -466,7 +472,9 @@ function shouldYieldToHost(): boolean { return true; } -function requestPaint() {} +function requestPaint() { + needsPaint = true; +} function forceFrameRate(fps: number) { if (fps < 0 || fps > 125) { @@ -486,6 +494,7 @@ function forceFrameRate(fps: number) { } const performWorkUntilDeadline = () => { + needsPaint = false; if (isMessageLoopRunning) { const currentTime = getCurrentTime(); // Keep track of the start time so we can measure how long the main thread