From 997ef6d0725ba445d163f32efdd01d93c8f3c87f Mon Sep 17 00:00:00 2001 From: Ella Date: Mon, 4 Nov 2024 15:15:11 +0100 Subject: [PATCH] Make it a private option --- packages/core-data/src/private-apis.js | 2 ++ packages/core-data/src/resolvers.js | 32 +++++++++++-------- packages/core-data/src/utils/index.js | 1 + .../src/utils/receive-intermediate-results.js | 3 ++ .../provider/use-block-editor-settings.js | 4 +++ 5 files changed, 28 insertions(+), 14 deletions(-) create mode 100644 packages/core-data/src/utils/receive-intermediate-results.js diff --git a/packages/core-data/src/private-apis.js b/packages/core-data/src/private-apis.js index 443db97957285d..e9fcf36f7e6090 100644 --- a/packages/core-data/src/private-apis.js +++ b/packages/core-data/src/private-apis.js @@ -2,9 +2,11 @@ * Internal dependencies */ import { useEntityRecordsWithPermissions } from './hooks/use-entity-records'; +import { RECEIVE_INTERMEDIATE_RESULTS } from './utils'; import { lock } from './lock-unlock'; export const privateApis = {}; lock( privateApis, { useEntityRecordsWithPermissions, + RECEIVE_INTERMEDIATE_RESULTS, } ); diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index 58b3e1bfe18330..a35403c0493460 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -21,6 +21,7 @@ import { getUserPermissionCacheKey, getUserPermissionsFromAllowHeader, ALLOWED_RESOURCE_ACTIONS, + RECEIVE_INTERMEDIATE_RESULTS, } from './utils'; import { getSyncProvider } from './sync'; import { fetchBlockPatterns } from './fetch'; @@ -275,8 +276,23 @@ export const getEntityRecords = ...query, } ); - let records, meta; - if ( query.per_page === -1 ) { + let records = [], + meta; + if ( entityConfig.supportsPagination && query.per_page !== -1 ) { + const response = await apiFetch( { path, parse: false } ); + records = Object.values( await response.json() ); + meta = { + totalItems: parseInt( + response.headers.get( 'X-WP-Total' ) + ), + totalPages: parseInt( + response.headers.get( 'X-WP-TotalPages' ) + ), + }; + } else if ( + query.per_page === -1 && + query[ RECEIVE_INTERMEDIATE_RESULTS ] === true + ) { let page = 1; let totalPages; @@ -304,7 +320,6 @@ export const getEntityRecords = getResolutionsArgs( pageRecords ) ); } ); - page++; } while ( page <= totalPages ); @@ -312,17 +327,6 @@ export const getEntityRecords = totalItems: records.length, totalPages: 1, }; - } else if ( entityConfig.supportsPagination ) { - const response = await apiFetch( { path, parse: false } ); - records = Object.values( await response.json() ); - meta = { - totalItems: parseInt( - response.headers.get( 'X-WP-Total' ) - ), - totalPages: parseInt( - response.headers.get( 'X-WP-TotalPages' ) - ), - }; } else { records = Object.values( await apiFetch( { path } ) ); meta = { diff --git a/packages/core-data/src/utils/index.js b/packages/core-data/src/utils/index.js index 189635647779e5..db10359bda07dd 100644 --- a/packages/core-data/src/utils/index.js +++ b/packages/core-data/src/utils/index.js @@ -14,3 +14,4 @@ export { getUserPermissionsFromAllowHeader, ALLOWED_RESOURCE_ACTIONS, } from './user-permissions'; +export { RECEIVE_INTERMEDIATE_RESULTS } from './receive-intermediate-results'; diff --git a/packages/core-data/src/utils/receive-intermediate-results.js b/packages/core-data/src/utils/receive-intermediate-results.js new file mode 100644 index 00000000000000..53d2295b28b390 --- /dev/null +++ b/packages/core-data/src/utils/receive-intermediate-results.js @@ -0,0 +1,3 @@ +export const RECEIVE_INTERMEDIATE_RESULTS = Symbol( + 'RECEIVE_INTERMEDIATE_RESULTS' +); diff --git a/packages/editor/src/components/provider/use-block-editor-settings.js b/packages/editor/src/components/provider/use-block-editor-settings.js index ac6a8a8b9ec910..6c5edd786bc55f 100644 --- a/packages/editor/src/components/provider/use-block-editor-settings.js +++ b/packages/editor/src/components/provider/use-block-editor-settings.js @@ -7,6 +7,7 @@ import { store as coreStore, __experimentalFetchLinkSuggestions as fetchLinkSuggestions, __experimentalFetchUrlData as fetchUrlData, + privateApis as coreDataPrivateApis, } from '@wordpress/core-data'; import { __ } from '@wordpress/i18n'; import { store as preferencesStore } from '@wordpress/preferences'; @@ -28,10 +29,13 @@ import { useGlobalStylesContext } from '../global-styles-provider'; const EMPTY_OBJECT = {}; +const { RECEIVE_INTERMEDIATE_RESULTS } = unlock( coreDataPrivateApis ); + function __experimentalReusableBlocksSelect( select ) { const { getEntityRecords } = select( coreStore ); return getEntityRecords( 'postType', 'wp_block', { per_page: -1, + [ RECEIVE_INTERMEDIATE_RESULTS ]: true, } ); }