Hi everyone, I'm Kasong.
Scheduler (Scheduler) is an important part React
At the same time, it is also an independent package. Any continuous and interruptible process can be Scheduler
, such as:
const work = {count: 100};
function doWork(work) {
work.count--;
console.log('do work!')
}
work
satisfies two conditions:
- Work is continuous. It needs to be executed 100 times in total, and
doWork
- Work can be interrupted. After the interruption is resumed, continue to execute
work.count
Work that meets these two conditions can be scheduled Scheduler
After scheduling, Scheduler
internally generate the corresponding task
, and executed at the right time task.callback
:
const task1 = {
// 过期时间 等于 当前时间 + 优先级对应时间
expirationTime: currentTime + priority,
callback: doWork.bind(null, work)
}
This article will explain the implementation principle of Scheduler
Knowing that you don't like to look at large sections of code, so there is no line of code in this article. At the end of the article, there is Scheduler
. If you are interested, you can check it out.
Opening~
Work flow overview
Scheduler
is as shown in the figure below, which will be explained in detail next:
There are two confusing concepts in Scheduler
- delay
delay
represents the task need to delay execution time . Configuring the delay
of task
will first enter timerQueue
in.
When the corresponding time of delay
task
will be transferred to taskQueue
.
- expirationTime
expirationTime
representatives Task expiration time .
Not all task
will be configured with delay
, delay
without task
will directly enter taskQueue
. This leads to taskQueue
may exist in multiple task
.
How to decide which task.callback
to execute first? Scheduler
based on task.expirationTime
, the smaller the value, the higher the priority.
If task.expirationTime
less than the current time, not only will the priority be the highest, but task.callback
will not be interrupted.
Summarize several situations of task
- Configure
delay
anddelay
not expired:task
must not be executed - Configure
delay
and expire, or do not configuredelay
oftask
, andtask.expirationTime
not expired:task.expirationTime
sorting according to 061c9334f432bb, execute in order task.expirationTime
expired bytask
: the highest priority, synchronous, non-interruptible
Detailed workflow
After replacing the process overview diagram with the Scheduler
, it is as follows:
The complete workflow is as follows:
- Execute
Scheduler.scheduleCallback
generatetask
According to passes the delay parameter , the generated task
will enter timerQueue
or taskQueue
.
- When
timerQueue
the firsttask
after the delay time has expired, executionadvanceTimers
the due Task fromtimerQueue
moved intaskQueue
in
Wherein, timerQueue
, taskQueue
data structure is minor vertex stack implemented
priority queue.
- Next,
requestHostCallback
method, he will in the newexecution of the macro task
workLoop
method
callback in the macro task method of many, Scheduler
use in a browser environment default MessageChannel
achieve.
MessageChannel
is not supported, it will be downgraded to setTimeout
. Node
or old version of IE will use
setImmediate
.
workLoop
method will cyclically consumetaskQueue
intask
(that is, executetask.callback
) until one of the following conditions is met, and the cycle is interrupted:
taskQueue
does not exist intask
- Time slice exhausted
- After the loop is interrupted, if
taskQueue
not empty, go to step 3. IftimerQueue
not empty, go to step 2
Summarize
To sum up, the complete execution process of Scheduler
taskQueue
production (moved fromtimerQueue
or executedscheduleCallback
generation) to the process of consumption (that is, the gray part in the figure), this is an asynchronous looptaskQueue
specific consumption process (i.e., theworkLoop
execution method), which is a synchronization cycle
If you want to know how to use Scheduler React, you can refer to 100 lines of code to implement the React core scheduling function
Welcome to join human high-quality front-end framework group , take flight
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。