functiondispatchAction<S, A>( fiber: Fiber, queue: UpdateQueue<S, A>, action: A, ) { const alternate = fiber.alternate;
// [情况一]: 如果当前fiber处于`mount`阶段, 且在其内部产生了更新 if ( fiber === currentlyRenderingFiber || (alternate !== null && alternate === currentlyRenderingFiber) ) { // This is a render phase update. Stash it in a lazily-created map of // queue -> linked list of updates. After this render pass, we'll restart // and apply the stashed updates on top of the work-in-progress hook.
// 标识是否在`mount`阶段产生更新 // 产生的更新会保存到`renderPhaseUpdates`字典上 // 后续在渲染完当前组件之后, 会根据`didScheduleRenderPhaseUpdate`, 来决定是否处理更新 didScheduleRenderPhaseUpdate = true; const update: Update<S, A> = { expirationTime: renderExpirationTime, action, eagerReducer: null, eagerState: null, next: null, }; if (renderPhaseUpdates === null) { renderPhaseUpdates = newMap(); } const firstRenderPhaseUpdate = renderPhaseUpdates.get(queue); if (firstRenderPhaseUpdate === undefined) { renderPhaseUpdates.set(queue, update); } else { // Append the update to the end of the list. let lastRenderPhaseUpdate = firstRenderPhaseUpdate; while (lastRenderPhaseUpdate.next !== null) { lastRenderPhaseUpdate = lastRenderPhaseUpdate.next; } lastRenderPhaseUpdate.next = update; } } // [情况二]: 如果当前fiber处于`mount`阶段, 但不是它内部产生的更新, 安排一个新的调度工作 else { flushPassiveEffects();
// Append the update to the end of the list. const last = queue.last; if (last === null) { // This is the first update. Create a circular list. update.next = update; } else { const first = last.next; if (first !== null) { // Still circular. update.next = first; } last.next = update; } queue.last = update;