Skip to content

Commit ce126fb

Browse files
authored
Fix priority inference of next level of work (#15478)
Bugfix for `inferPriorityFromExpirationTime` function. It happened to work in our existing tests because we use virtual time. Flow would have caught this if expiration times were an opaque type. We should consider that in the future. (The downside of opaque types is that all operations would have to go through helper functions, which may or may not get inlined by Closure.)
1 parent 71c8759 commit ce126fb

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

packages/react-reconciler/src/ReactFiberExpirationTime.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,14 @@ export function inferPriorityFromExpirationTime(
111111
return IdlePriority;
112112
}
113113
const msUntil =
114-
msToExpirationTime(expirationTime) - msToExpirationTime(currentTime);
114+
expirationTimeToMs(expirationTime) - expirationTimeToMs(currentTime);
115115
if (msUntil <= 0) {
116116
return ImmediatePriority;
117117
}
118-
if (msUntil <= HIGH_PRIORITY_EXPIRATION) {
118+
if (msUntil <= HIGH_PRIORITY_EXPIRATION + HIGH_PRIORITY_BATCH_SIZE) {
119119
return UserBlockingPriority;
120120
}
121-
if (msUntil <= LOW_PRIORITY_EXPIRATION) {
121+
if (msUntil <= LOW_PRIORITY_EXPIRATION + LOW_PRIORITY_BATCH_SIZE) {
122122
return NormalPriority;
123123
}
124124

packages/react-reconciler/src/__tests__/ReactSchedulerIntegration-test.internal.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,22 @@ describe('ReactSchedulerIntegration', () => {
150150
]);
151151
});
152152

153+
it('after completing a level of work, infers priority of the next batch based on its expiration time', () => {
154+
function App({label}) {
155+
Scheduler.yieldValue(`${label} [${getCurrentPriorityAsString()}]`);
156+
return label;
157+
}
158+
159+
// Schedule two separate updates at different priorities
160+
runWithPriority(UserBlockingPriority, () => {
161+
ReactNoop.render(<App label="A" />);
162+
});
163+
ReactNoop.render(<App label="B" />);
164+
165+
// The second update should run at normal priority
166+
expect(Scheduler).toFlushAndYield(['A [UserBlocking]', 'B [Normal]']);
167+
});
168+
153169
// TODO
154170
it.skip('passive effects have render priority even if they are flushed early', () => {});
155171
});

0 commit comments

Comments
 (0)