main
js 110 lines 4.52 KB
Raw
1 /**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @flow strict
8 */
9
10 import * as Scheduler from './Scheduler';
11 import type {Callback, Task} from './Scheduler';
12 import type {PriorityLevel} from '../SchedulerPriorities';
13 import typeof * as PriorityLevels from '../SchedulerPriorities';
14 import typeof * as SchedulerExportsType from './Scheduler';
15 import typeof * as SchedulerNativeExportsType from './SchedulerNative';
16
17 // This type is supposed to reflect the actual methods and arguments currently supported by the C++ implementation:
18 // https://github.com/facebook/react-native/blob/main/packages/react-native/ReactCommon/react/renderer/runtimescheduler/RuntimeSchedulerBinding.cpp
19 type NativeSchedulerType = {
20 unstable_ImmediatePriority: PriorityLevels['ImmediatePriority'],
21 unstable_UserBlockingPriority: PriorityLevels['UserBlockingPriority'],
22 unstable_NormalPriority: PriorityLevels['NormalPriority'],
23 unstable_IdlePriority: PriorityLevels['IdlePriority'],
24 unstable_LowPriority: PriorityLevels['LowPriority'],
25 unstable_scheduleCallback: (
26 priorityLevel: PriorityLevel,
27 callback: Callback,
28 ) => Task,
29 unstable_cancelCallback: (task: Task) => void,
30 unstable_getCurrentPriorityLevel: () => PriorityLevel,
31 unstable_shouldYield: () => boolean,
32 unstable_requestPaint: () => void,
33 unstable_now: () => DOMHighResTimeStamp,
34 };
35
36 declare const nativeRuntimeScheduler: void | NativeSchedulerType;
37
38 export const unstable_UserBlockingPriority: PriorityLevels['UserBlockingPriority'] =
39 typeof nativeRuntimeScheduler !== 'undefined'
40 ? nativeRuntimeScheduler.unstable_UserBlockingPriority
41 : Scheduler.unstable_UserBlockingPriority;
42
43 export const unstable_NormalPriority: PriorityLevels['NormalPriority'] =
44 typeof nativeRuntimeScheduler !== 'undefined'
45 ? nativeRuntimeScheduler.unstable_NormalPriority
46 : Scheduler.unstable_NormalPriority;
47
48 export const unstable_IdlePriority: PriorityLevels['IdlePriority'] =
49 typeof nativeRuntimeScheduler !== 'undefined'
50 ? nativeRuntimeScheduler.unstable_IdlePriority
51 : Scheduler.unstable_IdlePriority;
52
53 export const unstable_LowPriority: PriorityLevels['LowPriority'] =
54 typeof nativeRuntimeScheduler !== 'undefined'
55 ? nativeRuntimeScheduler.unstable_LowPriority
56 : Scheduler.unstable_LowPriority;
57
58 export const unstable_ImmediatePriority: PriorityLevels['ImmediatePriority'] =
59 typeof nativeRuntimeScheduler !== 'undefined'
60 ? nativeRuntimeScheduler.unstable_ImmediatePriority
61 : Scheduler.unstable_ImmediatePriority;
62
63 export const unstable_scheduleCallback: (
64 priorityLevel: PriorityLevel,
65 callback: Callback,
66 ) => Task =
67 typeof nativeRuntimeScheduler !== 'undefined'
68 ? nativeRuntimeScheduler.unstable_scheduleCallback
69 : Scheduler.unstable_scheduleCallback;
70
71 export const unstable_cancelCallback: (task: Task) => void =
72 typeof nativeRuntimeScheduler !== 'undefined'
73 ? nativeRuntimeScheduler.unstable_cancelCallback
74 : Scheduler.unstable_cancelCallback;
75
76 export const unstable_getCurrentPriorityLevel: () => PriorityLevel =
77 typeof nativeRuntimeScheduler !== 'undefined'
78 ? nativeRuntimeScheduler.unstable_getCurrentPriorityLevel
79 : Scheduler.unstable_getCurrentPriorityLevel;
80
81 export const unstable_shouldYield: () => boolean =
82 typeof nativeRuntimeScheduler !== 'undefined'
83 ? nativeRuntimeScheduler.unstable_shouldYield
84 : Scheduler.unstable_shouldYield;
85
86 export const unstable_requestPaint: () => void =
87 typeof nativeRuntimeScheduler !== 'undefined'
88 ? nativeRuntimeScheduler.unstable_requestPaint
89 : Scheduler.unstable_requestPaint;
90
91 export const unstable_now: () => number | DOMHighResTimeStamp =
92 typeof nativeRuntimeScheduler !== 'undefined'
93 ? nativeRuntimeScheduler.unstable_now
94 : Scheduler.unstable_now;
95
96 // These were never implemented on the native scheduler because React never calls them.
97 // For consistency, let's disable them altogether and make them throw.
98 export const unstable_next: any = throwNotImplemented;
99 export const unstable_runWithPriority: any = throwNotImplemented;
100 export const unstable_wrapCallback: any = throwNotImplemented;
101 export const unstable_forceFrameRate: any = throwNotImplemented;
102 export const unstable_Profiling: any = null;
103
104 function throwNotImplemented() {
105 throw Error('Not implemented.');
106 }
107
108 // Flow magic to verify the exports of this file match the original version.
109 export type {Callback, Task};
110 null as any as SchedulerExportsType as SchedulerNativeExportsType as SchedulerExportsType;