跳到主要内容

React DOM update priority

一、作用

二、设置当前更新优先级

备注
export function setCurrentUpdatePriority(
newPriority: EventPriority,
// Closure will consistently not inline this function when it has arity 1
// however when it has arity 2 even if the second arg is omitted at every
// callsite it seems to inline it even when the internal length of the function
// is much longer. I hope this is consistent enough to rely on across builds
// 当函数的参数数为 1 时,Closure 一定不会内联这个函数。但是当参数数为 2 时,即使在
// 每个调用点都省略第二个参数,它似乎仍然会内联这个函数。即使函数的内部长度要长得多。
// 我希望这种行为在各种构建中足够一致,可以依赖
IntentionallyUnusedArgument?: empty,
): void {
ReactDOMSharedInternals.p /* currentUpdatePriority */ = newPriority;
}

三、获取当前更新优先级

备注
export function getCurrentUpdatePriority(): EventPriority {
return ReactDOMSharedInternals.p; /* currentUpdatePriority */
}

四、解决更新优先级

备注
export function resolveUpdatePriority(): EventPriority {
const updatePriority = ReactDOMSharedInternals.p; /* currentUpdatePriority */
if (updatePriority !== NoEventPriority) {
return updatePriority;
}
const currentEvent = window.event;
if (currentEvent === undefined) {
return DefaultEventPriority;
}
return getEventPriority(currentEvent.type);
}

五、以优先级运行

export function runWithPriority<T>(priority: EventPriority, fn: () => T): T {
const previousPriority = getCurrentUpdatePriority();
try {
setCurrentUpdatePriority(priority);
return fn();
} finally {
setCurrentUpdatePriority(previousPriority);
}
}