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

Patterns: receive intermediate responses while unbound request is resolving #66713

Merged
merged 4 commits into from
Nov 5, 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually can see this as being useful as a public API. That said, I wonder about the impact on the "resolution state" and things like that.

The other thing I'm wondering, is that basically this means that the "pagination" logic for "-1" has moved within the resolver, so there's a potential to actually remove the code that is responsible for this in apiFetch and just reuse the same code here. The only different being that the action to "fill" the state won't be call until the end if RECEIVE_INTERMEDIATE_RESULTS is false.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could. I also saw that the resolver is anyway already dependent on knowledge of WP Total Pages etc., seen in trunk already 20 lines below. Do you think we should do it here or try separately?

Regarding the resolution state, I guess that's one thing to talk about before making this public. I think it's fine to resolve only after all requests are completed. It does not prevent components from selecting the unresolved state.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we should do it here or try separately?

I'd say separately.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind separate but I feel like we can follow-up on this rather than leave it as nice to improve later.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@youknowriad Well, can we remove it from api-fetch though? We'd break back compat? Not all -1 requests necessarily go through this resolver.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point :) maybe we can deprecate, WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could I guess. I'll look into it tomorrow. In the meantime, I'll merge this PR so the performance problem is addressed.

) {
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