Skip to content

Commit 9c4e1c8

Browse files
committed
流程图
1 parent 51f0e22 commit 9c4e1c8

File tree

11 files changed

+87
-13
lines changed

11 files changed

+87
-13
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "index.js",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1",
8-
"mmdc:scheduler": "npx mmdc -i ./packages/scheduler/flow.mmd -o ./packages/scheduler/images/flow.svg"
8+
"mmdc:scheduler": "./script/mmdc.sh packages/scheduler/mmd"
99
},
1010
"repository": {
1111
"type": "git",

packages/scheduler/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@
22

33
这是React内部使用的调度系统,核心入口函数是unstable_scheduleCallback。以下是流程图,详细的解释在代码里面看。
44

5-
![流程图](./images/flow.svg)
5+
![流程图1](./mmd/global.mmd.svg)
6+
7+
![流程图2](./mmd/workloop.mmd.svg)
8+
9+
![流程3](./mmd/flow.mmd.svg)

packages/scheduler/images/flow.svg

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/scheduler/flow.mmd renamed to packages/scheduler/mmd/flow.mmd

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
graph TD;
33

4+
classDef someclass fill:#f96;
5+
46
main((入口)) --> task;
57

68
task --> unstable_scheduleCallback;
@@ -13,7 +15,6 @@ push -- 如果当前不存在调度流程 --> requestHostCallback;
1315

1416
push -- 设置了延迟时间 --> timerQueue;
1517

16-
1718
requestHostCallback --->schedulePerformWorkUntilDeadline;
1819

1920
requestHostCallback -- 将flushWork保存到全局作为回调函数 --> flushWork;
@@ -38,7 +39,6 @@ performWorkUntilDeadline --> flushWork;
3839

3940
performWorkUntilDeadline --将deadline设置为当前时间加yieldInterval --> deadline
4041

41-
yieldInterval --> 5ms;
4242

4343
isHostCallbackScheduled;
4444

@@ -52,13 +52,7 @@ flushWork -- true --> isPerformingWork;
5252

5353
flushWork -- 保存当前环境变量 --> workLoop;
5454

55-
workLoop --> advanceTimers;
56-
57-
advanceTimers -- 检查延迟队列 --> timerQueue;
58-
59-
advanceTimers --取出过期任务到过期队列--> taskQueue;
60-
61-
workLoop --> D2{检查是否满足执行条件}
55+
workLoop:::someclass --> D2{检查是否满足执行条件}
6256

6357
D2 --> runTask[执行任务task];
6458

packages/scheduler/mmd/flow.mmd.svg

Lines changed: 1 addition & 0 deletions
Loading

packages/scheduler/mmd/global.mmd

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
graph TD;
2+
3+
g[全局变量]
4+
5+
var1>yieldInterval = 5ms];
6+
7+
var2>timerQueue:延迟任务队列];
8+
9+
var3>taskQueue:过期任务队列];
10+
11+
var4>isHostCallbackScheduled:是否正在执行调度逻辑]
12+
13+
var5>isHostTimeoutScheduled:是否有定时任务计时器]
14+
15+
g --- var1
16+
g --- var2
17+
g --- var3
18+
g --- var4
19+
g --- var5
20+
21+

packages/scheduler/mmd/global.mmd.svg

Lines changed: 1 addition & 0 deletions
Loading

packages/scheduler/mmd/workloop.mmd

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
sequenceDiagram title: workLoop
2+
participant flushWork
3+
participant workLoop
4+
participant advanceTimers
5+
participant timerQueue
6+
participant taskQueue
7+
8+
participant task
9+
10+
flushWork ->> workLoop:调用
11+
loop while循环
12+
workLoop -->>+advanceTimers:调用advanceTimers
13+
loop while循环
14+
advanceTimers ->>timerQueue:peek
15+
alt 队列为空
16+
advanceTimers ->> workLoop:返回
17+
else 队列不为空
18+
alt 最前面的任务已过期
19+
advanceTimers ->>timerQueue:pop
20+
advanceTimers ->>taskQueue:push
21+
else 没有任务过期
22+
advanceTimers ->>-workLoop:
23+
end
24+
end
25+
end
26+
27+
loop while循环
28+
workLoop ->> taskQueue:peek
29+
alt 调度没被暂停
30+
alt 当前已经没时间了,或者有输入绘制事件
31+
workLoop ->> workLoop:跳出循环
32+
else 执行需要被调度的任务回调
33+
workLoop ->> +task:执行回调
34+
task --> -workLoop: 返回需要链式调用的回调函数
35+
end
36+
end
37+
end
38+
end
39+
40+
alt 任务已经全部被执行
41+
workLoop ->> workLoop: 启动定时器requestHostTimeout
42+
end
43+
workLoop ->> flushWork: 返回
44+

packages/scheduler/mmd/workloop.mmd.svg

Lines changed: 1 addition & 0 deletions
Loading

packages/scheduler/src/forks/Scheduler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ function workLoop(hasTimeRemaining: boolean, initialTime: number) {
307307
currentTask.isQueued = false;
308308
}
309309
/**
310-
* 如果不存在后续的任务需要执行,则弹出当前任务
310+
* 弹出已被执行的任务
311311
*/
312312
if (currentTask === peek(taskQueue)) {
313313
pop(taskQueue);

0 commit comments

Comments
 (0)