From 1ebd7d2619a598be0fd00cff79fd3dabf5ad8b05 Mon Sep 17 00:00:00 2001 From: epiqueras Date: Thu, 23 Apr 2020 12:59:47 -0700 Subject: [PATCH 1/3] Data Controls: Add a synchronous `select` control. --- packages/data-controls/README.md | 29 ++++++++++++++++++++++ packages/data-controls/src/index.js | 37 +++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/packages/data-controls/README.md b/packages/data-controls/README.md index a85d9c4ac101aa..c82fdf10f0fb42 100644 --- a/packages/data-controls/README.md +++ b/packages/data-controls/README.md @@ -132,5 +132,34 @@ _Returns_ - `Object`: The control descriptor. +# **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. + diff --git a/packages/data-controls/src/index.js b/packages/data-controls/src/index.js index c0c1fe21807864..06bba4fc91b9e3 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 { 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. * @@ -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 ); From da630d644a7b58045f8cc8a8bb5e4b32e8667d2a Mon Sep 17 00:00:00 2001 From: epiqueras Date: Thu, 23 Apr 2020 13:00:37 -0700 Subject: [PATCH 2/3] Editor: Use `syncSelect` in `resetEditorBlocks`. --- packages/editor/src/store/actions.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/editor/src/store/actions.js b/packages/editor/src/store/actions.js index 453a606c86206d..27c294b81c9752 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, + syncSelect, + 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 syncSelect( 'core', 'getEditedEntityRecord', 'postType', From c14a3369e3d9f228e8e46230e39b0e671309df3e Mon Sep 17 00:00:00 2001 From: epiqueras Date: Thu, 23 Apr 2020 13:47:18 -0700 Subject: [PATCH 3/3] Data Controls: Make `syncSelect` "__unstable". --- packages/data-controls/README.md | 29 ---------------------------- packages/data-controls/src/index.js | 10 +++++----- packages/editor/src/store/actions.js | 4 ++-- 3 files changed, 7 insertions(+), 36 deletions(-) diff --git a/packages/data-controls/README.md b/packages/data-controls/README.md index c82fdf10f0fb42..a85d9c4ac101aa 100644 --- a/packages/data-controls/README.md +++ b/packages/data-controls/README.md @@ -132,34 +132,5 @@ _Returns_ - `Object`: The control descriptor. -# **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. - diff --git a/packages/data-controls/src/index.js b/packages/data-controls/src/index.js index 06bba4fc91b9e3..91c97e4ec981d6 100644 --- a/packages/data-controls/src/index.js +++ b/packages/data-controls/src/index.js @@ -75,18 +75,18 @@ export function select( storeKey, selectorName, ...args ) { * * @example * ```js - * import { syncSelect } from '@wordpress/data-controls'; + * import { __unstableSyncSelect } from '@wordpress/data-controls'; * - * // Action generator using `syncSelect`. + * // Action generator using `__unstableSyncSelect`. * export function* myAction() { - * const isEditorSideBarOpened = yield syncSelect( 'core/edit-post', 'isEditorSideBarOpened' ); - * // Do stuff with the result from the `syncSelect`. + * const isEditorSideBarOpened = yield __unstableSyncSelect( 'core/edit-post', 'isEditorSideBarOpened' ); + * // Do stuff with the result from the `__unstableSyncSelect`. * } * ``` * * @return {Object} The control descriptor. */ -export function syncSelect( storeKey, selectorName, ...args ) { +export function __unstableSyncSelect( storeKey, selectorName, ...args ) { return { type: 'SYNC_SELECT', storeKey, diff --git a/packages/editor/src/store/actions.js b/packages/editor/src/store/actions.js index 27c294b81c9752..dc785fa4480bfd 100644 --- a/packages/editor/src/store/actions.js +++ b/packages/editor/src/store/actions.js @@ -10,7 +10,7 @@ import deprecated from '@wordpress/deprecated'; import { dispatch, select, - syncSelect, + __unstableSyncSelect, apiFetch, } from '@wordpress/data-controls'; import { parse, synchronizeBlocksWithTemplate } from '@wordpress/blocks'; @@ -677,7 +677,7 @@ export function* resetEditorBlocks( blocks, options = {} ) { if ( __unstableShouldCreateUndoLevel !== false ) { const { id, type } = yield select( STORE_KEY, 'getCurrentPost' ); const noChange = - ( yield syncSelect( + ( yield __unstableSyncSelect( 'core', 'getEditedEntityRecord', 'postType',