Skip to content

Commit

Permalink
Reuse mergedContext
Browse files Browse the repository at this point in the history
  • Loading branch information
SantosGuillamot committed Jul 24, 2024
1 parent f8e0e33 commit d5d070d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
14 changes: 9 additions & 5 deletions packages/blocks/src/api/test/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -1706,7 +1706,7 @@ describe( 'blocks', () => {

it( 'should correctly merge properties when bootstrap happens after registration', () => {
// Register source in the client.
const clientProperties = {
const clientOnlyProperties = {
getValues: () => 'values',
setValues: () => 'new values',
getPlaceholder: () => 'placeholder',
Expand All @@ -1715,7 +1715,8 @@ describe( 'blocks', () => {
registerBlockBindingsSource( {
name: 'core/custom-source',
label: 'Client Label',
...clientProperties,
usesContext: [ 'postId', 'postType' ],
...clientOnlyProperties,
} );

// Bootstrap source from the server.
Expand All @@ -1724,14 +1725,17 @@ describe( 'blocks', () => {
).addBootstrappedBlockBindingsSource( {
name: 'core/custom-source',
label: 'Server Label',
usesContext: [ 'postId' ],
usesContext: [ 'postId', 'serverContext' ],
} );

// Check that the bootstrap values prevail and the client properties are still there.
expect( getBlockBindingsSource( 'core/custom-source' ) ).toEqual( {
// Should use the server label.
label: 'Server Label',
usesContext: [ 'postId' ],
...clientProperties,
// Should merge usesContext from server and client.
usesContext: [ 'postId', 'postType', 'serverContext' ],
// Should keep client properties.
...clientOnlyProperties,
} );

unregisterBlockBindingsSource( 'core/custom-source' );
Expand Down
24 changes: 12 additions & 12 deletions packages/blocks/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,19 +372,19 @@ export function collections( state = {}, action ) {
}

export function blockBindingsSources( state = {}, action ) {
// Merge usesContext with existing values, potentially defined in the server registration.
let mergedUsesContext = [
...( state[ action.name ]?.usesContext || [] ),
...( action.usesContext || [] ),
];
// Remove duplicates.
mergedUsesContext =
mergedUsesContext.length > 0
? [ ...new Set( mergedUsesContext ) ]
: undefined;

switch ( action.type ) {
case 'ADD_BLOCK_BINDINGS_SOURCE':
// Merge usesContext with existing values, potentially defined in the server registration.
let mergedUsesContext = [
...( state[ action.name ]?.usesContext || [] ),
...( action.usesContext || [] ),
];
// Remove duplicates.
mergedUsesContext =
mergedUsesContext.length > 0
? [ ...new Set( mergedUsesContext ) ]
: undefined;

return {
...state,
[ action.name ]: {
Expand All @@ -407,7 +407,7 @@ export function blockBindingsSources( state = {}, action ) {
*/
...state[ action.name ],
label: action.label,
usesContext: action.usesContext,
usesContext: mergedUsesContext,
},
};
case 'REMOVE_BLOCK_BINDINGS_SOURCE':
Expand Down

0 comments on commit d5d070d

Please sign in to comment.