Skip to content

Commit 135e33c

Browse files
authored
Flow: typing of Scheduler (#25317)
Enables well formed exports for /scheduler. Some of the modules there were missing `@flow` and were therefore completely unchecked (despite some spurious types sprinkled around).
1 parent cc8cb14 commit 135e33c

9 files changed

+120
-46
lines changed

packages/scheduler/src/SchedulerFeatureFlags.js

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
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 strict
78
*/
89

910
export const enableSchedulerDebugging = false;

packages/scheduler/src/SchedulerMinHeap.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,24 @@
77
* @flow strict
88
*/
99

10-
type Heap = Array<Node>;
10+
type Heap<T: Node> = Array<T>;
1111
type Node = {
1212
id: number,
1313
sortIndex: number,
14+
...
1415
};
1516

16-
export function push(heap: Heap, node: Node): void {
17+
export function push<T: Node>(heap: Heap<T>, node: T): void {
1718
const index = heap.length;
1819
heap.push(node);
1920
siftUp(heap, node, index);
2021
}
2122

22-
export function peek(heap: Heap): Node | null {
23+
export function peek<T: Node>(heap: Heap<T>): T | null {
2324
return heap.length === 0 ? null : heap[0];
2425
}
2526

26-
export function pop(heap: Heap): Node | null {
27+
export function pop<T: Node>(heap: Heap<T>): T | null {
2728
if (heap.length === 0) {
2829
return null;
2930
}
@@ -36,7 +37,7 @@ export function pop(heap: Heap): Node | null {
3637
return first;
3738
}
3839

39-
function siftUp(heap, node, i) {
40+
function siftUp<T: Node>(heap: Heap<T>, node: T, i: number): void {
4041
let index = i;
4142
while (index > 0) {
4243
const parentIndex = (index - 1) >>> 1;
@@ -53,7 +54,7 @@ function siftUp(heap, node, i) {
5354
}
5455
}
5556

56-
function siftDown(heap, node, i) {
57+
function siftDown<T: Node>(heap: Heap<T>, node: T, i: number): void {
5758
let index = i;
5859
const length = heap.length;
5960
const halfLength = length >>> 1;
@@ -85,7 +86,7 @@ function siftDown(heap, node, i) {
8586
}
8687
}
8788

88-
function compare(a, b) {
89+
function compare(a: Node, b: Node) {
8990
// Compare sort index first, then task id.
9091
const diff = a.sortIndex - b.sortIndex;
9192
return diff !== 0 ? diff : a.id - b.id;

packages/scheduler/src/SchedulerPriorities.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
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
7+
* @flow strict
88
*/
99

1010
export type PriorityLevel = 0 | 1 | 2 | 3 | 4 | 5;

packages/scheduler/src/forks/Scheduler.js

+53-15
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
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+
1114
import {
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;
4560
const hasPerformanceNow =
4661
typeof performance === 'object' && typeof performance.now === 'function';
4762

@@ -96,7 +111,9 @@ const localSetImmediate =
96111

97112
const 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

429454
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);
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

442472
let 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

594627
function requestHostTimeout(callback, ms) {
628+
// $FlowFixMe[not-a-function] nullable value
595629
taskTimeoutID = localSetTimeout(() => {
596630
callback(getCurrentTime());
597631
}, ms);
598632
}
599633

600634
function cancelHostTimeout() {
635+
// $FlowFixMe[not-a-function] nullable value
601636
localClearTimeout(taskTimeoutID);
602-
taskTimeoutID = -1;
637+
taskTimeoutID = ((-1: any): TimeoutID);
603638
}
604639

605640
export {
@@ -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,

packages/scheduler/src/forks/SchedulerFeatureFlags.www-dynamic.js

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
*
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
6-
*
76
*/
87

98
// In www, these flags are controlled by GKs. Because most GKs have some

packages/scheduler/src/forks/SchedulerFeatureFlags.www.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
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

10+
// $FlowFixMe[cannot-resolve-module]
911
const dynamicFeatureFlags = require('SchedulerFeatureFlags');
1012

1113
// Re-export dynamic flags from the www version.
@@ -19,4 +21,5 @@ export const {
1921
maxYieldMs,
2022
} = dynamicFeatureFlags;
2123

22-
export const enableProfiling = __PROFILE__ && enableProfilingFeatureFlag;
24+
export const enableProfiling: boolean =
25+
__PROFILE__ && enableProfilingFeatureFlag;

0 commit comments

Comments
 (0)