-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Async scheduling of batched updates #34295
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
60c80d1
Await the next animation frame between core data API calls in __exper…
adamziel d9b7bb9
Lint
adamziel 5fb1765
Fix unit tests
adamziel 24eadcf
Lint
adamziel 9e7e872
Add a comment stating that this, ideally, is only a temporary solutio…
adamziel 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
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 |
---|---|---|
|
@@ -643,7 +643,24 @@ export function* __experimentalBatch( requests ) { | |
); | ||
}, | ||
}; | ||
const resultPromises = requests.map( ( request ) => request( api ) ); | ||
const resultPromises = []; | ||
const awaitNextFrame = () => | ||
__unstableAwaitPromise( | ||
new Promise( ( resolve ) => | ||
window.requestAnimationFrame( () => resolve() ) | ||
) | ||
); | ||
for ( const request of requests ) { | ||
resultPromises.push( request( api ) ); | ||
|
||
// Each request( api ) is pretty fast, but when there's a lot of them it may block the browser for a few | ||
// seconds. Let's split this long, blocking task into bite-sized pieces scheduled separately to give the | ||
// browser a space for processing other tasks. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you clarify more what request calls are doing here? Can you share examples? I have trouble understanding why this would be blocking the UI. |
||
// | ||
// Ideally this will be just a temporary fix and the blocking would not be an issue in the first place. | ||
// Replacing redux-rungen usage with async thunks may help with that, see https://github.com/WordPress/gutenberg/pull/27276 | ||
yield awaitNextFrame(); | ||
} | ||
const [ , ...results ] = yield __unstableAwaitPromise( | ||
Promise.all( [ batch.run(), ...resultPromises ] ) | ||
); | ||
|
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
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.
requestAnimationFrame
is not the best function to call here. It always waits 16ms for the next frame, even if the browser is not busy with anything else. Dispatching 60 parallel requests will be spread over exactly one second.setTimeout
orsetImmediate
would be better.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.
Also this could only happen after the first
saveEntityRecord
which would already trigger the progress indicator.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.
setTimeout
might not be better either, some browsers will only fire the callback after a certain amount of time (mostly 1 second) if the tab is in the background.setImmediate
is non-standard, so probably not the best either. I wonder if usingPromise.resolve()
to schedule a microtask work here?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.
Promise.resolve()
wouldn't prevent freezing of the UI. It schedules a microtask right after the current script finishes running, and no other event, like user input or an animation frame, can squeeze in between them.setImmediate
, appropriately polyfilled, is probably the best. Just be careful to not choose a polyfill that usesqueueMicrotask
or something equivalent. The popularasync
package has a polyfill that does exactly that, so the danger is real.