44 * This source code is licensed under the MIT license found in the
55 * LICENSE file in the root directory of this source tree.
66 *
7+ * @flow
78 */
89
910/* eslint-disable no-var */
1011
12+ import type { PriorityLevel } from '../SchedulerPriorities' ;
13+
1114import {
1215 enableSchedulerDebugging ,
1316 enableProfiling ,
@@ -41,7 +44,19 @@ import {
4144 startLoggingProfilingEvents ,
4245} from '../SchedulerProfiling' ;
4346
44- let getCurrentTime ;
47+ export type Callback = boolean => ?Callback ;
48+
49+ type Task = {
50+ id : number ,
51+ callback : Callback | null ,
52+ priorityLevel : PriorityLevel ,
53+ startTime : number ,
54+ expirationTime : number ,
55+ sortIndex : number ,
56+ isQueued ?: boolean ,
57+ } ;
58+
59+ let getCurrentTime : ( ) => number | DOMHighResTimeStamp ;
4560const hasPerformanceNow =
4661 typeof performance === 'object' && typeof performance . now === 'function' ;
4762
@@ -96,7 +111,9 @@ const localSetImmediate =
96111
97112const isInputPending =
98113 typeof navigator !== 'undefined' &&
114+ // $FlowFixMe[prop-missing]
99115 navigator . scheduling !== undefined &&
116+ // $FlowFixMe[incompatible-type]
100117 navigator . scheduling . isInputPending !== undefined
101118 ? navigator . scheduling . isInputPending . bind ( navigator . scheduling )
102119 : null ;
@@ -247,7 +264,10 @@ function workLoop(hasTimeRemaining, initialTime) {
247264 }
248265}
249266
250- function unstable_runWithPriority ( priorityLevel , eventHandler ) {
267+ function unstable_runWithPriority < T > (
268+ priorityLevel: PriorityLevel,
269+ eventHandler: () => T ,
270+ ) : T {
251271 switch ( priorityLevel ) {
252272 case ImmediatePriority :
253273 case UserBlockingPriority :
@@ -269,7 +289,7 @@ function unstable_runWithPriority(priorityLevel, eventHandler) {
269289 }
270290}
271291
272- function unstable_next ( eventHandler ) {
292+ function unstable_next < T > (eventHandler: () = > T ) : T {
273293 var priorityLevel ;
274294 switch ( currentPriorityLevel ) {
275295 case ImmediatePriority :
@@ -294,8 +314,9 @@ function unstable_next(eventHandler) {
294314 }
295315}
296316
297- function unstable_wrapCallback ( callback ) {
317+ function unstable_wrapCallback < T : ( ... Array < mixed > ) => mixed > ( callback : T ) : T {
298318 var parentPriorityLevel = currentPriorityLevel ;
319+ // $FlowFixMe[incompatible-return]
299320 return function ( ) {
300321 // This is a fork of runWithPriority, inlined for performance.
301322 var previousPriorityLevel = currentPriorityLevel ;
@@ -309,7 +330,11 @@ function unstable_wrapCallback(callback) {
309330 } ;
310331}
311332
312- function unstable_scheduleCallback ( priorityLevel , callback , options ) {
333+ function unstable_scheduleCallback (
334+ priorityLevel : PriorityLevel ,
335+ callback : Callback ,
336+ options ?: { delay : number } ,
337+ ) : Task {
313338 var currentTime = getCurrentTime ( ) ;
314339
315340 var startTime ;
@@ -346,7 +371,7 @@ function unstable_scheduleCallback(priorityLevel, callback, options) {
346371
347372 var expirationTime = startTime + timeout ;
348373
349- var newTask = {
374+ var newTask : Task = {
350375 id : taskIdCounter ++ ,
351376 callback,
352377 priorityLevel,
@@ -403,11 +428,11 @@ function unstable_continueExecution() {
403428 }
404429}
405430
406- function unstable_getFirstCallbackNode ( ) {
431+ function unstable_getFirstCallbackNode ( ) : Task | null {
407432 return peek ( taskQueue ) ;
408433}
409434
410- function unstable_cancelCallback ( task ) {
435+ function unstable_cancelCallback ( task : Task ) {
411436 if ( enableProfiling ) {
412437 if ( task . isQueued ) {
413438 const currentTime = getCurrentTime ( ) ;
@@ -422,13 +447,18 @@ function unstable_cancelCallback(task) {
422447 task . callback = null ;
423448}
424449
425- function unstable_getCurrentPriorityLevel ( ) {
450+ function unstable_getCurrentPriorityLevel ( ) : PriorityLevel {
426451 return currentPriorityLevel ;
427452}
428453
429454let isMessageLoopRunning = false ;
430- let scheduledHostCallback = null ;
431- let taskTimeoutID = - 1 ;
455+ let scheduledHostCallback :
456+ | null
457+ | ( (
458+ hasTimeRemaining : boolean ,
459+ initialTime : DOMHighResTimeStamp | number ,
460+ ) => boolean ) = null ;
461+ let taskTimeoutID : TimeoutID = ( - 1 : any ) ;
432462
433463// Scheduler periodically yields in case there is other work on the main
434464// thread, like user events. By default, it yields multiple times per frame.
@@ -441,7 +471,7 @@ let startTime = -1;
441471
442472let needsPaint = false ;
443473
444- function shouldYieldToHost ( ) {
474+ function shouldYieldToHost ( ) : boolean {
445475 const timeElapsed = getCurrentTime ( ) - startTime ;
446476 if ( timeElapsed < frameInterval ) {
447477 // The main thread has only been blocked for a really short amount of time;
@@ -490,7 +520,9 @@ function requestPaint() {
490520 if (
491521 enableIsInputPending &&
492522 navigator !== undefined &&
523+ // $FlowFixMe[prop-missing]
493524 navigator . scheduling !== undefined &&
525+ // $FlowFixMe[incompatible-type]
494526 navigator . scheduling . isInputPending !== undefined
495527 ) {
496528 needsPaint = true ;
@@ -499,7 +531,7 @@ function requestPaint() {
499531 // Since we yield every frame regardless, `requestPaint` has no effect.
500532}
501533
502- function forceFrameRate ( fps ) {
534+ function forceFrameRate ( fps : number ) {
503535 if ( fps < 0 || fps > 125 ) {
504536 // Using console['error'] to evade Babel and ESLint
505537 console [ 'error' ] (
@@ -579,6 +611,7 @@ if (typeof localSetImmediate === 'function') {
579611} else {
580612 // We should only fallback here in non-browser environments.
581613 schedulePerformWorkUntilDeadline = ( ) => {
614+ // $FlowFixMe[not-a-function] nullable value
582615 localSetTimeout ( performWorkUntilDeadline , 0 ) ;
583616 } ;
584617}
@@ -592,14 +625,16 @@ function requestHostCallback(callback) {
592625}
593626
594627function requestHostTimeout ( callback , ms ) {
628+ // $FlowFixMe[not-a-function] nullable value
595629 taskTimeoutID = localSetTimeout ( ( ) => {
596630 callback ( getCurrentTime ( ) ) ;
597631 } , ms ) ;
598632}
599633
600634function cancelHostTimeout ( ) {
635+ // $FlowFixMe[not-a-function] nullable value
601636 localClearTimeout ( taskTimeoutID ) ;
602- taskTimeoutID = - 1 ;
637+ taskTimeoutID = ( ( - 1 : any ) : TimeoutID ) ;
603638}
604639
605640export {
@@ -623,7 +658,10 @@ export {
623658 forceFrameRate as unstable_forceFrameRate ,
624659} ;
625660
626- export const unstable_Profiling = enableProfiling
661+ export const unstable_Profiling : {
662+ startLoggingProfilingEvents ( ) : void ,
663+ stopLoggingProfilingEvents ( ) : ArrayBuffer | null ,
664+ } | null = enableProfiling
627665 ? {
628666 startLoggingProfilingEvents,
629667 stopLoggingProfilingEvents,
0 commit comments