@@ -308,17 +308,34 @@ export function getWorkInProgressRoot(): FiberRoot | null {
308
308
return workInProgressRoot ;
309
309
}
310
310
311
+ /**
312
+ * 该函数只是拿到了一个react定义的currentTime,即大概相对于MAGIC_NUMBER_OFFSET过了多久了
313
+ * 具体计算逻辑如下:
314
+ * 初始化(包括重置了currentEventTime后)、Render、Commit,return msToExpirationTime(now());
315
+ * 其他时候返回上次计算的currentEventTime
316
+ */
311
317
export function requestCurrentTimeForUpdate ( ) {
318
+ // executionContext最开始的值为0b000000
319
+ // RenderContext = 0b010000
320
+ // CommitContext = 0b100000
321
+ // 下面这个判断可以理解为通过executionContext控制当前是Render还是Commit阶段
322
+ // 若是其中之一个阶段,就直接return msToExpirationTime
323
+ // ReactDOM.render可以理解为初始化,并不属于Render或Commit阶段
312
324
if ( ( executionContext & ( RenderContext | CommitContext ) ) !== NoContext ) {
313
325
// We're inside React, so it's fine to read the actual time.
314
326
return msToExpirationTime ( now ( ) ) ;
315
327
}
316
328
// We're not inside React, so we may be in the middle of a browser event.
329
+ // 既不是Render也不是Commit阶段,
330
+ // 并且初始化后也没有调用performConcurrentWorkOnRoot重置currentEventTime
331
+ // 就return 先前计算出来的currentEventTime
317
332
if ( currentEventTime !== NoWork ) {
318
333
// Use the same start time for all updates until we enter React again.
319
334
return currentEventTime ;
320
335
}
321
336
// This is the first update since React yielded. Compute a new start time.
337
+ // 既不是Render也不是Commit阶段,
338
+ // 初始化或调用performConcurrentWorkOnRoot重置了currentEventTime,同第一个判断的return
322
339
currentEventTime = msToExpirationTime ( now ( ) ) ;
323
340
return currentEventTime ;
324
341
}
@@ -327,30 +344,44 @@ export function getCurrentTime() {
327
344
return msToExpirationTime ( now ( ) ) ;
328
345
}
329
346
347
+ /**
348
+ * 该函数用于计算过期时间的,该值越大,优先级越高,返回值有如下几种数字类型
349
+ * Sync: 最大,优先级最高
350
+ * Batched: Sync-1,优先级第二
351
+ * renderExpirationTime: 要么为0,要么是之前已经用该函数计算出来的expirationTime
352
+ * computeInteractiveExpiration: 比renderExpirationTime小一点
353
+ * computeAsyncExpiration: 比computeInteractiveExpiration小一点
354
+ */
330
355
export function computeExpirationForFiber (
331
356
currentTime : ExpirationTime ,
332
357
fiber : Fiber ,
333
358
suspenseConfig : null | SuspenseConfig ,
334
359
) : ExpirationTime {
335
360
const mode = fiber . mode ;
336
361
if ( ( mode & BlockingMode ) === NoMode ) {
337
- return Sync ;
362
+ // BlockingMode=0010,那么只要mode的倒数第二位为0,就认为属于BlockingMode类别
363
+ return Sync ; // Sync,同步,数值最大,优先级最高
338
364
}
339
365
366
+ // 获取当前调度的优先级,调度优先级分类见Scheduler.js
340
367
const priorityLevel = getCurrentPriorityLevel ( ) ;
341
368
if ( ( mode & ConcurrentMode ) === NoMode ) {
369
+ // ConcurrentMode=0100,那么只要mode的倒数第三位为0,就认为属于ConcurrentMode类别
370
+ // Batched = Sync - 1,优先级第二
342
371
return priorityLevel === ImmediatePriority ? Sync : Batched ;
343
372
}
344
373
345
374
if ( ( executionContext & RenderContext ) !== NoContext ) {
346
375
// Use whatever time we're already rendering
347
376
// TODO: Should there be a way to opt out, like with `runWithPriority`?
377
+ // 若是Render阶段,就返回renderExpirationTime
348
378
return renderExpirationTime ;
349
379
}
350
380
351
381
let expirationTime ;
352
382
if ( suspenseConfig !== null ) {
353
383
// Compute an expiration time based on the Suspense timeout.
384
+ // Suspense组件的特别的expirationTime,后面再研究
354
385
expirationTime = computeSuspenseExpiration (
355
386
currentTime ,
356
387
suspenseConfig . timeoutMs | 0 || LOW_PRIORITY_EXPIRATION ,
@@ -363,11 +394,15 @@ export function computeExpirationForFiber(
363
394
break ;
364
395
case UserBlockingPriority:
365
396
// TODO: Rename this to computeUserBlockingExpiration
397
+ // UserBlockingPriority,走交互过期时间
398
+ // 这个可以理解为是发生用户交互事件的优先级处理
366
399
expirationTime = computeInteractiveExpiration ( currentTime ) ;
367
400
break ;
368
401
case NormalPriority:
369
402
case LowPriority: // TODO: Handle LowPriority
370
403
// TODO: Rename this to... something better.
404
+ // NormaliPriority和LowPriority都是这个异步过期时间
405
+ // 这个值会比交互过期时间的值还小一点
371
406
expirationTime = computeAsyncExpiration ( currentTime ) ;
372
407
break ;
373
408
case IdlePriority:
@@ -1099,7 +1134,7 @@ function flushPendingDiscreteUpdates() {
1099
1134
flushSyncCallbackQueue ( ) ;
1100
1135
}
1101
1136
1102
- export function batchedUpdates < A , R > ( fn : A => R , a : A ) : R {
1137
+ export function batchedUpdates < A , R > ( fn : ( A ) = > R , a : A ) : R {
1103
1138
const prevExecutionContext = executionContext ;
1104
1139
executionContext |= BatchedContext ;
1105
1140
try {
@@ -1113,7 +1148,7 @@ export function batchedUpdates<A, R>(fn: A => R, a: A): R {
1113
1148
}
1114
1149
}
1115
1150
1116
- export function batchedEventUpdates < A , R > ( fn : A => R , a : A ) : R {
1151
+ export function batchedEventUpdates < A , R > ( fn : ( A ) = > R , a : A ) : R {
1117
1152
const prevExecutionContext = executionContext ;
1118
1153
executionContext |= EventContext ;
1119
1154
try {
@@ -1163,7 +1198,7 @@ export function unbatchedUpdates<A, R>(fn: (a: A) => R, a: A): R {
1163
1198
}
1164
1199
}
1165
1200
1166
- export function flushSync < A , R > ( fn : A => R , a : A ) : R {
1201
+ export function flushSync < A , R > ( fn : ( A ) = > R , a : A ) : R {
1167
1202
const prevExecutionContext = executionContext ;
1168
1203
if ( ( prevExecutionContext & ( RenderContext | CommitContext ) ) !== NoContext ) {
1169
1204
if ( __DEV__ ) {
@@ -3344,7 +3379,7 @@ function scheduleInteractions(root, expirationTime, interactions) {
3344
3379
const pendingInteractionMap = root . pendingInteractionMap_old ;
3345
3380
const pendingInteractions = pendingInteractionMap . get ( expirationTime ) ;
3346
3381
if ( pendingInteractions != null ) {
3347
- interactions . forEach ( interaction => {
3382
+ interactions . forEach ( ( interaction ) => {
3348
3383
if ( ! pendingInteractions . has ( interaction ) ) {
3349
3384
// Update the pending async work count for previously unscheduled interaction.
3350
3385
interaction . __count ++ ;
@@ -3356,7 +3391,7 @@ function scheduleInteractions(root, expirationTime, interactions) {
3356
3391
pendingInteractionMap . set ( expirationTime , new Set ( interactions ) ) ;
3357
3392
3358
3393
// Update the pending async work count for the current interactions.
3359
- interactions . forEach ( interaction => {
3394
+ interactions . forEach ( ( interaction ) => {
3360
3395
interaction . __count ++ ;
3361
3396
} ) ;
3362
3397
}
@@ -3393,7 +3428,7 @@ function startWorkOnPendingInteractions(root, expirationTime) {
3393
3428
root . pendingInteractionMap_old . forEach (
3394
3429
( scheduledInteractions , scheduledExpirationTime ) => {
3395
3430
if ( scheduledExpirationTime >= expirationTime ) {
3396
- scheduledInteractions . forEach ( interaction =>
3431
+ scheduledInteractions . forEach ( ( interaction ) =>
3397
3432
interactions . add ( interaction ) ,
3398
3433
) ;
3399
3434
}
@@ -3456,7 +3491,7 @@ function finishPendingInteractions(root, committedExpirationTime) {
3456
3491
if ( scheduledExpirationTime > earliestRemainingTimeAfterCommit ) {
3457
3492
pendingInteractionMap . delete ( scheduledExpirationTime ) ;
3458
3493
3459
- scheduledInteractions . forEach ( interaction => {
3494
+ scheduledInteractions . forEach ( ( interaction ) => {
3460
3495
interaction . __count -- ;
3461
3496
3462
3497
if ( subscriber !== null && interaction . __count === 0 ) {
@@ -3652,7 +3687,7 @@ export function act(callback: () => Thenable<mixed>): Thenable<void> {
3652
3687
}
3653
3688
} ) ;
3654
3689
} ,
3655
- err => {
3690
+ ( err ) => {
3656
3691
onDone ( ) ;
3657
3692
reject ( err ) ;
3658
3693
} ,
0 commit comments