Skip to content

Commit

Permalink
getEntityRecords: batch actions (#60591)
Browse files Browse the repository at this point in the history
Co-authored-by: ellatrix <[email protected]>
Co-authored-by: youknowriad <[email protected]>
  • Loading branch information
3 people authored Apr 9, 2024
1 parent 5bac103 commit d579009
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 33 deletions.
64 changes: 34 additions & 30 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export const getEditedEntityRecord = forwardResolver( 'getEntityRecord' );
*/
export const getEntityRecords =
( kind, name, query = {} ) =>
async ( { dispatch } ) => {
async ( { dispatch, registry } ) => {
const configs = await dispatch( getOrLoadEntitiesConfig( kind, name ) );
const entityConfig = configs.find(
( config ) => config.name === name && config.kind === kind
Expand Down Expand Up @@ -261,37 +261,41 @@ export const getEntityRecords =
} );
}

dispatch.receiveEntityRecords(
kind,
name,
records,
query,
false,
undefined,
meta
);
registry.batch( () => {
dispatch.receiveEntityRecords(
kind,
name,
records,
query,
false,
undefined,
meta
);

// When requesting all fields, the list of results can be used to
// resolve the `getEntityRecord` selector in addition to `getEntityRecords`.
// See https://github.com/WordPress/gutenberg/pull/26575
if ( ! query?._fields && ! query.context ) {
const key = entityConfig.key || DEFAULT_ENTITY_KEY;
const resolutionsArgs = records
.filter( ( record ) => record[ key ] )
.map( ( record ) => [ kind, name, record[ key ] ] );
// When requesting all fields, the list of results can be used to
// resolve the `getEntityRecord` selector in addition to `getEntityRecords`.
// See https://github.com/WordPress/gutenberg/pull/26575
if ( ! query?._fields && ! query.context ) {
const key = entityConfig.key || DEFAULT_ENTITY_KEY;
const resolutionsArgs = records
.filter( ( record ) => record[ key ] )
.map( ( record ) => [ kind, name, record[ key ] ] );

dispatch( {
type: 'START_RESOLUTIONS',
selectorName: 'getEntityRecord',
args: resolutionsArgs,
} );
dispatch( {
type: 'FINISH_RESOLUTIONS',
selectorName: 'getEntityRecord',
args: resolutionsArgs,
} );
}

dispatch( {
type: 'START_RESOLUTIONS',
selectorName: 'getEntityRecord',
args: resolutionsArgs,
} );
dispatch( {
type: 'FINISH_RESOLUTIONS',
selectorName: 'getEntityRecord',
args: resolutionsArgs,
} );
}
} finally {
dispatch.__unstableReleaseStoreLock( lock );
} );
} catch ( e ) {
dispatch.__unstableReleaseStoreLock( lock );
}
};
Expand Down
7 changes: 4 additions & 3 deletions packages/core-data/src/test/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ describe( 'getEntityRecords', () => {
baseURLParams: { context: 'edit' },
},
];
const registry = { batch: ( callback ) => callback() };

beforeEach( async () => {
triggerFetch.mockReset();
Expand All @@ -160,7 +161,7 @@ describe( 'getEntityRecords', () => {
// Provide response
triggerFetch.mockImplementation( () => POST_TYPES );

await getEntityRecords( 'root', 'postType' )( { dispatch } );
await getEntityRecords( 'root', 'postType' )( { dispatch, registry } );

// Fetch request should have been issued.
expect( triggerFetch ).toHaveBeenCalledWith( {
Expand Down Expand Up @@ -191,7 +192,7 @@ describe( 'getEntityRecords', () => {
// Provide response
triggerFetch.mockImplementation( () => POST_TYPES );

await getEntityRecords( 'root', 'postType' )( { dispatch } );
await getEntityRecords( 'root', 'postType' )( { dispatch, registry } );

// Fetch request should have been issued.
expect( triggerFetch ).toHaveBeenCalledWith( {
Expand Down Expand Up @@ -221,7 +222,7 @@ describe( 'getEntityRecords', () => {
// Provide response
triggerFetch.mockImplementation( () => POST_TYPES );

await getEntityRecords( 'root', 'postType' )( { dispatch } );
await getEntityRecords( 'root', 'postType' )( { dispatch, registry } );

// Fetch request should have been issued.
expect( triggerFetch ).toHaveBeenCalledWith( {
Expand Down

1 comment on commit d579009

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in d579009.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/8615433483
📝 Reported issues:

Please sign in to comment.