1 ) 概述
requestIdleCallback
这个API
requestAnimationFrame
来说,它的优先级会低很多requestAnimationFrame
是浏览器要渲染当前帧的时候,调用这个回调2 )时间片源码
requestWork
函数里面,如果 expirationTime 异步的,就会调用 scheduleCallbackWithExpirationTime
function scheduleCallbackWithExpirationTime(
root: FiberRoot,
expirationTime: ExpirationTime,
) {
if (callbackExpirationTime !== NoWork) {
// A callback is already scheduled. Check its expiration time (timeout).
if (expirationTime > callbackExpirationTime) {
// Existing callback has sufficient timeout. Exit.
return;
} else {
if (callbackID !== null) {
// Existing callback has insufficient timeout. Cancel and schedule a
// new one.
cancelDeferredCallback(callbackID);
}
}
// The request callback timer is already running. Don't start a new one.
} else {
startRequestCallbackTimer();
}
callbackExpirationTime = expirationTime;
const currentMs = now() - originalStartTimeMs;
const expirationTimeMs = expirationTimeToMs(expirationTime);
const timeout = expirationTimeMs - currentMs;
callbackID = scheduleDeferredCallback(performAsyncWork, {timeout});
}
callbackExpirationTime
对应的是 上一次调用 React Scheduler 去申请了一个callbackcallbackExpirationTime
!== NoWork
代表之前有一个callback在执行了cancelDeferredCallback
把之前的 cancel 掉startRequestCallbackTimer
这个函数跳过,不涉及主流程,涉及DEV Tool 相关callbackExpirationTime
timeout
scheduleDeferredCallback
这个方法来自于 ReactFiberHostConfig.js
export * from 'react-dom/src/client/ReactDOMHostConfig';
scheduleDeferredCallback
方法export {
unstable_scheduleCallback as scheduleDeferredCallback,
} from 'scheduler';
callbackID = scheduleDeferredCallback(performAsyncWork, {timeout});
cancelDeferredCallback(callbackID);
performAsyncWork
requestWork
中,当 expirationTime === Sync 时,调用的也是 performSyncWork
这个是同步的scheduleCallbackWithExpirationTime
函数,最终调用的是这里的 performAsyncWork