From a24cac933f9dd35a884be83efd05437b9fd819f4 Mon Sep 17 00:00:00 2001 From: ramon Date: Wed, 22 Nov 2023 17:04:58 +1100 Subject: [PATCH 1/4] Remove default value of query {} on getRevision. It messes up selector memoization since the function signatures are different. --- packages/core-data/src/queried-data/reducer.js | 2 +- packages/core-data/src/resolvers.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/core-data/src/queried-data/reducer.js b/packages/core-data/src/queried-data/reducer.js index 3462f00b685693..1f9bb64c7889d7 100644 --- a/packages/core-data/src/queried-data/reducer.js +++ b/packages/core-data/src/queried-data/reducer.js @@ -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 diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index 8735764a880b8b..2fadff71737b71 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -823,7 +823,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( From 83f3838b038e4cf77a2f2c5113598eb3019383ce Mon Sep 17 00:00:00 2001 From: ramon Date: Wed, 22 Nov 2023 19:04:29 +1100 Subject: [PATCH 2/4] 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). --- packages/core-data/src/resolvers.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index 2fadff71737b71..30956a0681cd9e 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -801,6 +801,26 @@ export const getRevisions = false, meta ); + + // When requesting all fields, the list of results can be used to + // resolve the `getEntityRecord` selector in addition to `getEntityRecords`. + 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. From 9f993928007242088b4a5b7e9f0569e3229939e8 Mon Sep 17 00:00:00 2001 From: ramon Date: Thu, 23 Nov 2023 14:24:10 +1100 Subject: [PATCH 3/4] Copy update. Now that https://github.com/WordPress/gutenberg/pull/55827 has landed we can start supporting individual global styles URLs as well. --- packages/core-data/src/entities.js | 6 ++++-- packages/core-data/src/resolvers.js | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/core-data/src/entities.js b/packages/core-data/src/entities.js index e85673492ef56e..c655b55e326f03 100644 --- a/packages/core-data/src/entities.js +++ b/packages/core-data/src/entities.js @@ -211,8 +211,10 @@ export const rootEntitiesConfig = [ baseURLParams: { context: 'edit' }, plural: 'globalStylesVariations', // Should be different than 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, }, diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index 30956a0681cd9e..6e821183e3116b 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -803,7 +803,7 @@ export const getRevisions = ); // When requesting all fields, the list of results can be used to - // resolve the `getEntityRecord` selector in addition to `getEntityRecords`. + // resolve the `getRevision` selector in addition to `getRevisions`. if ( ! query?._fields && ! query.context ) { const key = entityConfig.key || DEFAULT_ENTITY_KEY; const resolutionsArgs = records From 2bb2a9980c19947567cbd48c924347a56f665b8e Mon Sep 17 00:00:00 2001 From: ramon Date: Thu, 23 Nov 2023 14:24:34 +1100 Subject: [PATCH 4/4] While I'm here... :) --- packages/core-data/src/entities.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core-data/src/entities.js b/packages/core-data/src/entities.js index c655b55e326f03..9f736af1c83784 100644 --- a/packages/core-data/src/entities.js +++ b/packages/core-data/src/entities.js @@ -209,7 +209,7 @@ 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, revisionId ) => `/wp/v2/global-styles/${ parentId }/revisions${