@samitouri / QOS-React-2 / commits / e7fcfe1047

[scheduler] Put isPendingInput behind a flag (#15962)

Andrew Clark committed Jun 22, 2019 at 00:46 UTC e7fcfe104734c081bab7418a22ec74a1a34e0709
3 files changed +46 -30
packages/scheduler/src/SchedulerFeatureFlags.js
+1
@@ -7,3 +7,4 @@
7 */
8
9 export const enableSchedulerDebugging = false;
10 +export const enableIsInputPending = false;
packages/scheduler/src/forks/SchedulerFeatureFlags.www.js
+4 -1
@@ -6,4 +6,7 @@
6 *
7 */
8
9 -export const enableSchedulerDebugging = true;
9 +export const {
10 + enableIsInputPending,
11 + enableSchedulerDebugging,
12 +} = require('SchedulerFeatureFlags');
packages/scheduler/src/forks/SchedulerHostConfig.default.js
+41 -29
@@ -5,6 +5,8 @@
5 * LICENSE file in the root directory of this source tree.
6 */
7
8 +import {enableIsInputPending} from '../SchedulerFeatureFlags';
9 +
10 // The DOM Scheduler implementation is similar to requestIdleCallback. It
11 // works by scheduling a requestAnimationFrame, storing the time for the start
12 // of the frame, then scheduling a postMessage which gets scheduled after paint.
@@ -166,36 +168,50 @@ if (
168 let maxFrameLength = 150;
169 let needsPaint = false;
170
169 - const isInputPending =
171 + if (
172 + enableIsInputPending &&
173 navigator !== undefined &&
174 navigator.scheduling !== undefined &&
175 navigator.scheduling.isInputPending !== undefined
173 - ? navigator.scheduling.isInputPending
174 - : null;
175 -
176 - shouldYieldToHost = function() {
177 - const currentTime = getCurrentTime();
178 - if (currentTime < frameDeadline) {
179 - // There's still time left in the frame.
180 - return false;
181 - } else {
182 - // There's no time left in the frame. We may want to yield control of the
183 - // main thread, so the browser can perform high priority tasks. The main
184 - // ones are painting and user input. If we're certain there's no user
185 - // input, then we can yield less often without making the app less
186 - // responsive. We'll also check if a paint was requested. We'll eventually
187 - // yield regardless, since there could be other main thread tasks that we
188 - // don't know about.
189 - if (!needsPaint && isInputPending !== null && !isInputPending()) {
190 - // There's no pending input, and no task requested a paint. Only yield
191 - // if we've reached the max frame length.
176 + ) {
177 + const isInputPending = navigator.scheduling.isInputPending;
178 + shouldYieldToHost = function() {
179 + const currentTime = getCurrentTime();
180 + if (currentTime >= frameDeadline) {
181 + // There's no time left in the frame. We may want to yield control of
182 + // the main thread, so the browser can perform high priority tasks. The
183 + // main ones are painting and user input. If there's a pending paint or
184 + // a pending input, then we should yield. But if there's neither, then
185 + // we can yield less often while remaining responsive. We'll eventually
186 + // yield regardless, since there could be a pending paint that wasn't
187 + // accompanied by a call to `requestPaint`, or other main thread tasks
188 + // like network events.
189 + if (needsPaint || isInputPending()) {
190 + // There is either a pending paint or a pending input.
191 + return true;
192 + }
193 + // There's no pending input. Only yield if we've reached the max
194 + // frame length.
195 return currentTime >= frameDeadline + maxFrameLength;
196 + } else {
197 + // There's still time left in the frame.
198 + return false;
199 }
194 - // Either there is pending input, or there's no way for us to be sure
195 - // because `isInputPending` is not available.
196 - return true;
197 - }
198 - };
200 + };
201 +
202 + requestPaint = function() {
203 + needsPaint = true;
204 + };
205 + } else {
206 + // `isInputPending` is not available. Since we have no way of knowing if
207 + // there's pending input, always yield at the end of the frame.
208 + shouldYieldToHost = function() {
209 + return getCurrentTime() >= frameDeadline;
210 + };
211 +
212 + // Since we yield every frame regardless, `requestPaint` has no effect.
213 + requestPaint = function() {};
214 + }
215
216 forceFrameRate = function(fps) {
217 if (fps < 0 || fps > 125) {
@@ -327,8 +343,4 @@ if (
343 localClearTimeout(timeoutID);
344 timeoutID = -1;
345 };
330 -
331 - requestPaint = function() {
332 - needsPaint = true;
333 - };
346 }