Skip to content

Commit

Permalink
Data: deprecate the getIsResolving selector
Browse files Browse the repository at this point in the history
  • Loading branch information
jsnajdr committed Mar 18, 2024
1 parent 2bcd06b commit e55182b
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 33 deletions.
3 changes: 3 additions & 0 deletions packages/core-data/src/hooks/test/use-entity-record.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ describe( 'useEntityRecord', () => {
record: undefined,
save: expect.any( Function ),
hasResolved: false,
hasStarted: false,
isResolving: false,
status: 'IDLE',
} );
Expand All @@ -70,6 +71,7 @@ describe( 'useEntityRecord', () => {
record: { hello: 'world', id: 1 },
save: expect.any( Function ),
hasResolved: true,
hasStarted: true,
isResolving: false,
status: 'SUCCESS',
} );
Expand Down Expand Up @@ -99,6 +101,7 @@ describe( 'useEntityRecord', () => {
record: { hello: 'world', id: 1 },
save: expect.any( Function ),
hasResolved: true,
hasStarted: true,
isResolving: false,
status: 'SUCCESS',
} )
Expand Down
2 changes: 2 additions & 0 deletions packages/core-data/src/hooks/test/use-entity-records.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ describe( 'useEntityRecords', () => {
expect( data ).toEqual( {
records: null,
hasResolved: false,
hasStarted: false,
isResolving: false,
status: 'IDLE',
totalItems: null,
Expand All @@ -65,6 +66,7 @@ describe( 'useEntityRecords', () => {
expect( data ).toEqual( {
records: TEST_RECORDS,
hasResolved: true,
hasStarted: true,
isResolving: false,
status: 'SUCCESS',
totalItems: null,
Expand Down
3 changes: 3 additions & 0 deletions packages/core-data/src/hooks/test/use-query-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ describe( 'useQuerySelect', () => {
data: 'bar',
isResolving: false,
hasResolved: false,
hasStarted: false,
status: 'IDLE',
} );
} );
Expand Down Expand Up @@ -165,6 +166,7 @@ describe( 'useQuerySelect', () => {
data: 10,
isResolving: false,
hasResolved: false,
hasStarted: false,
status: 'IDLE',
} );

Expand All @@ -180,6 +182,7 @@ describe( 'useQuerySelect', () => {
data: 15,
isResolving: false,
hasResolved: true,
hasStarted: true,
status: 'SUCCESS',
} )
);
Expand Down
37 changes: 21 additions & 16 deletions packages/core-data/src/hooks/use-query-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ interface QuerySelectResponse< Data > {
/** the requested selector return value */
data: Data;

/** is the record still being resolved? Via the `getIsResolving` meta-selector */
/** is the record still being resolved? Via the `isResolving` meta-selector */
isResolving: boolean;

/** was the resolution started? Via the `hasStartedResolution` meta-selector */
Expand Down Expand Up @@ -108,31 +108,36 @@ const enrichSelectors = memoize( ( ( selectors ) => {
get:
() =>
( ...args: unknown[] ) => {
const { getIsResolving, hasFinishedResolution } = selectors;
const isResolving = !! getIsResolving( selectorName, args );
const hasResolved =
! isResolving &&
hasFinishedResolution( selectorName, args );
const data = selectors[ selectorName ]( ...args );
const resolutionStatus = selectors.getResolutionState(
selectorName,
args
)?.status;

let status;
if ( isResolving ) {
status = Status.Resolving;
} else if ( hasResolved ) {
if ( data ) {
switch ( resolutionStatus ) {
case 'resolving':
status = Status.Resolving;
break;
case 'finished':
status = Status.Success;
} else {
break;
case 'error':
status = Status.Error;
}
} else {
status = Status.Idle;
break;
case undefined:
status = Status.Idle;
break;
}

return {
data,
status,
isResolving,
hasResolved,
isResolving: status === Status.Resolving,
hasStarted: status !== Status.Idle,
hasResolved:
status === Status.Success ||
status === Status.Error,
};
},
} );
Expand Down
2 changes: 2 additions & 0 deletions packages/data/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Deprecate the `getIsResolved` meta-selector ([#59679](https://github.com/WordPress/gutenberg/pull/59679)).

## 9.23.0 (2024-03-06)

## 9.22.0 (2024-02-21)
Expand Down
26 changes: 21 additions & 5 deletions packages/data/src/redux-store/metadata/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
*/
import createSelector from 'rememo';

/**
* WordPress dependencies
*/
import deprecated from '@wordpress/deprecated';

/**
* Internal dependencies
*/
Expand Down Expand Up @@ -34,10 +39,16 @@ export function getResolutionState( state, selectorName, args ) {
}

/**
* Returns the raw `isResolving` value for a given selector name,
* and arguments set. May be undefined if the selector has never been resolved
* or not resolved for the given set of arguments, otherwise true or false for
* resolution started and completed respectively.
* Returns an `isResolving`-like value for a given selector name and arguments set.
* Its value is either `undefined` if the selector has never been resolved or has been
* invalidated, or a `true`/`false` boolean value if the resolution is in progress or
* has finished, respectively.
*
* This is a legacy selector that was implemented when the "raw" internal data had
* this `undefined | boolean` format. Nowadays the internal value is an object that
* can be retrieved with `getResolutionState`.
*
* @deprecated
*
* @param {State} state Data state.
* @param {string} selectorName Selector name.
Expand All @@ -46,8 +57,13 @@ export function getResolutionState( state, selectorName, args ) {
* @return {boolean | undefined} isResolving value.
*/
export function getIsResolving( state, selectorName, args ) {
const resolutionState = getResolutionState( state, selectorName, args );
deprecated( 'wp.data.select( store ).getIsResolving', {
since: '6.6',
version: '6.8',
alternative: 'wp.data.select( store ).getResolutionState',
} );

const resolutionState = getResolutionState( state, selectorName, args );
return resolutionState && resolutionState.status === 'resolving';
}

Expand Down
4 changes: 4 additions & 0 deletions packages/data/src/redux-store/metadata/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@ describe( 'getIsResolving', () => {
registry.registerStore( 'testStore', testStore );
} );

const DEPRECATION_MESSAGE =
'wp.data.select( store ).getIsResolving is deprecated since version 6.6 and will be removed in version 6.8. Please use wp.data.select( store ).getResolutionState instead.';

it( 'should return undefined if no state by reducerKey, selectorName', () => {
const result = registry
.select( 'testStore' )
.getIsResolving( 'getFoo', [] );

expect( result ).toBe( undefined );
expect( console ).toHaveWarnedWith( DEPRECATION_MESSAGE );
} );

it( 'should return undefined if state by reducerKey, selectorName, but not args', () => {
Expand Down
27 changes: 17 additions & 10 deletions packages/edit-site/src/components/page-patterns/use-patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ const templatePartToPattern = ( templatePart ) => ( {

const selectTemplatePartsAsPatterns = createSelector(
( select, categoryId, search = '' ) => {
const { getEntityRecords, getIsResolving } = select( coreStore );
const { getEntityRecords, isResolving: isResolvingSelector } =
select( coreStore );
const { __experimentalGetDefaultTemplatePartAreas } =
select( editorStore );
const query = { per_page: -1 };
Expand Down Expand Up @@ -78,7 +79,7 @@ const selectTemplatePartsAsPatterns = createSelector(
);
};

const isResolving = getIsResolving( 'getEntityRecords', [
const isResolving = isResolvingSelector( 'getEntityRecords', [
'postType',
TEMPLATE_PART_POST_TYPE,
query,
Expand All @@ -99,7 +100,7 @@ const selectTemplatePartsAsPatterns = createSelector(
per_page: -1,
}
),
select( coreStore ).getIsResolving( 'getEntityRecords', [
select( coreStore ).isResolving( 'getEntityRecords', [
'postType',
TEMPLATE_PART_POST_TYPE,
{ per_page: -1 },
Expand All @@ -111,7 +112,7 @@ const selectTemplatePartsAsPatterns = createSelector(
const selectThemePatterns = createSelector(
( select ) => {
const { getSettings } = unlock( select( editSiteStore ) );
const { getIsResolving } = select( coreStore );
const { isResolving: isResolvingSelector } = select( coreStore );
const settings = getSettings();
const blockPatterns =
settings.__experimentalAdditionalBlockPatterns ??
Expand All @@ -137,11 +138,14 @@ const selectThemePatterns = createSelector(
__unstableSkipMigrationLogs: true,
} ),
} ) );
return { patterns, isResolving: getIsResolving( 'getBlockPatterns' ) };
return {
patterns,
isResolving: isResolvingSelector( 'getBlockPatterns' ),
};
},
( select ) => [
select( coreStore ).getBlockPatterns(),
select( coreStore ).getIsResolving( 'getBlockPatterns' ),
select( coreStore ).isResolving( 'getBlockPatterns' ),
unlock( select( editSiteStore ) ).getSettings(),
]
);
Expand Down Expand Up @@ -228,8 +232,11 @@ const convertPatternPostToItem = ( patternPost, categories ) => ( {

const selectUserPatterns = createSelector(
( select, syncStatus, search = '' ) => {
const { getEntityRecords, getIsResolving, getUserPatternCategories } =
select( coreStore );
const {
getEntityRecords,
isResolving: isResolvingSelector,
getUserPatternCategories,
} = select( coreStore );

const query = { per_page: -1 };
const patternPosts = getEntityRecords(
Expand All @@ -248,7 +255,7 @@ const selectUserPatterns = createSelector(
)
: EMPTY_PATTERN_LIST;

const isResolving = getIsResolving( 'getEntityRecords', [
const isResolving = isResolvingSelector( 'getEntityRecords', [
'postType',
PATTERN_TYPES.user,
query,
Expand Down Expand Up @@ -277,7 +284,7 @@ const selectUserPatterns = createSelector(
select( coreStore ).getEntityRecords( 'postType', PATTERN_TYPES.user, {
per_page: -1,
} ),
select( coreStore ).getIsResolving( 'getEntityRecords', [
select( coreStore ).isResolving( 'getEntityRecords', [
'postType',
PATTERN_TYPES.user,
{ per_page: -1 },
Expand Down
5 changes: 3 additions & 2 deletions packages/editor/src/components/document-bar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ export default function DocumentBar() {
getEditorSettings,
__experimentalGetTemplateInfo: getTemplateInfo,
} = select( editorStore );
const { getEditedEntityRecord, getIsResolving } = select( coreStore );
const { getEditedEntityRecord, isResolving: isResolvingSelector } =
select( coreStore );
const _postType = getCurrentPostType();
const _postId = getCurrentPostId();
const _document = getEditedEntityRecord(
Expand All @@ -86,7 +87,7 @@ export default function DocumentBar() {
return {
postType: _postType,
document: _document,
isResolving: getIsResolving(
isResolving: isResolvingSelector(
'getEditedEntityRecord',
'postType',
_postType,
Expand Down

0 comments on commit e55182b

Please sign in to comment.