-
Notifications
You must be signed in to change notification settings - Fork 4.8k
getEntityRecords: batch actions #60591
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -193,7 +193,7 @@ export const getEditedEntityRecord = forwardResolver( 'getEntityRecord' ); | |||||||
| */ | ||||||||
| export const getEntityRecords = | ||||||||
| ( kind, name, query = {} ) => | ||||||||
| async ( { dispatch } ) => { | ||||||||
| async ( { dispatch, registry } ) => { | ||||||||
| const configs = await dispatch( getOrLoadEntitiesConfig( kind, name ) ); | ||||||||
| const entityConfig = configs.find( | ||||||||
| ( config ) => config.name === name && config.kind === kind | ||||||||
|
|
@@ -261,37 +261,41 @@ export const getEntityRecords = | |||||||
| } ); | ||||||||
| } | ||||||||
|
|
||||||||
| dispatch.receiveEntityRecords( | ||||||||
| kind, | ||||||||
| name, | ||||||||
| records, | ||||||||
| query, | ||||||||
| false, | ||||||||
| undefined, | ||||||||
| meta | ||||||||
| ); | ||||||||
| registry.batch( () => { | ||||||||
| dispatch.receiveEntityRecords( | ||||||||
| kind, | ||||||||
| name, | ||||||||
| records, | ||||||||
| query, | ||||||||
| false, | ||||||||
| undefined, | ||||||||
| meta | ||||||||
| ); | ||||||||
|
|
||||||||
| // When requesting all fields, the list of results can be used to | ||||||||
| // resolve the `getEntityRecord` selector in addition to `getEntityRecords`. | ||||||||
| // See https://github.com/WordPress/gutenberg/pull/26575 | ||||||||
| 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 ] ] ); | ||||||||
| // When requesting all fields, the list of results can be used to | ||||||||
| // resolve the `getEntityRecord` selector in addition to `getEntityRecords`. | ||||||||
| // See https://github.com/WordPress/gutenberg/pull/26575 | ||||||||
| 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 ] ] ); | ||||||||
|
|
||||||||
| dispatch( { | ||||||||
| type: 'START_RESOLUTIONS', | ||||||||
| selectorName: 'getEntityRecord', | ||||||||
| args: resolutionsArgs, | ||||||||
| } ); | ||||||||
| dispatch( { | ||||||||
| type: 'FINISH_RESOLUTIONS', | ||||||||
| selectorName: 'getEntityRecord', | ||||||||
| args: resolutionsArgs, | ||||||||
| } ); | ||||||||
| } | ||||||||
|
|
||||||||
| dispatch( { | ||||||||
| type: 'START_RESOLUTIONS', | ||||||||
| selectorName: 'getEntityRecord', | ||||||||
| args: resolutionsArgs, | ||||||||
| } ); | ||||||||
| dispatch( { | ||||||||
| type: 'FINISH_RESOLUTIONS', | ||||||||
| selectorName: 'getEntityRecord', | ||||||||
| args: resolutionsArgs, | ||||||||
| } ); | ||||||||
| } | ||||||||
| } finally { | ||||||||
| dispatch.__unstableReleaseStoreLock( lock ); | ||||||||
| } ); | ||||||||
| } catch ( e ) { | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should switch this back to But that means we won't be able to batch gutenberg/packages/data/src/redux-store/index.js Lines 533 to 535 in 0d1943c
|
||||||||
| dispatch.__unstableReleaseStoreLock( lock ); | ||||||||
| } | ||||||||
| }; | ||||||||
|
|
||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the start and finish actions are batched, then there's no point dispatching
START_RESOLUTIONS. The state will be overwritten byFINISH_RESOLUTIONS, and no listener will see the state between start and finish because listeners are not called during a batch.If some listener is observing the resolution state and expects to see the full cycle,
undefined->resolving->finished, it might get suprised that it won't ever seeresolving. It will go directly tofinished. Not sure how much breaking this is.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it probably doesn't really make sense to call "start" in this case, we just want to make these as resolved to avoid requests.