Skip to content
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
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion packages/core-data/src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() )
Copy link
Member

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 or setImmediate would be better.

Copy link
Contributor Author

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.

Copy link
Member

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 using Promise.resolve() to schedule a microtask work here?

Copy link
Member

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 uses queueMicrotask or something equivalent. The popular async package has a polyfill that does exactly that, so the danger is real.

)
);
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.
Copy link
Contributor

Choose a reason for hiding this comment

The 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 ] )
);
Expand Down
7 changes: 6 additions & 1 deletion packages/core-data/src/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,13 @@ describe( '__experimentalBatch', () => {
),
};
const dispatch = () => actions;
// Run generator up to the last `yield awaitNextFrame( )`.
generator.next( dispatch );
generator.next();
generator.next();
const { value: awaitPromiseControl } = generator.next();

// Run generator up to `yield __unstableAwaitPromise( ... )`.
const { value: awaitPromiseControl } = generator.next( dispatch );
expect( actions.saveEntityRecord ).toHaveBeenCalledWith(
'root',
'widget',
Expand Down