4
4
* This source code is licensed under the MIT license found in the
5
5
* LICENSE file in the root directory of this source tree.
6
6
*
7
+ * @flow
7
8
*/
8
9
9
10
/* eslint-disable no-var */
10
11
12
+ import type { PriorityLevel } from '../SchedulerPriorities' ;
13
+
11
14
import {
12
15
enableSchedulerDebugging ,
13
16
enableProfiling ,
@@ -41,7 +44,19 @@ import {
41
44
startLoggingProfilingEvents ,
42
45
} from '../SchedulerProfiling' ;
43
46
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 ;
45
60
const hasPerformanceNow =
46
61
typeof performance === 'object' && typeof performance . now === 'function' ;
47
62
@@ -96,7 +111,9 @@ const localSetImmediate =
96
111
97
112
const isInputPending =
98
113
typeof navigator !== 'undefined' &&
114
+ // $FlowFixMe[prop-missing]
99
115
navigator . scheduling !== undefined &&
116
+ // $FlowFixMe[incompatible-type]
100
117
navigator . scheduling . isInputPending !== undefined
101
118
? navigator . scheduling . isInputPending . bind ( navigator . scheduling )
102
119
: null ;
@@ -247,7 +264,10 @@ function workLoop(hasTimeRemaining, initialTime) {
247
264
}
248
265
}
249
266
250
- function unstable_runWithPriority ( priorityLevel , eventHandler ) {
267
+ function unstable_runWithPriority < T > (
268
+ priorityLevel: PriorityLevel,
269
+ eventHandler: () => T ,
270
+ ) : T {
251
271
switch ( priorityLevel ) {
252
272
case ImmediatePriority :
253
273
case UserBlockingPriority :
@@ -269,7 +289,7 @@ function unstable_runWithPriority(priorityLevel, eventHandler) {
269
289
}
270
290
}
271
291
272
- function unstable_next ( eventHandler ) {
292
+ function unstable_next < T > (eventHandler: () = > T ) : T {
273
293
var priorityLevel ;
274
294
switch ( currentPriorityLevel ) {
275
295
case ImmediatePriority :
@@ -294,8 +314,9 @@ function unstable_next(eventHandler) {
294
314
}
295
315
}
296
316
297
- function unstable_wrapCallback ( callback ) {
317
+ function unstable_wrapCallback < T : ( ... Array < mixed > ) => mixed > ( callback : T ) : T {
298
318
var parentPriorityLevel = currentPriorityLevel ;
319
+ // $FlowFixMe[incompatible-return]
299
320
return function ( ) {
300
321
// This is a fork of runWithPriority, inlined for performance.
301
322
var previousPriorityLevel = currentPriorityLevel ;
@@ -309,7 +330,11 @@ function unstable_wrapCallback(callback) {
309
330
} ;
310
331
}
311
332
312
- function unstable_scheduleCallback ( priorityLevel , callback , options ) {
333
+ function unstable_scheduleCallback (
334
+ priorityLevel : PriorityLevel ,
335
+ callback : Callback ,
336
+ options ?: { delay : number } ,
337
+ ) : Task {
313
338
var currentTime = getCurrentTime ( ) ;
314
339
315
340
var startTime ;
@@ -346,7 +371,7 @@ function unstable_scheduleCallback(priorityLevel, callback, options) {
346
371
347
372
var expirationTime = startTime + timeout ;
348
373
349
- var newTask = {
374
+ var newTask : Task = {
350
375
id : taskIdCounter ++ ,
351
376
callback,
352
377
priorityLevel,
@@ -403,11 +428,11 @@ function unstable_continueExecution() {
403
428
}
404
429
}
405
430
406
- function unstable_getFirstCallbackNode ( ) {
431
+ function unstable_getFirstCallbackNode ( ) : Task | null {
407
432
return peek ( taskQueue ) ;
408
433
}
409
434
410
- function unstable_cancelCallback ( task ) {
435
+ function unstable_cancelCallback ( task : Task ) {
411
436
if ( enableProfiling ) {
412
437
if ( task . isQueued ) {
413
438
const currentTime = getCurrentTime ( ) ;
@@ -422,13 +447,18 @@ function unstable_cancelCallback(task) {
422
447
task . callback = null ;
423
448
}
424
449
425
- function unstable_getCurrentPriorityLevel ( ) {
450
+ function unstable_getCurrentPriorityLevel ( ) : PriorityLevel {
426
451
return currentPriorityLevel ;
427
452
}
428
453
429
454
let 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 ) ;
432
462
433
463
// Scheduler periodically yields in case there is other work on the main
434
464
// thread, like user events. By default, it yields multiple times per frame.
@@ -441,7 +471,7 @@ let startTime = -1;
441
471
442
472
let needsPaint = false ;
443
473
444
- function shouldYieldToHost ( ) {
474
+ function shouldYieldToHost ( ) : boolean {
445
475
const timeElapsed = getCurrentTime ( ) - startTime ;
446
476
if ( timeElapsed < frameInterval ) {
447
477
// The main thread has only been blocked for a really short amount of time;
@@ -490,7 +520,9 @@ function requestPaint() {
490
520
if (
491
521
enableIsInputPending &&
492
522
navigator !== undefined &&
523
+ // $FlowFixMe[prop-missing]
493
524
navigator . scheduling !== undefined &&
525
+ // $FlowFixMe[incompatible-type]
494
526
navigator . scheduling . isInputPending !== undefined
495
527
) {
496
528
needsPaint = true ;
@@ -499,7 +531,7 @@ function requestPaint() {
499
531
// Since we yield every frame regardless, `requestPaint` has no effect.
500
532
}
501
533
502
- function forceFrameRate ( fps ) {
534
+ function forceFrameRate ( fps : number ) {
503
535
if ( fps < 0 || fps > 125 ) {
504
536
// Using console['error'] to evade Babel and ESLint
505
537
console [ 'error' ] (
@@ -579,6 +611,7 @@ if (typeof localSetImmediate === 'function') {
579
611
} else {
580
612
// We should only fallback here in non-browser environments.
581
613
schedulePerformWorkUntilDeadline = ( ) => {
614
+ // $FlowFixMe[not-a-function] nullable value
582
615
localSetTimeout ( performWorkUntilDeadline , 0 ) ;
583
616
} ;
584
617
}
@@ -592,14 +625,16 @@ function requestHostCallback(callback) {
592
625
}
593
626
594
627
function requestHostTimeout ( callback , ms ) {
628
+ // $FlowFixMe[not-a-function] nullable value
595
629
taskTimeoutID = localSetTimeout ( ( ) => {
596
630
callback ( getCurrentTime ( ) ) ;
597
631
} , ms ) ;
598
632
}
599
633
600
634
function cancelHostTimeout ( ) {
635
+ // $FlowFixMe[not-a-function] nullable value
601
636
localClearTimeout ( taskTimeoutID ) ;
602
- taskTimeoutID = - 1 ;
637
+ taskTimeoutID = ( ( - 1 : any ) : TimeoutID ) ;
603
638
}
604
639
605
640
export {
@@ -623,7 +658,10 @@ export {
623
658
forceFrameRate as unstable_forceFrameRate ,
624
659
} ;
625
660
626
- export const unstable_Profiling = enableProfiling
661
+ export const unstable_Profiling : {
662
+ startLoggingProfilingEvents ( ) : void ,
663
+ stopLoggingProfilingEvents ( ) : ArrayBuffer | null ,
664
+ } | null = enableProfiling
627
665
? {
628
666
startLoggingProfilingEvents,
629
667
stopLoggingProfilingEvents,
0 commit comments