Skip to content

Commit

Permalink
Blocks: Refactor generator-based actions to thunks (#36468)
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Nov 29, 2021
1 parent b7a4e2b commit fb8a732
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
51 changes: 26 additions & 25 deletions packages/blocks/src/store/actions.js
Expand Up @@ -6,13 +6,11 @@ import { castArray, isFunction, isPlainObject, omit, pick, some } from 'lodash';
/**
* WordPress dependencies
*/
import { select } from '@wordpress/data';
import { applyFilters } from '@wordpress/hooks';

/**
* Internal dependencies
*/
import { STORE_NAME } from './constants';
import { isValidIcon, normalizeIconObject } from '../api/utils';
import { DEPRECATED_ENTRY_KEYS } from '../api/constants';

Expand All @@ -36,11 +34,13 @@ const LEGACY_CATEGORY_MAPPING = {
* Takes the unprocessed block type data and applies all the existing filters for the registered block type.
* Next, it validates all the settings and performs additional processing to the block type definition.
*
* @param {WPBlockType} blockType Unprocessed block type settings.
* @param {WPBlockType} blockType Unprocessed block type settings.
* @param {Object} thunkArgs Argument object for the thunk middleware.
* @param {Function} thunkArgs.select Function to select from the store.
*
* @return {?WPBlockType} The block, if it has been successfully registered; otherwise `undefined`.
*/
function processBlockType( blockType ) {
const processBlockType = ( blockType, { select } ) => {
const { name } = blockType;

const settings = applyFilters(
Expand Down Expand Up @@ -92,7 +92,7 @@ function processBlockType( blockType ) {

if (
'category' in settings &&
! some( select( STORE_NAME ).getCategories(), {
! some( select.getCategories(), {
slug: settings.category,
} )
) {
Expand Down Expand Up @@ -125,7 +125,7 @@ function processBlockType( blockType ) {
}

return settings;
}
};

/**
* Returns an action object used in signalling that block types have been added.
Expand All @@ -142,27 +142,28 @@ export function addBlockTypes( blockTypes ) {
}

/**
* Yields action objects signaling that the passed block type's settings should be stored in the state.
* Signals that the passed block type's settings should be stored in the state.
*
* @param {WPBlockType} blockType Unprocessed block type settings.
*
* @yield {Object} Action object.
*/
export function* __experimentalRegisterBlockType( blockType ) {
yield {
export const __experimentalRegisterBlockType = ( blockType ) => ( {
dispatch,
select,
} ) => {
dispatch( {
type: 'ADD_UNPROCESSED_BLOCK_TYPE',
blockType,
};
} );

const processedBlockType = processBlockType( blockType );
const processedBlockType = processBlockType( blockType, { select } );
if ( ! processedBlockType ) {
return;
}
yield addBlockTypes( processedBlockType );
}
dispatch.addBlockTypes( processedBlockType );
};

/**
* Yields an action object signaling that all block types should be computed again.
* Signals that all block types should be computed again.
* It uses stored unprocessed block types and all the most recent list of registered filters.
*
* It addresses the issue where third party block filters get registered after third party blocks. A sample sequence:
Expand All @@ -174,18 +175,18 @@ export function* __experimentalRegisterBlockType( blockType ) {
* 6. Block F.
* 7. Filter G.
* In this scenario some filters would not get applied for all blocks because they are registered too late.
*
* @yield {Object} Action object.
*/
export function* __experimentalReapplyBlockTypeFilters() {
const unprocessedBlockTypes = select(
STORE_NAME
).__experimentalGetUnprocessedBlockTypes();
export const __experimentalReapplyBlockTypeFilters = () => ( {
dispatch,
select,
} ) => {
const unprocessedBlockTypes = select.__experimentalGetUnprocessedBlockTypes();

const processedBlockTypes = Object.keys( unprocessedBlockTypes ).reduce(
( accumulator, blockName ) => {
const result = processBlockType(
unprocessedBlockTypes[ blockName ]
unprocessedBlockTypes[ blockName ],
{ select }
);
if ( result ) {
accumulator.push( result );
Expand All @@ -199,8 +200,8 @@ export function* __experimentalReapplyBlockTypeFilters() {
return;
}

yield addBlockTypes( processedBlockTypes );
}
dispatch.addBlockTypes( processedBlockTypes );
};

/**
* Returns an action object used to remove a registered block type.
Expand Down
1 change: 1 addition & 0 deletions packages/blocks/src/store/index.js
Expand Up @@ -22,6 +22,7 @@ export const store = createReduxStore( STORE_NAME, {
reducer,
selectors,
actions,
__experimentalUseThunks: true,
} );

register( store );

0 comments on commit fb8a732

Please sign in to comment.