function flushPassiveEffects() {
// Returns whether passive effects were flushed.
// TODO: Combine this check with the one in flushPassiveEFfectsImpl. We should
// probably just combine the two functions. I believe they were only separate
// in the first place because we used to wrap it with
// `Scheduler.runWithPriority`, which accepts a function. But now we track the
// priority within React itself, so we can mutate the variable directly.
if (rootWithPendingPassiveEffects !== null) {
// Cache the root since rootWithPendingPassiveEffects is cleared in
// flushPassiveEffectsImpl
var root = rootWithPendingPassiveEffects; // Cache and clear the remaining lanes flag; it must be reset since this
// method can be called from various places, not always from commitRoot
// where the remaining lanes are known
var remainingLanes = pendingPassiveEffectsRemainingLanes;
pendingPassiveEffectsRemainingLanes = NoLanes;
var renderPriority = lanesToEventPriority(pendingPassiveEffectsLanes);
var priority = lowerEventPriority(DefaultEventPriority, renderPriority);
var prevTransition = ReactCurrentBatchConfig$3.transition;
var previousPriority = getCurrentUpdatePriority();
try {
ReactCurrentBatchConfig$3.transition = null;
setCurrentUpdatePriority(priority);
return flushPassiveEffectsImpl();
} finally {
setCurrentUpdatePriority(previousPriority);
ReactCurrentBatchConfig$3.transition = prevTransition; // Once passive effects have run for the tree - giving components a
// chance to retain cache instances they use - release the pooled
// cache at the root (if there is one)
releaseRootPooledCache(root, remainingLanes);
}
}
return false;
}
函数的主要步骤如下:
- 首先检查
rootWithPendingPassiveEffects是否为null,如果不为null,则表示有待处理的被动副作用。 - 缓存
rootWithPendingPassiveEffects,因为在flushPassiveEffectsImpl函数中会清除该变量。 - 缓存并清除
pendingPassiveEffectsRemainingLanes,因为在处理被动副作用时,需要知道剩余的lanes。 - 根据
pendingPassiveEffectsLanes计算出渲染优先级,并将其转换为事件优先级。 - 设置当前更新的优先级为计算出的优先级。
- 调用
flushPassiveEffectsImpl函数来处理被动副作用。 - 在
finally块中,恢复之前的更新优先级和ReactCurrentBatchConfig$3.transition的值。 - 一旦被动副作用在整个树上运行完毕,释放根节点上的缓存池。
- 如果有待处理的被动副作用,则返回
true,否则返回false。