Skip to content

Commit

Permalink
Make it a private option
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Nov 4, 2024
1 parent 30e4a46 commit 997ef6d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 14 deletions.
2 changes: 2 additions & 0 deletions packages/core-data/src/private-apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
} );
32 changes: 18 additions & 14 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
getUserPermissionCacheKey,
getUserPermissionsFromAllowHeader,
ALLOWED_RESOURCE_ACTIONS,
RECEIVE_INTERMEDIATE_RESULTS,
} from './utils';
import { getSyncProvider } from './sync';
import { fetchBlockPatterns } from './fetch';
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -304,25 +320,13 @@ export const getEntityRecords =
getResolutionsArgs( pageRecords )
);
} );

page++;
} while ( page <= totalPages );

meta = {
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 = {
Expand Down
1 change: 1 addition & 0 deletions packages/core-data/src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ export {
getUserPermissionsFromAllowHeader,
ALLOWED_RESOURCE_ACTIONS,
} from './user-permissions';
export { RECEIVE_INTERMEDIATE_RESULTS } from './receive-intermediate-results';
3 changes: 3 additions & 0 deletions packages/core-data/src/utils/receive-intermediate-results.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const RECEIVE_INTERMEDIATE_RESULTS = Symbol(
'RECEIVE_INTERMEDIATE_RESULTS'
);
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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,
} );
}

Expand Down

0 comments on commit 997ef6d

Please sign in to comment.