Skip to content

Commit c7df298

Browse files
update docs
1 parent b04aeeb commit c7df298

File tree

3 files changed

+19
-86
lines changed

3 files changed

+19
-86
lines changed

docs/React#updateQueue.md

Lines changed: 1 addition & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -71,38 +71,28 @@ export function processUpdateQueue<State>(
7171
instance: any,
7272
renderExpirationTime: ExpirationTime,
7373
): void {
74-
// This is always non-null on a ClassComponent or HostRoot
7574
const queue: UpdateQueue<State> = (workInProgress.updateQueue: any);
7675

7776
hasForceUpdate = false;
7877

7978
let firstBaseUpdate = queue.firstBaseUpdate;
8079
let lastBaseUpdate = queue.lastBaseUpdate;
8180

82-
// Check if there are pending updates. If so, transfer them to the base queue.
8381
// 查看当前fiber上的BaseQueue是否有没处理的update,有的话把pendingQueue链到后面,没有就直接复制,清空pendingQueue
8482
let pendingQueue = queue.shared.pending;
8583
if (pendingQueue !== null) {
8684
queue.shared.pending = null;
8785

88-
// The pending queue is circular. Disconnect the pointer between first
89-
// and last so that it's non-circular.
9086
const lastPendingUpdate = pendingQueue;
9187
const firstPendingUpdate = lastPendingUpdate.next;
9288
lastPendingUpdate.next = null;
93-
// Append pending updates to base queue
9489
if (lastBaseUpdate === null) {
9590
firstBaseUpdate = firstPendingUpdate;
9691
} else {
9792
lastBaseUpdate.next = firstPendingUpdate;
9893
}
9994
lastBaseUpdate = lastPendingUpdate;
10095

101-
// If there's a current queue, and it's different from the base queue, then
102-
// we need to transfer the updates to that queue, too. Because the base
103-
// queue is a singly-linked list with no cycles, we can append to both
104-
// lists and take advantage of structural sharing.
105-
// TODO: Pass `current` as argument
10696
const current = workInProgress.alternate;
10797
if (current !== null) {
10898
// This is always non-null on a ClassComponent or HostRoot
@@ -119,9 +109,7 @@ export function processUpdateQueue<State>(
119109
}
120110
}
121111

122-
// These values may change as we process the queue.
123112
if (firstBaseUpdate !== null) {
124-
// Iterate through the list of updates to compute the result.
125113
let newState = queue.baseState;
126114
let newExpirationTime = NoWork;
127115

@@ -134,9 +122,6 @@ export function processUpdateQueue<State>(
134122
const updateExpirationTime = update.expirationTime;
135123
if (updateExpirationTime < renderExpirationTime) {
136124
// update优先级不够,不执行,拷贝一份当前的update,完事后赋值给BaseQueue,等待下一次调度
137-
// Priority is insufficient. Skip this update. If this is the first
138-
// skipped update, the previous update/state is the new base
139-
// update/state.
140125
const clone: Update<State> = {
141126
expirationTime: update.expirationTime,
142127
suspenseConfig: update.suspenseConfig,
@@ -153,13 +138,11 @@ export function processUpdateQueue<State>(
153138
} else {
154139
newLastBaseUpdate = newLastBaseUpdate.next = clone;
155140
}
156-
// Update the remaining priority in the queue.
157141
if (updateExpirationTime > newExpirationTime) {
158142
newExpirationTime = updateExpirationTime;
159143
}
160144
} else {
161145
// update先级足够
162-
// This update does have sufficient priority.
163146

164147
// 应该是中断相关,猜测是需要保证update调用的连续性,否则可能有多个结果
165148
if (newLastBaseUpdate !== null) {
@@ -212,13 +195,6 @@ export function processUpdateQueue<State>(
212195
queue.firstBaseUpdate = newFirstBaseUpdate;
213196
queue.lastBaseUpdate = newLastBaseUpdate;
214197

215-
// Set the remaining expiration time to be whatever is remaining in the queue.
216-
// This should be fine because the only two other things that contribute to
217-
// expiration time are props and context. We're already in the middle of the
218-
// begin phase by the time we start processing the queue, so we've already
219-
// dealt with the props. Context in components that specify
220-
// shouldComponentUpdate is tricky; but we'll have to account for
221-
// that regardless.
222198
markUnprocessedUpdateTime(newExpirationTime);
223199
workInProgress.expirationTime = newExpirationTime;
224200
workInProgress.memoizedState = newState;
@@ -259,7 +235,6 @@ function mountState<S>(
259235
): [S, Dispatch<BasicStateAction<S>>] {
260236
const hook = mountWorkInProgressHook();
261237
if (typeof initialState === 'function') {
262-
// $FlowFixMe: Flow doesn't like mixed types
263238
initialState = initialState();
264239
}
265240
hook.memoizedState = hook.baseState = initialState;
@@ -294,17 +269,12 @@ function updateReducer<S, I, A>(
294269

295270
const current: Hook = ();
296271

297-
// The last rebase update that is NOT part of the base state.
298272
let baseQueue = current.baseQueue;
299273

300-
// The last pending update that hasn't been processed yet.
301274
// 把pengdingQueue链接到baseQueue后,清除pengding
302275
const pendingQueue = queue.pending;
303276
if (pendingQueue !== null) {
304-
// We have new updates that haven't been processed yet.
305-
// We'll add them to the base queue.
306277
if (baseQueue !== null) {
307-
// Merge the pending queue and the base queue.
308278
const baseFirst = baseQueue.next;
309279
const pendingFirst = pendingQueue.next;
310280
baseQueue.next = pendingFirst;
@@ -315,7 +285,6 @@ function updateReducer<S, I, A>(
315285
}
316286

317287
if (baseQueue !== null) {
318-
// We have a queue to process.
319288
const first = baseQueue.next;
320289
let newState = current.baseState;
321290

@@ -327,9 +296,6 @@ function updateReducer<S, I, A>(
327296
const updateExpirationTime = update.expirationTime;
328297
if (updateExpirationTime < renderExpirationTime) {
329298
// 优先级不足,把这个update链到newBaseQueueFirst,等循环结束后,重设到baseQueue, 等待下一次的调度
330-
// Priority is insufficient. Skip this update. If this is the first
331-
// skipped update, the previous update/state is the new base
332-
// update/state.
333299
const clone: Update<S, A> = {
334300
expirationTime: update.expirationTime,
335301
suspenseConfig: update.suspenseConfig,
@@ -344,17 +310,14 @@ function updateReducer<S, I, A>(
344310
} else {
345311
newBaseQueueLast = newBaseQueueLast.next = clone;
346312
}
347-
// Update the remaining priority in the queue.
348313
if (updateExpirationTime > currentlyRenderingFiber.expirationTime) {
349314
currentlyRenderingFiber.expirationTime = updateExpirationTime;
350315
markUnprocessedUpdateTime(updateExpirationTime);
351316
}
352317
} else {
353-
// This update does have sufficient priority.
354-
355318
if (newBaseQueueLast !== null) {
356319
const clone: Update<S, A> = {
357-
expirationTime: Sync, // This update is going to be committed so we never want uncommit it.
320+
expirationTime: Sync,
358321
suspenseConfig: update.suspenseConfig,
359322
action: update.action,
360323
eagerReducer: update.eagerReducer,
@@ -364,21 +327,8 @@ function updateReducer<S, I, A>(
364327
newBaseQueueLast = newBaseQueueLast.next = clone;
365328
}
366329

367-
// Mark the event time of this update as relevant to this render pass.
368-
// TODO: This should ideally use the true event time of this update rather than
369-
// its priority which is a derived and not reverseable value.
370-
// TODO: We should skip this update if it was already committed but currently
371-
// we have no way of detecting the difference between a committed and suspended
372-
// update here.
373-
markRenderEventTimeAndConfig(
374-
updateExpirationTime,
375-
update.suspenseConfig,
376-
);
377-
378330
// 根据reducer处理旧state,生成新state
379331
if (update.eagerReducer === reducer) {
380-
// If this update was processed eagerly, and its reducer matches the
381-
// current reducer, we can use the eagerly computed state.
382332
newState = ((update.eagerState: any): S);
383333
} else {
384334
const action = update.action;
@@ -439,11 +389,9 @@ function dispatchAction<S, A>(
439389
next: (null: any),
440390
};
441391

442-
// Append the update to the end of the list.
443392
// 把新update挂载到pending上
444393
const pending = queue.pending;
445394
if (pending === null) {
446-
// This is the first update. Create a circular list.
447395
update.next = update;
448396
} else {
449397
update.next = pending.next;
@@ -457,38 +405,24 @@ function dispatchAction<S, A>(
457405
(alternate !== null && alternate === currentlyRenderingFiber)
458406
) {
459407
// 在render阶段创建了更新,需要确认是不是死循环
460-
// This is a render phase update. Stash it in a lazily-created map of
461-
// queue -> linked list of updates. After this render pass, we'll restart
462-
// and apply the stashed updates on top of the work-in-progress hook.
463408
didScheduleRenderPhaseUpdateDuringThisPass = didScheduleRenderPhaseUpdate = true;
464409
update.expirationTime = renderExpirationTime;
465410
} else {
466411
if (
467412
fiber.expirationTime === NoWork &&
468413
(alternate === null || alternate.expirationTime === NoWork)
469414
) {
470-
// The queue is currently empty, which means we can eagerly compute the
471-
// next state before entering the render phase. If the new state is the
472-
// same as the current state, we may be able to bail out entirely.
473415
const lastRenderedReducer = queue.lastRenderedReducer;
474416
if (lastRenderedReducer !== null) {
475417
let prevDispatcher;
476418
try {
477419
const currentState: S = (queue.lastRenderedState: any);
478420
// 根据[state, setState],根据setState创建出新的state
479421
const eagerState = lastRenderedReducer(currentState, action);
480-
// Stash the eagerly computed state, and the reducer used to compute
481-
// it, on the update object. If the reducer hasn't changed by the
482-
// time we enter the render phase, then the eager state can be used
483-
// without calling the reducer again.
484422
update.eagerReducer = lastRenderedReducer;
485423
update.eagerState = eagerState;
486424
// 新老state对比,没有更新则可以不启动调度
487425
if (is(eagerState, currentState)) {
488-
// Fast path. We can bail out without scheduling React to re-render.
489-
// It's still possible that we'll need to rebase this update later,
490-
// if the component re-renders for a different reason and by that
491-
// time the reducer has changed.
492426
return;
493427
}
494428
} catch (error) {

docs/React#useEffect#useLayoutEffect.md

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
```js
1010
function mountEffect(
11-
create: () => (() => void) | void,
12-
deps: Array<mixed> | void | null,
13-
): void {
11+
create,
12+
deps,
13+
) {
1414
return mountEffectImpl(
1515
UpdateEffect | PassiveEffect,
1616
HookPassive,
@@ -20,9 +20,9 @@ function mountEffect(
2020
}
2121

2222
function updateEffect(
23-
create: () => (() => void) | void,
24-
deps: Array<mixed> | void | null,
25-
): void {
23+
create,
24+
deps,
25+
) {
2626
return updateEffectImpl(
2727
UpdateEffect | PassiveEffect,
2828
HookPassive,
@@ -35,24 +35,24 @@ function updateEffect(
3535

3636
```js
3737
function mountLayoutEffect(
38-
create: () => (() => void) | void,
39-
deps: Array<mixed> | void | null,
40-
): void {
38+
create,
39+
deps,
40+
) {
4141
return mountEffectImpl(UpdateEffect, HookLayout, create, deps);
4242
}
4343

4444
function updateLayoutEffect(
45-
create: () => (() => void) | void,
46-
deps: Array<mixed> | void | null,
47-
): void {
45+
create,
46+
deps,
47+
) {
4848
return updateEffectImpl(UpdateEffect, HookLayout, create, deps);
4949
}
5050
```
5151

5252
### 通用
5353

5454
```js
55-
function mountEffectImpl(fiberEffectTag, hookEffectTag, create, deps): void {
55+
function mountEffectImpl(fiberEffectTag, hookEffectTag, create, deps) {
5656
const hook = mountWorkInProgressHook();
5757
const nextDeps = deps === undefined ? null : deps;
5858
currentlyRenderingFiber.effectTag |= fiberEffectTag;
@@ -64,7 +64,7 @@ function mountEffectImpl(fiberEffectTag, hookEffectTag, create, deps): void {
6464
);
6565
}
6666

67-
function updateEffectImpl(fiberEffectTag, hookEffectTag, create, deps): void {
67+
function updateEffectImpl(fiberEffectTag, hookEffectTag, create, deps) {
6868
const hook = updateWorkInProgressHook();
6969
const nextDeps = deps === undefined ? null : deps;
7070
let destroy = undefined;
@@ -98,12 +98,12 @@ function pushEffect(tag, create, destroy, deps) {
9898
destroy,
9999
deps,
100100
// Circular
101-
next: (null: any),
101+
next: null,
102102
};
103-
let componentUpdateQueue: null | FunctionComponentUpdateQueue = (currentlyRenderingFiber.updateQueue: any);
103+
let componentUpdateQueue = (currentlyRenderingFiber.updateQueue: any);
104104
if (componentUpdateQueue === null) {
105105
componentUpdateQueue = createFunctionComponentUpdateQueue();
106-
currentlyRenderingFiber.updateQueue = (componentUpdateQueue: any);
106+
currentlyRenderingFiber.updateQueue = componentUpdateQueue;
107107
componentUpdateQueue.lastEffect = effect.next = effect;
108108
} else {
109109
const lastEffect = componentUpdateQueue.lastEffect;
@@ -118,7 +118,6 @@ function pushEffect(tag, create, destroy, deps) {
118118
}
119119
return effect;
120120
}
121-
122121
```
123122

124123

update.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"React#updateQueue.md": 1605602314209,
3-
"React中的commit流程.md": 1605341885210,
43
"React#useEffect#useLayoutEffect.md": 1605679096344,
54
"CSS grid介绍.md": 1596791290447,
65
"DNS简介.md": 1596791290447,
76
"ES6 Iterator要点总结.md": 1596791290447,
87
"HTTPS加密过程.md": 1596791290447,
98
"ReactDOM#render工作机制.md": 1602811104559,
109
"React中Context的使用和实现.md": 1601977560699,
10+
"React中的commit流程.md": 1605341885210,
1111
"React中的reconcile流程.md": 1604584251074,
1212
"React中的二进制类型.md": 1596791290447,
1313
"React中的调度算法.md": 1604325802933,

0 commit comments

Comments
 (0)