Skip to content

Commit

Permalink
Patterns: receive intermediate responses while unbound request is res…
Browse files Browse the repository at this point in the history
…olving (WordPress#66713)

Co-authored-by: ellatrix <[email protected]>
Co-authored-by: youknowriad <[email protected]>
Co-authored-by: mcsf <[email protected]>
Co-authored-by: sathyapulse <[email protected]>
  • Loading branch information
5 people authored Nov 5, 2024
1 parent ae29450 commit 7f49b39
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 16 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,
} );
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';
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 @@ -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

0 comments on commit 7f49b39

Please sign in to comment.