Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editor: Make sure resetEditorBlocks is synchronous. #21839

Merged
merged 3 commits into from
Apr 23, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions packages/data-controls/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,34 @@ _Returns_

- `Object`: The control descriptor.

<a name="syncSelect" href="#syncSelect">#</a> **syncSelect**

Dispatches a control action for triggering a registry select.

Note: This functions like the `select` control, but does not wait
for resolvers.

_Usage_

```js
import { syncSelect } from '@wordpress/data-controls';

// Action generator using `syncSelect`.
export function* myAction() {
const isEditorSideBarOpened = yield syncSelect( 'core/edit-post', 'isEditorSideBarOpened' );
// Do stuff with the result from the `syncSelect`.
}
```

_Parameters_

- _storeKey_ `string`: The key for the store the selector belongs to.
- _selectorName_ `string`: The name of the selector.
- _args_ `Array`: Arguments for the select.

_Returns_

- `Object`: The control descriptor.


<!-- END TOKEN(Autogenerated API docs) -->
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 { syncSelect } from '@wordpress/data-controls';
*
* // Action generator using `syncSelect`.
* export function* myAction() {
* const isEditorSideBarOpened = yield syncSelect( 'core/edit-post', 'isEditorSideBarOpened' );
* // Do stuff with the result from the `syncSelect`.
* }
* ```
*
* @return {Object} The control descriptor.
*/
export function syncSelect( 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,
syncSelect,
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 syncSelect(
'core',
'getEditedEntityRecord',
'postType',
Expand Down