Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport: Try/patterns receive while resolving to 6.7.2 #67244

Merged
merged 1 commit into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
} );
57 changes: 50 additions & 7 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 @@ -245,6 +246,14 @@ export const getEntityRecords =
{ exclusive: false }
);

const key = entityConfig.key || DEFAULT_ENTITY_KEY;

function getResolutionsArgs( records ) {
return records
.filter( ( record ) => record?.[ key ] )
.map( ( record ) => [ kind, name, record[ key ] ] );
}

try {
if ( query._fields ) {
// If requesting specific fields, items and query association to said
Expand All @@ -267,7 +276,8 @@ export const getEntityRecords =
...query,
} );

let records, meta;
let records = [],
meta;
if ( entityConfig.supportsPagination && query.per_page !== -1 ) {
const response = await apiFetch( { path, parse: false } );
records = Object.values( await response.json() );
Expand All @@ -279,6 +289,44 @@ export const getEntityRecords =
response.headers.get( 'X-WP-TotalPages' )
),
};
} else if (
query.per_page === -1 &&
query[ RECEIVE_INTERMEDIATE_RESULTS ] === true
) {
let page = 1;
let totalPages;

do {
const response = await apiFetch( {
path: addQueryArgs( path, { page, per_page: 100 } ),
parse: false,
} );
const pageRecords = Object.values( await response.json() );

totalPages = parseInt(
response.headers.get( 'X-WP-TotalPages' )
);

records.push( ...pageRecords );
registry.batch( () => {
dispatch.receiveEntityRecords(
kind,
name,
records,
query
);
dispatch.finishResolutions(
'getEntityRecord',
getResolutionsArgs( pageRecords )
);
} );
page++;
} while ( page <= totalPages );

meta = {
totalItems: records.length,
totalPages: 1,
};
} else {
records = Object.values( await apiFetch( { path } ) );
meta = {
Expand Down Expand Up @@ -318,11 +366,6 @@ export const getEntityRecords =
// See https://github.com/WordPress/gutenberg/pull/26575
// See https://github.com/WordPress/gutenberg/pull/64504
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 ] ] );

const targetHints = records
.filter( ( record ) => record?.[ key ] )
.map( ( record ) => ( {
Expand Down Expand Up @@ -356,7 +399,7 @@ export const getEntityRecords =
);
dispatch.finishResolutions(
'getEntityRecord',
resolutionsArgs
getResolutionsArgs( records )
);
dispatch.finishResolutions(
'canUser',
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';
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 @@ -29,17 +30,12 @@ import { useGlobalStylesContext } from '../global-styles-provider';
const EMPTY_OBJECT = {};

function __experimentalReusableBlocksSelect( select ) {
const { getEntityRecords, hasFinishedResolution } = select( coreStore );
const reusableBlocks = getEntityRecords( 'postType', 'wp_block', {
const { RECEIVE_INTERMEDIATE_RESULTS } = unlock( coreDataPrivateApis );
const { getEntityRecords } = select( coreStore );
return getEntityRecords( 'postType', 'wp_block', {
per_page: -1,
[ RECEIVE_INTERMEDIATE_RESULTS ]: true,
} );
return hasFinishedResolution( 'getEntityRecords', [
'postType',
'wp_block',
{ per_page: -1 },
] )
? reusableBlocks
: undefined;
}

const BLOCK_EDITOR_SETTINGS = [
Expand Down
Loading