-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rendering tasks should not jump the queue (#16284)
When React schedules a rendering task, it passes a `timeout` option based on its expiration time. This is intended to avoid starvation by other React updates. However, it also affects the relative priority of React tasks and other Scheduler tasks at the same level, like data processing. This adds a feature flag to disable passing a `timeout` option to Scheduler. React tasks will always append themselves to the end of the queue, without jumping ahead of already scheduled tasks. This does not affect the order in which React updates within a single root are processed, but it could affect updates across multiple roots. This also doesn't remove the expiration from Scheduler. It only means that React tasks are not given special treatment.
- Loading branch information
Showing
9 changed files
with
109 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
...ler/src/__tests__/ReactDisableSchedulerTimeoutBasedOnReactExpirationTime-test.internal.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
let React; | ||
let ReactFeatureFlags; | ||
let ReactNoop; | ||
let Scheduler; | ||
let Suspense; | ||
let scheduleCallback; | ||
let NormalPriority; | ||
|
||
describe('ReactSuspenseList', () => { | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
ReactFeatureFlags = require('shared/ReactFeatureFlags'); | ||
ReactFeatureFlags.debugRenderPhaseSideEffectsForStrictMode = false; | ||
ReactFeatureFlags.replayFailedUnitOfWorkWithInvokeGuardedCallback = false; | ||
ReactFeatureFlags.disableSchedulerTimeoutBasedOnReactExpirationTime = true; | ||
React = require('react'); | ||
ReactNoop = require('react-noop-renderer'); | ||
Scheduler = require('scheduler'); | ||
Suspense = React.Suspense; | ||
|
||
scheduleCallback = Scheduler.unstable_scheduleCallback; | ||
NormalPriority = Scheduler.unstable_NormalPriority; | ||
}); | ||
|
||
function Text(props) { | ||
Scheduler.unstable_yieldValue(props.text); | ||
return props.text; | ||
} | ||
|
||
function createAsyncText(text) { | ||
let resolved = false; | ||
let Component = function() { | ||
if (!resolved) { | ||
Scheduler.unstable_yieldValue('Suspend! [' + text + ']'); | ||
throw promise; | ||
} | ||
return <Text text={text} />; | ||
}; | ||
let promise = new Promise(resolve => { | ||
Component.resolve = function() { | ||
resolved = true; | ||
return resolve(); | ||
}; | ||
}); | ||
return Component; | ||
} | ||
|
||
it('appends rendering tasks to the end of the priority queue', async () => { | ||
const A = createAsyncText('A'); | ||
const B = createAsyncText('B'); | ||
|
||
function App({show}) { | ||
return ( | ||
<Suspense fallback={<Text text="Loading..." />}> | ||
{show ? <A /> : null} | ||
{show ? <B /> : null} | ||
</Suspense> | ||
); | ||
} | ||
|
||
const root = ReactNoop.createRoot(null); | ||
|
||
root.render(<App show={false} />); | ||
expect(Scheduler).toFlushAndYield([]); | ||
|
||
root.render(<App show={true} />); | ||
expect(Scheduler).toFlushAndYield([ | ||
'Suspend! [A]', | ||
'Suspend! [B]', | ||
'Loading...', | ||
]); | ||
expect(root).toMatchRenderedOutput(null); | ||
|
||
Scheduler.unstable_advanceTime(2000); | ||
expect(root).toMatchRenderedOutput(null); | ||
|
||
scheduleCallback(NormalPriority, () => { | ||
Scheduler.unstable_yieldValue('Resolve A'); | ||
A.resolve(); | ||
}); | ||
scheduleCallback(NormalPriority, () => { | ||
Scheduler.unstable_yieldValue('Resolve B'); | ||
B.resolve(); | ||
}); | ||
|
||
// This resolves A and schedules a task for React to retry. | ||
await expect(Scheduler).toFlushAndYieldThrough(['Resolve A']); | ||
|
||
// The next task that flushes should be the one that resolves B. The render | ||
// task should not jump the queue ahead of B. | ||
await expect(Scheduler).toFlushAndYieldThrough(['Resolve B']); | ||
|
||
expect(Scheduler).toFlushAndYield(['A', 'B']); | ||
expect(root).toMatchRenderedOutput('AB'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters