Skip to content

Commit

Permalink
Add batched variants for start/finish resolution actions
Browse files Browse the repository at this point in the history
  • Loading branch information
fluiddot committed Apr 20, 2021
1 parent 3419065 commit a40911a
Show file tree
Hide file tree
Showing 5 changed files with 262 additions and 100 deletions.
28 changes: 14 additions & 14 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,20 +219,20 @@ export function* getEntityRecords( kind, name, query = {} ) {
// See https://github.com/WordPress/gutenberg/pull/26575
if ( ! query?._fields ) {
const key = entity.key || DEFAULT_ENTITY_KEY;
for ( const record of records ) {
if ( record[ key ] ) {
yield {
type: 'START_RESOLUTION',
selectorName: 'getEntityRecord',
args: [ kind, name, record[ key ] ],
};
yield {
type: 'FINISH_RESOLUTION',
selectorName: 'getEntityRecord',
args: [ kind, name, record[ key ] ],
};
}
}
const resolutionsArgs = records
.filter( ( record ) => record[ key ] )
.map( ( record ) => [ kind, name, record[ key ] ] );

yield {
type: 'START_RESOLUTIONS',
selectorName: 'getEntityRecord',
args: resolutionsArgs,
};
yield {
type: 'FINISH_RESOLUTIONS',
selectorName: 'getEntityRecord',
args: resolutionsArgs,
};
}
} finally {
yield* __unstableReleaseStoreLock( lock );
Expand Down
8 changes: 4 additions & 4 deletions packages/core-data/src/test/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,14 @@ describe( 'getEntityRecords', () => {

// It should mark the entity record that has an ID as resolved
expect( fulfillment.next().value ).toEqual( {
type: 'START_RESOLUTION',
type: 'START_RESOLUTIONS',
selectorName: 'getEntityRecord',
args: [ ENTITIES[ 1 ].kind, ENTITIES[ 1 ].name, 2 ],
args: [ [ ENTITIES[ 1 ].kind, ENTITIES[ 1 ].name, 2 ] ],
} );
expect( fulfillment.next().value ).toEqual( {
type: 'FINISH_RESOLUTION',
type: 'FINISH_RESOLUTIONS',
selectorName: 'getEntityRecord',
args: [ ENTITIES[ 1 ].kind, ENTITIES[ 1 ].name, 2 ],
args: [ [ ENTITIES[ 1 ].kind, ENTITIES[ 1 ].name, 2 ] ],
} );
} );
} );
Expand Down
36 changes: 36 additions & 0 deletions packages/data/src/redux-store/metadata/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,42 @@ export function finishResolution( selectorName, args ) {
};
}

/**
* Returns an action object used in signalling that a batch of selector resolutions has
* started.
*
* @param {string} selectorName Name of selector for which resolver triggered.
* @param {...*} args Array of arguments to associate for uniqueness, each item
* is associated to a resolution.
*
* @return {Object} Action object.
*/
export function startResolutions( selectorName, args ) {
return {
type: 'START_RESOLUTIONS',
selectorName,
args,
};
}

/**
* Returns an action object used in signalling that a batch of selector resolutions has
* completed.
*
* @param {string} selectorName Name of selector for which resolver triggered.
* @param {...*} args Array of arguments to associate for uniqueness, each item
* is associated to a resolution.
*
* @return {Object} Action object.
*/
export function finishResolutions( selectorName, args ) {
return {
type: 'FINISH_RESOLUTIONS',
selectorName,
args,
};
}

/**
* Returns an action object used in signalling that we should invalidate the resolution cache.
*
Expand Down
11 changes: 11 additions & 0 deletions packages/data/src/redux-store/metadata/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ const subKeysIsResolved = onSubKey( 'selectorName' )(
nextState.set( action.args, isStarting );
return nextState;
}
case 'START_RESOLUTIONS':
case 'FINISH_RESOLUTIONS': {
const isStarting = action.type === 'START_RESOLUTIONS';
const nextState = new EquivalentKeyMap( state );
for ( const resolutionArgs of action.args ) {
nextState.set( resolutionArgs, isStarting );
}
return nextState;
}
case 'INVALIDATE_RESOLUTION': {
const nextState = new EquivalentKeyMap( state );
nextState.delete( action.args );
Expand Down Expand Up @@ -60,6 +69,8 @@ const isResolved = ( state = {}, action ) => {
: state;
case 'START_RESOLUTION':
case 'FINISH_RESOLUTION':
case 'START_RESOLUTIONS':
case 'FINISH_RESOLUTIONS':
case 'INVALIDATE_RESOLUTION':
return subKeysIsResolved( state, action );
}
Expand Down
Loading

0 comments on commit a40911a

Please sign in to comment.