diff --git a/packages/data-controls/src/index.js b/packages/data-controls/src/index.js index c0c1fe21807864..91c97e4ec981d6 100644 --- a/packages/data-controls/src/index.js +++ b/packages/data-controls/src/index.js @@ -63,6 +63,38 @@ export function select( storeKey, selectorName, ...args ) { }; } +/** + * Dispatches a control action for triggering a registry select. + * + * Note: This functions like the `select` control, but does not wait + * for resolvers. + * + * @param {string} storeKey The key for the store the selector belongs to. + * @param {string} selectorName The name of the selector. + * @param {Array} args Arguments for the select. + * + * @example + * ```js + * import { __unstableSyncSelect } from '@wordpress/data-controls'; + * + * // Action generator using `__unstableSyncSelect`. + * export function* myAction() { + * const isEditorSideBarOpened = yield __unstableSyncSelect( 'core/edit-post', 'isEditorSideBarOpened' ); + * // Do stuff with the result from the `__unstableSyncSelect`. + * } + * ``` + * + * @return {Object} The control descriptor. + */ +export function __unstableSyncSelect( storeKey, selectorName, ...args ) { + return { + type: 'SYNC_SELECT', + storeKey, + selectorName, + args, + }; +} + /** * Dispatches a control action for triggering a registry dispatch. * @@ -133,6 +165,11 @@ export const controls = { ]( storeKey )[ selectorName ]( ...args ); } ), + SYNC_SELECT: createRegistryControl( + ( registry ) => ( { storeKey, selectorName, args } ) => { + return registry.select( storeKey )[ selectorName ]( ...args ); + } + ), DISPATCH: createRegistryControl( ( registry ) => ( { storeKey, actionName, args } ) => { return registry.dispatch( storeKey )[ actionName ]( ...args ); diff --git a/packages/editor/src/store/actions.js b/packages/editor/src/store/actions.js index 453a606c86206d..dc785fa4480bfd 100644 --- a/packages/editor/src/store/actions.js +++ b/packages/editor/src/store/actions.js @@ -7,7 +7,12 @@ import { has, castArray } from 'lodash'; * WordPress dependencies */ import deprecated from '@wordpress/deprecated'; -import { dispatch, select, apiFetch } from '@wordpress/data-controls'; +import { + dispatch, + select, + __unstableSyncSelect, + apiFetch, +} from '@wordpress/data-controls'; import { parse, synchronizeBlocksWithTemplate } from '@wordpress/blocks'; /** @@ -672,7 +677,7 @@ export function* resetEditorBlocks( blocks, options = {} ) { if ( __unstableShouldCreateUndoLevel !== false ) { const { id, type } = yield select( STORE_KEY, 'getCurrentPost' ); const noChange = - ( yield select( + ( yield __unstableSyncSelect( 'core', 'getEditedEntityRecord', 'postType',