Skip to content

Commit

Permalink
remove subscribe
Browse files Browse the repository at this point in the history
  • Loading branch information
mxkae committed Mar 20, 2024
1 parent 0d0226a commit 81c4405
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 40 deletions.
40 changes: 1 addition & 39 deletions src/hooks/use-block-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
* WordPress dependencies
*/
import {
subscribe, select, createReduxStore, register, dispatch, useSelect,
select, createReduxStore, register, useSelect,
} from '@wordpress/data'
import { useBlockEditContext } from '@wordpress/block-editor'

Expand Down Expand Up @@ -140,44 +140,6 @@ register( createReduxStore( 'stackable/block-context', {
selectors: STORE_SELECTORS,
} ) )

// This holds the current tree of client ids, we check against this if the
// block/content structure has changed.
let prevClientIds = null
// let oldAllClientIds = null

// Subscribe to all editor changes, so we can listen in to block structure
// changes.
subscribe( () => {
const tree = select( 'stackable/block-editor' )?.getClientTree()
if ( tree && ! prevClientIds ) {
prevClientIds = tree
const blocks = fixReusableInnerBlocks( tree )
dispatch( 'stackable/block-context' ).setBlockTree( blocks )
return
}

// We can do a direct comparison here since the object being returned
// isn't changed unless the client id tree is changed, so this holds up
// even when blocks are edited.
if ( tree && tree !== prevClientIds ) {
prevClientIds = tree
const blocks = fixReusableInnerBlocks( tree )
dispatch( 'stackable/block-context' ).setBlockTree( blocks )
}
} )

// Use to correct the blocks returned from getBlocks.
// Applies only core/block (reusable blocks) - Adds missing innerBlocks
const fixReusableInnerBlocks = blocks => {
return ( blocks || [] ).map( block => {
return {
...block,
innerBlocks: fixReusableInnerBlocks( block.innerBlocks ),
name: select( 'core/block-editor' ).getBlockName( block.clientId ),
}
} )
}

// The default context if none is found. This can be true when the block is
// being previewed as an example.
const DEFAULT_CONTEXT = {
Expand Down
18 changes: 17 additions & 1 deletion src/plugins/get-client-id-tree/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import {
register, createReduxStore,
select,
select, dispatch,
} from '@wordpress/data'

// Include all the stored state.
Expand Down Expand Up @@ -49,10 +49,26 @@ const getUnmemoizedClientTree = rootClientId => {
return result
}

// Use to correct the blocks returned from getBlocks.
// Applies only core/block (reusable blocks) - Adds missing innerBlocks
const fixReusableInnerBlocks = blocks => {
return ( blocks || [] ).map( block => {
return {
...block,
innerBlocks: fixReusableInnerBlocks( block.innerBlocks ),
name: select( 'core/block-editor' ).getBlockName( block.clientId ),
}
} )
}

const STORE_REDUCER = ( state = DEFAULT_STATE, action ) => {
switch ( action.type ) {
case 'UPDATE_CLIENT_TREE': {
const tree = getUnmemoizedClientTree()
if ( tree ) {
const blocks = fixReusableInnerBlocks( tree )
dispatch( 'stackable/block-context' ).setBlockTree( blocks )
}

return {
...state,
Expand Down

0 comments on commit 81c4405

Please sign in to comment.