news 2026/6/26 1:33:05

react源码学习之Scheduler

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
react源码学习之Scheduler

简介

Scheduler主要用于调度执行Reconciler

组成

api

  • unstable_scheduleCallback
  • unstable_cancelCallback

调度回调有两种方式

  • setTimeout
  • 通过MessageChannel

Task

为任务调度单元

type Task={id:number,callback:Callback|null,priorityLevel:PriorityLevel,startTime:number,expirationTime:number,sortIndex:number,isQueued?:boolean,};

双堆

维护两个最小堆,按TasksortIndex排序

  • taskQueue:排序键为expirationTimestartTime小于等于当前时间currentTime的任务放入此堆中
  • timerQueue:排序键为startTimestartTime大于当前时间currentTime的任务放入此堆中
vartaskQueue:Array<Task>=[];vartimerQueue:Array<Task>=[];

优先级 到超时时间 的映射

优先级timeout超时时间
ImmediatePriority-1立即超时
UserBlockingPriorityconst userBlockingPriorityTimeout = 250
IdlePrioritymaxSigned31BitInt = 1073741823永远不会超时
LowPriorityconst lowPriorityTimeout = 10000
NormalPriorityconst normalPriorityTimeout = 5000

调度工作循环workLoop

functionworkLoop(initialTime:number){letcurrentTime=initialTime;advanceTimers(currentTime);currentTask=peek(taskQueue);while(currentTask!==null){if(!enableAlwaysYieldScheduler){if(currentTask.expirationTime>currentTime&&shouldYieldToHost()){// This currentTask hasn't expired, and we've reached the deadline.break;}}// $FlowFixMe[incompatible-use] found when upgrading Flowconstcallback=currentTask.callback;if(typeofcallback==='function'){// $FlowFixMe[incompatible-use] found when upgrading FlowcurrentTask.callback=null;// $FlowFixMe[incompatible-use] found when upgrading FlowcurrentPriorityLevel=currentTask.priorityLevel;// $FlowFixMe[incompatible-use] found when upgrading FlowconstdidUserCallbackTimeout=currentTask.expirationTime<=currentTime;if(enableProfiling){// $FlowFixMe[incompatible-call] found when upgrading FlowmarkTaskRun(currentTask,currentTime);}constcontinuationCallback=callback(didUserCallbackTimeout);currentTime=getCurrentTime();if(typeofcontinuationCallback==='function'){// If a continuation is returned, immediately yield to the main thread// regardless of how much time is left in the current time slice.// $FlowFixMe[incompatible-use] found when upgrading FlowcurrentTask.callback=continuationCallback;if(enableProfiling){// $FlowFixMe[incompatible-call] found when upgrading FlowmarkTaskYield(currentTask,currentTime);}advanceTimers(currentTime);returntrue;}else{if(enableProfiling){// $FlowFixMe[incompatible-call] found when upgrading FlowmarkTaskCompleted(currentTask,currentTime);// $FlowFixMe[incompatible-use] found when upgrading FlowcurrentTask.isQueued=false;}if(currentTask===peek(taskQueue)){pop(taskQueue);}advanceTimers(currentTime);}}else{pop(taskQueue);}currentTask=peek(taskQueue);if(enableAlwaysYieldScheduler){if(currentTask===null||currentTask.expirationTime>currentTime){// This currentTask hasn't expired we yield to the browser task.break;}}}// Return whether there's additional workif(currentTask!==null){returntrue;}else{constfirstTimer=peek(timerQueue);if(firstTimer!==null){requestHostTimeout(handleTimeout,firstTimer.startTime-currentTime);}returnfalse;}}

AI数字营销平台

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/26 1:29:36

HDFS javaAPI-windows的IDEA中java文件在linux中的hadoop平台运行

运行前提1、windows能ping通虚拟机IP地址【虚拟机网络改为桥接模式&#xff0c;改为固定IP】2、Linux开放端口&#xff1a;9000,9870,8088【使用windows的 PowerShell&#xff0c;执行&#xff1a;Test-NetConnection IP地址 -Port 9000&#xff0c;需要修改linux中hadoop配置文…

作者头像 李华
网站建设 2026/6/26 1:28:16

X浏览器:免费又极简,建议收藏 !

手机上自带的浏览器要么臃肿要么广告满天飞&#xff0c;用着糟心。之前一直用夸克&#xff0c;后来广告越来越多就换了&#xff0c;折腾了好几个才发先X浏览器&#xff0c;用到现在没再换过。这浏览器安装包才2MB&#xff0c;启动基本是秒开&#xff0c;装完占的存储也小德离谱…

作者头像 李华
网站建设 2026/6/26 1:27:35

Web渗透测试实战入门:从信息收集到漏洞利用的标准化流程

1. 项目概述&#xff1a;从“黑盒”到“白盒”的实战思维刚入行那会儿&#xff0c;看别人挖洞、渗透&#xff0c;总觉得像电影里的黑客&#xff0c;敲几行代码&#xff0c;网站就“沦陷”了&#xff0c;神秘又刺激。后来自己上手&#xff0c;才发现根本不是那么回事。真正的网站…

作者头像 李华
网站建设 2026/6/26 1:26:29

MuleSoft企业级AI编排:LLM服务化治理与生产落地实践

1. 项目概述&#xff1a;当企业级集成平台遇上大语言模型“AI Orchestration in Action: How MuleSoft and LLMs Fuel the Future of Enterprise AI”——这个标题不是一句空泛的行业口号&#xff0c;而是我在过去18个月里亲手落地的三个生产级AI增强型集成项目的统一内核。它讲…

作者头像 李华
网站建设 2026/6/26 1:24:03

TestDisk PhotoRec终极指南:三步快速恢复丢失的分区和文件

TestDisk & PhotoRec终极指南&#xff1a;三步快速恢复丢失的分区和文件 【免费下载链接】testdisk TestDisk & PhotoRec 项目地址: https://gitcode.com/gh_mirrors/te/testdisk 你是否曾因误删分区而惊慌失措&#xff1f;是否因格式化硬盘而追悔莫及&#xff…

作者头像 李华
网站建设 2026/6/26 1:20:45

课堂笔记写不完不会整理?2026如何快速整理课堂笔记哪个好怎么选

针对“课堂笔记写不完不会整理&#xff0c;2026如何快速整理课堂笔记哪个好怎么选”这个问题&#xff0c;直接给核心结论&#xff1a;选工具优先看三个标准&#xff0c;一是课堂录音转写准确率够不够&#xff0c;二是整理完能不能直接辅助复习记忆&#xff0c;三是能不能适配论…

作者头像 李华