Skip to content

Commit

Permalink
Editor: Make sure resetEditorBlocks is synchronous. (#21839)
Browse files Browse the repository at this point in the history
* Data Controls: Add a synchronous `select` control.

* Editor: Use `syncSelect` in `resetEditorBlocks`.

* Data Controls: Make `syncSelect` "__unstable".
  • Loading branch information
epiqueras authored Apr 23, 2020
1 parent aa44b76 commit 8eb5a47
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
37 changes: 37 additions & 0 deletions packages/data-controls/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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 );
Expand Down
9 changes: 7 additions & 2 deletions packages/editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 8eb5a47

Please sign in to comment.