Skip to content

Commit

Permalink
Core data: harmonize getRevision selector and resolver function signa…
Browse files Browse the repository at this point in the history
…tures (#56416)

* Remove default value of query {} on getRevision. It messes up selector memoization since the function signatures are different.

* Resolve individual revisions as per `getEntityRecords`. This ensures that any subsequent calls to `getRevision` for revisions that we've already fetched via `getRevisions()` are marked as resolved and therefore do not trigger the resolver (and therefore an API call).

* Copy update.
Now that #55827 has landed we can start supporting individual global styles URLs as well.

* While I'm here... :)
  • Loading branch information
ramonjd authored Nov 23, 2023
1 parent d33755d commit ab865c5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
8 changes: 5 additions & 3 deletions packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,12 @@ export const rootEntitiesConfig = [
kind: 'root',
baseURL: '/wp/v2/global-styles',
baseURLParams: { context: 'edit' },
plural: 'globalStylesVariations', // Should be different than name.
plural: 'globalStylesVariations', // Should be different from name.
getTitle: ( record ) => record?.title?.rendered || record?.title,
getRevisionsUrl: ( parentId ) =>
`/wp/v2/global-styles/${ parentId }/revisions`,
getRevisionsUrl: ( parentId, revisionId ) =>
`/wp/v2/global-styles/${ parentId }/revisions${
revisionId ? '/' + revisionId : ''
}`,
supports: {
revisions: true,
},
Expand Down
2 changes: 1 addition & 1 deletion packages/core-data/src/queried-data/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function getMergedItemIds( itemIds, nextItemIds, page, perPage ) {
for ( let i = 0; i < size; i++ ) {
// Preserve existing item ID except for subset of range of next items.
// We need to check against the possible maximum upper boundary because
// a page could recieve less items than what was previously stored.
// a page could receive fewer than what was previously stored.
const isInNextItemsRange =
i >= nextItemIdsStartIndex && i < nextItemIdsStartIndex + perPage;
mergedItemIds[ i ] = isInNextItemsRange
Expand Down
22 changes: 21 additions & 1 deletion packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,26 @@ export const getRevisions =
false,
meta
);

// When requesting all fields, the list of results can be used to
// resolve the `getRevision` selector in addition to `getRevisions`.
if ( ! query?._fields && ! query.context ) {
const key = entityConfig.key || DEFAULT_ENTITY_KEY;
const resolutionsArgs = records
.filter( ( record ) => record[ key ] )
.map( ( record ) => [ kind, name, recordKey, record[ key ] ] );

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

// Invalidate cache when a new revision is created.
Expand All @@ -823,7 +843,7 @@ getRevisions.shouldInvalidate = ( action, kind, name, recordKey ) =>
* fields, fields must always include the ID.
*/
export const getRevision =
( kind, name, recordKey, revisionKey, query = {} ) =>
( kind, name, recordKey, revisionKey, query ) =>
async ( { dispatch } ) => {
const configs = await dispatch( getOrLoadEntitiesConfig( kind ) );
const entityConfig = configs.find(
Expand Down

1 comment on commit ab865c5

@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 ab865c5.
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/6975077691
📝 Reported issues:

Please sign in to comment.