From e55182bd6d74536627b61670fa1aab0bb30e975b Mon Sep 17 00:00:00 2001 From: Jarda Snajdr Date: Thu, 7 Mar 2024 13:04:37 +0100 Subject: [PATCH] Data: deprecate the getIsResolving selector --- .../src/hooks/test/use-entity-record.js | 3 ++ .../src/hooks/test/use-entity-records.js | 2 + .../src/hooks/test/use-query-select.js | 3 ++ .../core-data/src/hooks/use-query-select.ts | 37 +++++++++++-------- packages/data/CHANGELOG.md | 2 + .../src/redux-store/metadata/selectors.js | 26 ++++++++++--- .../redux-store/metadata/test/selectors.js | 4 ++ .../components/page-patterns/use-patterns.js | 27 +++++++++----- .../src/components/document-bar/index.js | 5 ++- 9 files changed, 76 insertions(+), 33 deletions(-) diff --git a/packages/core-data/src/hooks/test/use-entity-record.js b/packages/core-data/src/hooks/test/use-entity-record.js index 0961445b58c1ff..49964339a4f2b8 100644 --- a/packages/core-data/src/hooks/test/use-entity-record.js +++ b/packages/core-data/src/hooks/test/use-entity-record.js @@ -51,6 +51,7 @@ describe( 'useEntityRecord', () => { record: undefined, save: expect.any( Function ), hasResolved: false, + hasStarted: false, isResolving: false, status: 'IDLE', } ); @@ -70,6 +71,7 @@ describe( 'useEntityRecord', () => { record: { hello: 'world', id: 1 }, save: expect.any( Function ), hasResolved: true, + hasStarted: true, isResolving: false, status: 'SUCCESS', } ); @@ -99,6 +101,7 @@ describe( 'useEntityRecord', () => { record: { hello: 'world', id: 1 }, save: expect.any( Function ), hasResolved: true, + hasStarted: true, isResolving: false, status: 'SUCCESS', } ) diff --git a/packages/core-data/src/hooks/test/use-entity-records.js b/packages/core-data/src/hooks/test/use-entity-records.js index af490b65096290..2547cda890b05e 100644 --- a/packages/core-data/src/hooks/test/use-entity-records.js +++ b/packages/core-data/src/hooks/test/use-entity-records.js @@ -49,6 +49,7 @@ describe( 'useEntityRecords', () => { expect( data ).toEqual( { records: null, hasResolved: false, + hasStarted: false, isResolving: false, status: 'IDLE', totalItems: null, @@ -65,6 +66,7 @@ describe( 'useEntityRecords', () => { expect( data ).toEqual( { records: TEST_RECORDS, hasResolved: true, + hasStarted: true, isResolving: false, status: 'SUCCESS', totalItems: null, diff --git a/packages/core-data/src/hooks/test/use-query-select.js b/packages/core-data/src/hooks/test/use-query-select.js index 2fbc6951a49361..894851f79a9c78 100644 --- a/packages/core-data/src/hooks/test/use-query-select.js +++ b/packages/core-data/src/hooks/test/use-query-select.js @@ -116,6 +116,7 @@ describe( 'useQuerySelect', () => { data: 'bar', isResolving: false, hasResolved: false, + hasStarted: false, status: 'IDLE', } ); } ); @@ -165,6 +166,7 @@ describe( 'useQuerySelect', () => { data: 10, isResolving: false, hasResolved: false, + hasStarted: false, status: 'IDLE', } ); @@ -180,6 +182,7 @@ describe( 'useQuerySelect', () => { data: 15, isResolving: false, hasResolved: true, + hasStarted: true, status: 'SUCCESS', } ) ); diff --git a/packages/core-data/src/hooks/use-query-select.ts b/packages/core-data/src/hooks/use-query-select.ts index 401c2e471dc3b2..abee3323895afd 100644 --- a/packages/core-data/src/hooks/use-query-select.ts +++ b/packages/core-data/src/hooks/use-query-select.ts @@ -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 */ @@ -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, }; }, } ); diff --git a/packages/data/CHANGELOG.md b/packages/data/CHANGELOG.md index 800b6d38b7c4e7..09829dc4cf1e30 100644 --- a/packages/data/CHANGELOG.md +++ b/packages/data/CHANGELOG.md @@ -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) diff --git a/packages/data/src/redux-store/metadata/selectors.js b/packages/data/src/redux-store/metadata/selectors.js index 884ec714efd574..4f1ef4c1ec87a2 100644 --- a/packages/data/src/redux-store/metadata/selectors.js +++ b/packages/data/src/redux-store/metadata/selectors.js @@ -3,6 +3,11 @@ */ import createSelector from 'rememo'; +/** + * WordPress dependencies + */ +import deprecated from '@wordpress/deprecated'; + /** * Internal dependencies */ @@ -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. @@ -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'; } diff --git a/packages/data/src/redux-store/metadata/test/selectors.js b/packages/data/src/redux-store/metadata/test/selectors.js index faf9da3a169853..37eb820889cc15 100644 --- a/packages/data/src/redux-store/metadata/test/selectors.js +++ b/packages/data/src/redux-store/metadata/test/selectors.js @@ -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', () => { diff --git a/packages/edit-site/src/components/page-patterns/use-patterns.js b/packages/edit-site/src/components/page-patterns/use-patterns.js index b099b70b36fe1a..606ac59a02d986 100644 --- a/packages/edit-site/src/components/page-patterns/use-patterns.js +++ b/packages/edit-site/src/components/page-patterns/use-patterns.js @@ -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 }; @@ -78,7 +79,7 @@ const selectTemplatePartsAsPatterns = createSelector( ); }; - const isResolving = getIsResolving( 'getEntityRecords', [ + const isResolving = isResolvingSelector( 'getEntityRecords', [ 'postType', TEMPLATE_PART_POST_TYPE, query, @@ -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 }, @@ -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 ?? @@ -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(), ] ); @@ -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( @@ -248,7 +255,7 @@ const selectUserPatterns = createSelector( ) : EMPTY_PATTERN_LIST; - const isResolving = getIsResolving( 'getEntityRecords', [ + const isResolving = isResolvingSelector( 'getEntityRecords', [ 'postType', PATTERN_TYPES.user, query, @@ -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 }, diff --git a/packages/editor/src/components/document-bar/index.js b/packages/editor/src/components/document-bar/index.js index d0b7126c0d270d..25356f3b2a1fa3 100644 --- a/packages/editor/src/components/document-bar/index.js +++ b/packages/editor/src/components/document-bar/index.js @@ -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( @@ -86,7 +87,7 @@ export default function DocumentBar() { return { postType: _postType, document: _document, - isResolving: getIsResolving( + isResolving: isResolvingSelector( 'getEditedEntityRecord', 'postType', _postType,