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

Redux store: batch resolver startResolution actions #60590

Closed
wants to merge 6 commits into from
Closed
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
35 changes: 32 additions & 3 deletions packages/data/src/redux-store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@ export default function createReduxStore( key, options ) {
selectorName,
resolver,
store,
resolversCache
resolversCache,
registry
);
}

Expand Down Expand Up @@ -570,6 +571,8 @@ function mapResolvers( resolvers ) {
} );
}

const queuesByRegistry = new WeakMap();

/**
* Returns a selector with a matched resolver.
* Resolvers are side effects invoked once per argument set of a given selector call,
Expand All @@ -580,13 +583,15 @@ function mapResolvers( resolvers ) {
* @param {Object} resolver Resolver to call.
* @param {Object} store The redux store to which the resolvers should be mapped.
* @param {Object} resolversCache Resolvers Cache.
* @param {Object} registry
*/
function mapSelectorWithResolver(
selector,
selectorName,
resolver,
store,
resolversCache
resolversCache,
registry
) {
function fulfillSelector( args ) {
const state = store.getState();
Expand All @@ -613,11 +618,19 @@ function mapSelectorWithResolver(

resolversCache.markAsRunning( selectorName, args );

setTimeout( async () => {
let queue = queuesByRegistry.get( registry );

if ( ! queue ) {
queue = [];
queuesByRegistry.set( registry, queue );
}

async function fulfillResolution() {
resolversCache.clear( selectorName, args );
store.dispatch(
metadataActions.startResolution( selectorName, args )
);

try {
const action = resolver.fulfill( ...args );
if ( action ) {
Expand All @@ -631,6 +644,22 @@ function mapSelectorWithResolver(
metadataActions.failResolution( selectorName, args, error )
);
}
}

queue.push( fulfillResolution );

setTimeout( () => {
if ( queue.length ) {
// Many resolvers can be called at once. The point of this is to
// at least batch `startResolution` actions all together.
registry.batch( () => {
for ( const fulfill of queue ) {
fulfill();
}
} );

queue.length = 0;
}
}, 0 );
}

Expand Down
Loading