跳到主要内容

React Native 等环境使用的 Scheduler

让调度程序在 React Native 运行更稳定。

一、兼容性支持

兼容性导出
declare const nativeRuntimeScheduler: void | NativeSchedulerType;

export const unstable_UserBlockingPriority: PriorityLevels['UserBlockingPriority'] =
typeof nativeRuntimeScheduler !== 'undefined'
? nativeRuntimeScheduler.unstable_UserBlockingPriority
: Scheduler.unstable_UserBlockingPriority;

export const unstable_NormalPriority: PriorityLevels['NormalPriority'] =
typeof nativeRuntimeScheduler !== 'undefined'
? nativeRuntimeScheduler.unstable_NormalPriority
: Scheduler.unstable_NormalPriority;

export const unstable_IdlePriority: PriorityLevels['IdlePriority'] =
typeof nativeRuntimeScheduler !== 'undefined'
? nativeRuntimeScheduler.unstable_IdlePriority
: Scheduler.unstable_IdlePriority;

export const unstable_LowPriority: PriorityLevels['LowPriority'] =
typeof nativeRuntimeScheduler !== 'undefined'
? nativeRuntimeScheduler.unstable_LowPriority
: Scheduler.unstable_LowPriority;

export const unstable_ImmediatePriority: PriorityLevels['ImmediatePriority'] =
typeof nativeRuntimeScheduler !== 'undefined'
? nativeRuntimeScheduler.unstable_ImmediatePriority
: Scheduler.unstable_ImmediatePriority;

export const unstable_scheduleCallback: (
priorityLevel: PriorityLevel,
callback: Callback,
) => Task =
typeof nativeRuntimeScheduler !== 'undefined'
? nativeRuntimeScheduler.unstable_scheduleCallback
: Scheduler.unstable_scheduleCallback;

export const unstable_cancelCallback: (task: Task) => void =
typeof nativeRuntimeScheduler !== 'undefined'
? nativeRuntimeScheduler.unstable_cancelCallback
: Scheduler.unstable_cancelCallback;

export const unstable_getCurrentPriorityLevel: () => PriorityLevel =
typeof nativeRuntimeScheduler !== 'undefined'
? nativeRuntimeScheduler.unstable_getCurrentPriorityLevel
: Scheduler.unstable_getCurrentPriorityLevel;

export const unstable_shouldYield: () => boolean =
typeof nativeRuntimeScheduler !== 'undefined'
? nativeRuntimeScheduler.unstable_shouldYield
: Scheduler.unstable_shouldYield;

export const unstable_requestPaint: () => void =
typeof nativeRuntimeScheduler !== 'undefined'
? nativeRuntimeScheduler.unstable_requestPaint
: Scheduler.unstable_requestPaint;

export const unstable_now: () => number | DOMHighResTimeStamp =
typeof nativeRuntimeScheduler !== 'undefined'
? nativeRuntimeScheduler.unstable_now
: Scheduler.unstable_now;

二、不支持功能将抛出错误

// These were never implemented on the native scheduler because React never calls them.
// 这些从未在本地调度器上实现过,因为 React 从未调用它们。
// For consistency, let's disable them altogether and make them throw.
// 为了一致性,我们完全禁用它们,并使其抛出异常。
export const unstable_next: any = throwNotImplemented;
export const unstable_runWithPriority: any = throwNotImplemented;
export const unstable_wrapCallback: any = throwNotImplemented;
export const unstable_forceFrameRate: any = throwNotImplemented;
export const unstable_Profiling: any = null;

function throwNotImplemented() {
throw Error('Not implemented.');
}