@@ -308,17 +308,34 @@ export function getWorkInProgressRoot(): FiberRoot | null {
308308 return workInProgressRoot ;
309309}
310310
311+ /**
312+ * 该函数只是拿到了一个react定义的currentTime,即大概相对于MAGIC_NUMBER_OFFSET过了多久了
313+ * 具体计算逻辑如下:
314+ * 初始化(包括重置了currentEventTime后)、Render、Commit,return msToExpirationTime(now());
315+ * 其他时候返回上次计算的currentEventTime
316+ */
311317export function requestCurrentTimeForUpdate ( ) {
318+ // executionContext最开始的值为0b000000
319+ // RenderContext = 0b010000
320+ // CommitContext = 0b100000
321+ // 下面这个判断可以理解为通过executionContext控制当前是Render还是Commit阶段
322+ // 若是其中之一个阶段,就直接return msToExpirationTime
323+ // ReactDOM.render可以理解为初始化,并不属于Render或Commit阶段
312324 if ( ( executionContext & ( RenderContext | CommitContext ) ) !== NoContext ) {
313325 // We're inside React, so it's fine to read the actual time.
314326 return msToExpirationTime ( now ( ) ) ;
315327 }
316328 // 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
317332 if ( currentEventTime !== NoWork ) {
318333 // Use the same start time for all updates until we enter React again.
319334 return currentEventTime ;
320335 }
321336 // This is the first update since React yielded. Compute a new start time.
337+ // 既不是Render也不是Commit阶段,
338+ // 初始化或调用performConcurrentWorkOnRoot重置了currentEventTime,同第一个判断的return
322339 currentEventTime = msToExpirationTime ( now ( ) ) ;
323340 return currentEventTime ;
324341}
@@ -327,30 +344,44 @@ export function getCurrentTime() {
327344 return msToExpirationTime ( now ( ) ) ;
328345}
329346
347+ /**
348+ * 该函数用于计算过期时间的,该值越大,优先级越高,返回值有如下几种数字类型
349+ * Sync: 最大,优先级最高
350+ * Batched: Sync-1,优先级第二
351+ * renderExpirationTime: 要么为0,要么是之前已经用该函数计算出来的expirationTime
352+ * computeInteractiveExpiration: 比renderExpirationTime小一点
353+ * computeAsyncExpiration: 比computeInteractiveExpiration小一点
354+ */
330355export function computeExpirationForFiber (
331356 currentTime : ExpirationTime ,
332357 fiber : Fiber ,
333358 suspenseConfig : null | SuspenseConfig ,
334359) : ExpirationTime {
335360 const mode = fiber . mode ;
336361 if ( ( mode & BlockingMode ) === NoMode ) {
337- return Sync ;
362+ // BlockingMode=0010,那么只要mode的倒数第二位为0,就认为属于BlockingMode类别
363+ return Sync ; // Sync,同步,数值最大,优先级最高
338364 }
339365
366+ // 获取当前调度的优先级,调度优先级分类见Scheduler.js
340367 const priorityLevel = getCurrentPriorityLevel ( ) ;
341368 if ( ( mode & ConcurrentMode ) === NoMode ) {
369+ // ConcurrentMode=0100,那么只要mode的倒数第三位为0,就认为属于ConcurrentMode类别
370+ // Batched = Sync - 1,优先级第二
342371 return priorityLevel === ImmediatePriority ? Sync : Batched ;
343372 }
344373
345374 if ( ( executionContext & RenderContext ) !== NoContext ) {
346375 // Use whatever time we're already rendering
347376 // TODO: Should there be a way to opt out, like with `runWithPriority`?
377+ // 若是Render阶段,就返回renderExpirationTime
348378 return renderExpirationTime ;
349379 }
350380
351381 let expirationTime ;
352382 if ( suspenseConfig !== null ) {
353383 // Compute an expiration time based on the Suspense timeout.
384+ // Suspense组件的特别的expirationTime,后面再研究
354385 expirationTime = computeSuspenseExpiration (
355386 currentTime ,
356387 suspenseConfig . timeoutMs | 0 || LOW_PRIORITY_EXPIRATION ,
@@ -363,11 +394,15 @@ export function computeExpirationForFiber(
363394 break ;
364395 case UserBlockingPriority:
365396 // TODO: Rename this to computeUserBlockingExpiration
397+ // UserBlockingPriority,走交互过期时间
398+ // 这个可以理解为是发生用户交互事件的优先级处理
366399 expirationTime = computeInteractiveExpiration ( currentTime ) ;
367400 break ;
368401 case NormalPriority:
369402 case LowPriority: // TODO: Handle LowPriority
370403 // TODO: Rename this to... something better.
404+ // NormaliPriority和LowPriority都是这个异步过期时间
405+ // 这个值会比交互过期时间的值还小一点
371406 expirationTime = computeAsyncExpiration ( currentTime ) ;
372407 break ;
373408 case IdlePriority:
@@ -1099,7 +1134,7 @@ function flushPendingDiscreteUpdates() {
10991134 flushSyncCallbackQueue ( ) ;
11001135}
11011136
1102- export function batchedUpdates < A , R > ( fn : A => R , a : A ) : R {
1137+ export function batchedUpdates < A , R > ( fn : ( A ) = > R , a : A ) : R {
11031138 const prevExecutionContext = executionContext ;
11041139 executionContext |= BatchedContext ;
11051140 try {
@@ -1113,7 +1148,7 @@ export function batchedUpdates<A, R>(fn: A => R, a: A): R {
11131148 }
11141149}
11151150
1116- export function batchedEventUpdates < A , R > ( fn : A => R , a : A ) : R {
1151+ export function batchedEventUpdates < A , R > ( fn : ( A ) = > R , a : A ) : R {
11171152 const prevExecutionContext = executionContext ;
11181153 executionContext |= EventContext ;
11191154 try {
@@ -1163,7 +1198,7 @@ export function unbatchedUpdates<A, R>(fn: (a: A) => R, a: A): R {
11631198 }
11641199}
11651200
1166- export function flushSync < A , R > ( fn : A => R , a : A ) : R {
1201+ export function flushSync < A , R > ( fn : ( A ) = > R , a : A ) : R {
11671202 const prevExecutionContext = executionContext ;
11681203 if ( ( prevExecutionContext & ( RenderContext | CommitContext ) ) !== NoContext ) {
11691204 if ( __DEV__ ) {
@@ -3344,7 +3379,7 @@ function scheduleInteractions(root, expirationTime, interactions) {
33443379 const pendingInteractionMap = root . pendingInteractionMap_old ;
33453380 const pendingInteractions = pendingInteractionMap . get ( expirationTime ) ;
33463381 if ( pendingInteractions != null ) {
3347- interactions . forEach ( interaction => {
3382+ interactions . forEach ( ( interaction ) => {
33483383 if ( ! pendingInteractions . has ( interaction ) ) {
33493384 // Update the pending async work count for previously unscheduled interaction.
33503385 interaction . __count ++ ;
@@ -3356,7 +3391,7 @@ function scheduleInteractions(root, expirationTime, interactions) {
33563391 pendingInteractionMap . set ( expirationTime , new Set ( interactions ) ) ;
33573392
33583393 // Update the pending async work count for the current interactions.
3359- interactions . forEach ( interaction => {
3394+ interactions . forEach ( ( interaction ) => {
33603395 interaction . __count ++ ;
33613396 } ) ;
33623397 }
@@ -3393,7 +3428,7 @@ function startWorkOnPendingInteractions(root, expirationTime) {
33933428 root . pendingInteractionMap_old . forEach (
33943429 ( scheduledInteractions , scheduledExpirationTime ) => {
33953430 if ( scheduledExpirationTime >= expirationTime ) {
3396- scheduledInteractions . forEach ( interaction =>
3431+ scheduledInteractions . forEach ( ( interaction ) =>
33973432 interactions . add ( interaction ) ,
33983433 ) ;
33993434 }
@@ -3456,7 +3491,7 @@ function finishPendingInteractions(root, committedExpirationTime) {
34563491 if ( scheduledExpirationTime > earliestRemainingTimeAfterCommit ) {
34573492 pendingInteractionMap . delete ( scheduledExpirationTime ) ;
34583493
3459- scheduledInteractions . forEach ( interaction => {
3494+ scheduledInteractions . forEach ( ( interaction ) => {
34603495 interaction . __count -- ;
34613496
34623497 if ( subscriber !== null && interaction . __count === 0 ) {
@@ -3652,7 +3687,7 @@ export function act(callback: () => Thenable<mixed>): Thenable<void> {
36523687 }
36533688 } ) ;
36543689 } ,
3655- err => {
3690+ ( err ) => {
36563691 onDone ( ) ;
36573692 reject ( err ) ;
36583693 } ,
0 commit comments