-
Notifications
You must be signed in to change notification settings - Fork 47.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix async batching in React.startTransition #29226
Merged
acdlite
merged 1 commit into
facebook:main
from
acdlite:fix-async-batching-starttransition
May 23, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
143 changes: 143 additions & 0 deletions
143
packages/react-dom/src/__tests__/ReactStartTransitionMultipleRenderers-test.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,143 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @emails react-core | ||
*/ | ||
|
||
'use strict'; | ||
|
||
describe('ReactStartTransitionMultipleRenderers', () => { | ||
let act; | ||
let container; | ||
let React; | ||
let ReactDOMClient; | ||
let Scheduler; | ||
let assertLog; | ||
let startTransition; | ||
let useOptimistic; | ||
let textCache; | ||
|
||
beforeEach(() => { | ||
jest.resetModules(); | ||
React = require('react'); | ||
ReactDOMClient = require('react-dom/client'); | ||
Scheduler = require('scheduler'); | ||
act = require('internal-test-utils').act; | ||
assertLog = require('internal-test-utils').assertLog; | ||
startTransition = React.startTransition; | ||
useOptimistic = React.useOptimistic; | ||
container = document.createElement('div'); | ||
document.body.appendChild(container); | ||
|
||
textCache = new Map(); | ||
}); | ||
|
||
function resolveText(text) { | ||
const record = textCache.get(text); | ||
if (record === undefined) { | ||
const newRecord = { | ||
status: 'resolved', | ||
value: text, | ||
}; | ||
textCache.set(text, newRecord); | ||
} else if (record.status === 'pending') { | ||
const thenable = record.value; | ||
record.status = 'resolved'; | ||
record.value = text; | ||
thenable.pings.forEach(t => t(text)); | ||
} | ||
} | ||
|
||
function getText(text) { | ||
const record = textCache.get(text); | ||
if (record === undefined) { | ||
const thenable = { | ||
pings: [], | ||
then(resolve) { | ||
if (newRecord.status === 'pending') { | ||
thenable.pings.push(resolve); | ||
} else { | ||
Promise.resolve().then(() => resolve(newRecord.value)); | ||
} | ||
}, | ||
}; | ||
const newRecord = { | ||
status: 'pending', | ||
value: thenable, | ||
}; | ||
textCache.set(text, newRecord); | ||
return thenable; | ||
} else { | ||
switch (record.status) { | ||
case 'pending': | ||
return record.value; | ||
case 'rejected': | ||
return Promise.reject(record.value); | ||
case 'resolved': | ||
return Promise.resolve(record.value); | ||
} | ||
} | ||
} | ||
|
||
function Text({text}) { | ||
Scheduler.log(text); | ||
return text; | ||
} | ||
|
||
afterEach(() => { | ||
document.body.removeChild(container); | ||
}); | ||
|
||
// This test imports multiple reconcilers. Because of how the renderers are | ||
// built, it only works when running tests using the actual build artifacts, | ||
// not the source files. | ||
// @gate !source | ||
test('React.startTransition works across multiple renderers', async () => { | ||
const ReactNoop = require('react-noop-renderer'); | ||
|
||
const setIsPendings = new Set(); | ||
|
||
function App() { | ||
const [isPending, setIsPending] = useOptimistic(false); | ||
setIsPendings.add(setIsPending); | ||
return <Text text={isPending ? 'Pending' : 'Not pending'} />; | ||
} | ||
|
||
const noopRoot = ReactNoop.createRoot(null); | ||
const domRoot = ReactDOMClient.createRoot(container); | ||
|
||
// Run the same component in two separate renderers. | ||
await act(() => { | ||
noopRoot.render(<App />); | ||
domRoot.render(<App />); | ||
}); | ||
assertLog(['Not pending', 'Not pending']); | ||
expect(container.textContent).toEqual('Not pending'); | ||
expect(noopRoot).toMatchRenderedOutput('Not pending'); | ||
|
||
await act(() => { | ||
startTransition(async () => { | ||
// Wait until after an async gap before setting the optimistic state. | ||
await getText('Wait before setting isPending'); | ||
setIsPendings.forEach(setIsPending => setIsPending(true)); | ||
|
||
// The optimistic state should not be reverted until the | ||
// action completes. | ||
await getText('Wait until end of async action'); | ||
}); | ||
}); | ||
|
||
await act(() => resolveText('Wait before setting isPending')); | ||
assertLog(['Pending', 'Pending']); | ||
expect(container.textContent).toEqual('Pending'); | ||
expect(noopRoot).toMatchRenderedOutput('Pending'); | ||
|
||
await act(() => resolveText('Wait until end of async action')); | ||
assertLog(['Not pending', 'Not pending']); | ||
expect(container.textContent).toEqual('Not pending'); | ||
expect(noopRoot).toMatchRenderedOutput('Not pending'); | ||
}); | ||
}); |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For flushSync and friends we went with a noop default function so we don't have to do this type check on every invocation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is calling a noop function less work than a null check?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sebmarkbage had some concern over the runtime's ability to optimize it but I don't recall the specifics