行数
https://github.com/a8397550/react-source-share/blob/master/default/react-dom-default.js
代码行数 21526行 scheduleUpdateOnFiber
执行 21911 行 renderRoot(root, Sync, true); // 返回了一个这个哦 commitRoot.bind(…)
注意 21654 行 scheduleCallbackForRoot
var scheduleWork = scheduleUpdateOnFiber;
function scheduleUpdateOnFiber(fiber, expirationTime) {
checkForNestedUpdates();
warnAboutInvalidUpdatesOnClassComponentsInDEV(fiber);
var root = markUpdateTimeFromFiberToRoot(fiber, expirationTime);
if (root === null) {
warnAboutUpdateOnUnmountedFiberInDEV(fiber);
return;
}
// NoWork 常量 0
root.pingTime = NoWork;
function checkForInterruption(fiberThatReceivedUpdate, updateExpirationTime) {
// 初始化时 变量 workInProgressRoot = null
// 常量 enableUserTimingAPI = true
if (enableUserTimingAPI && workInProgressRoot !== null &&
// updateExpirationTime = 1073741823
// 变量 renderExpirationTime = 0
updateExpirationTime > renderExpirationTime) {
interruptedBy = fiberThatReceivedUpdate;
}
}
checkForInterruption(fiber, expirationTime);
// record 记录 [ˈrekɔːd , rɪˈkɔːd]
// schedule 工作计划 [ˈskedʒuːl]
function recordScheduleUpdate() {
// enableUserTimingAPI 常量 true
if (enableUserTimingAPI) {
// 变量 初始化时 = false
if (isCommitting) {
hasScheduledUpdateInCurrentCommit = true;
}
// 初始化时 currentPhase = null
if (currentPhase !== null &&
currentPhase !== 'componentWillMount'
&& currentPhase !== 'componentWillReceiveProps') {
hasScheduledUpdateInCurrentPhase = true;
}
}
}
recordScheduleUpdate();
// React-DOM render 源码分析二 介绍过这个方法
// 初始化时 priorityLevel = 97
var priorityLevel = getCurrentPriorityLevel();
// 初始化时 expirationTime === 1073741823 === 1073741823 === Sync
// true
if (expirationTime === Sync) {
if (
// 常量 NoContext = 0
// 初始化时 executionContext = 8 LegacyUnbatchedContext = 8
// 8 & 8 = 8 下条语句初始化时结果为true
(executionContext & LegacyUnbatchedContext) !== NoContext &&
// 初始化 (8 & (16 | 32)) === NoContext 结果为true
(executionContext & (RenderContext | CommitContext)) === NoContext) {
// 初始化时,此方法啥也没干
schedulePendingInteractions(root, expirationTime);
var callback = renderRoot(root, Sync, true);
while (callback !== null) {
callback = callback(true);
}
} else {
scheduleCallbackForRoot(root, ImmediatePriority, Sync);
if (executionContext === NoContext) {
flushSyncCallbackQueue();
}
}
} else {
scheduleCallbackForRoot(root, priorityLevel, expirationTime);
}
if ((executionContext & DiscreteEventContext) !== NoContext && (
// Only updates at user-blocking priority or greater are considered
// discrete, even inside a discrete event.
priorityLevel === UserBlockingPriority$2 || priorityLevel === ImmediatePriority)) {
// This is the result of a discrete event. Track the lowest priority
// discrete update per root so we can flush them early, if needed.
if (rootsWithPendingDiscreteUpdates === null) {
rootsWithPendingDiscreteUpdates = new Map([[root, expirationTime]]);
} else {
var lastDiscreteTime = rootsWithPendingDiscreteUpdates.get(root);
if (lastDiscreteTime === undefined || lastDiscreteTime > expirationTime) {
rootsWithPendingDiscreteUpdates.set(root, expirationTime);
}
}
}
}
function checkForNestedUpdates() {
// nestedUpdateCount = 0 , 常量 NESTED_UPDATE_LIMIT = 50
if (nestedUpdateCount > NESTED_UPDATE_LIMIT) {
nestedUpdateCount = 0;
rootWithNestedUpdates = null;
(function () {
{
{
throw ReactError(Error('Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.'));
}
}
})();
}
{
// nestedPassiveUpdateCount = 0, 常量 NESTED_PASSIVE_UPDATE_LIMIT = 50
if (nestedPassiveUpdateCount > NESTED_PASSIVE_UPDATE_LIMIT) {
nestedPassiveUpdateCount = 0;
warning$1(false, 'Maximum update depth exceeded. This can happen when a component ' + "calls setState inside useEffect, but useEffect either doesn't " + 'have a dependency array, or one of the dependencies changes on ' + 'every Render.');
}
}
}
function warnAboutInvalidUpdatesOnClassComponentsInDEV(fiber) {
{
// 初始化时,fiber.tag = 3, ClassComponent 常量 1
if (fiber.tag === ClassComponent) {
switch (phase) {
case 'getChildContext':
if (didWarnAboutUpdateInGetChildContext) {
return;
}
warningWithoutStack$1(false, 'setState(...): Cannot call setState() inside getChildContext()');
didWarnAboutUpdateInGetChildContext = true;
break;
case 'render':
if (didWarnAboutUpdateInRender) {
return;
}
warningWithoutStack$1(false, 'Cannot update during an existing state transition (such as ' + 'within `Render`). Render methods should be a pure function of ' + 'props and state.');
didWarnAboutUpdateInRender = true;
break;
}
}
}
}
function markUpdateTimeFromFiberToRoot(fiber, expirationTime) {
// 初始化时
// expirationTime = 1073741823
// fiber.expirationTime = 0
if (fiber.expirationTime < expirationTime) {
fiber.expirationTime = expirationTime;
}
// 初始化 fiber.alternate = null
var alternate = fiber.alternate;
if (alternate !== null && alternate.expirationTime < expirationTime) {
alternate.expirationTime = expirationTime;
}
// 初始化时 fiber.return = null
var node = fiber.return;
var root = null;
// 常量 3
if (node === null && fiber.tag === HostRoot) {
// fiber.stateNode = FiberRootNode
root = fiber.stateNode;
} else {
while (node !== null) {
alternate = node.alternate;
if (node.childExpirationTime < expirationTime) {
node.childExpirationTime = expirationTime;
if (alternate !== null && alternate.childExpirationTime < expirationTime) {
alternate.childExpirationTime = expirationTime;
}
} else if (alternate !== null && alternate.childExpirationTime < expirationTime) {
alternate.childExpirationTime = expirationTime;
}
if (node.return === null && node.tag === HostRoot) {
root = node.stateNode;
break;
}
node = node.return;
}
}
if (root !== null) {
// 初始化时 root.firstPendingTime = 0;
// 英first 首个 [fɜːst]
var firstPendingTime = root.firstPendingTime;
if (expirationTime > firstPendingTime) {
root.firstPendingTime = expirationTime;
}
// 初始化 root.lastPendingTime = 0;
var lastPendingTime = root.lastPendingTime;
// NoWork = 0
if (lastPendingTime === NoWork || expirationTime < lastPendingTime) {
root.lastPendingTime = expirationTime;
}
}
return root;
}
function schedulePendingInteractions(root, expirationTime) {
// root FiberRootNode
// expirationTime === 1073741823
// 常量 enableSchedulerTracing = true
if (!enableSchedulerTracing) {
return;
}
// var ReactInternals$2 = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
// var _ReactInternals$Sched$1 = ReactInternals$2.SchedulerTracing;
// var __interactionsRef = _ReactInternals$Sched$1.__interactionsRef;
// 加载react-dom时初始化 ,render前就有值了 __interactionsRef.current: Set(0) {}
scheduleInteractions(root, expirationTime, __interactionsRef.current);
}
function scheduleInteractions(root, expirationTime, interactions) {
// 常量 true
if (!enableSchedulerTracing) {
return;
}
if (interactions.size > 0) {
var pendingInteractionMap = root.pendingInteractionMap;
var pendingInteractions = pendingInteractionMap.get(expirationTime);
if (pendingInteractions != null) {
interactions.forEach(function (interaction) {
if (!pendingInteractions.has(interaction)) {
// Update the pending async work count for previously unscheduled interaction.
interaction.__count++;
}
pendingInteractions.add(interaction);
});
} else {
pendingInteractionMap.set(expirationTime, new Set(interactions));
// Update the pending async work count for the current interactions.
interactions.forEach(function (interaction) {
interaction.__count++;
});
}
var subscriber = __subscriberRef.current;
if (subscriber !== null) {
var threadID = computeThreadID(root, expirationTime);
subscriber.onWorkScheduled(interactions, threadID);
}
}
}