Skip to content

Commit

Permalink
Document deep cloning to make the intent clearer
Browse files Browse the repository at this point in the history
  • Loading branch information
tyxla committed Aug 29, 2022
1 parent 7c00ce0 commit 367e3ce
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
ROOT_LEVEL_ID,
} from './fixtures/block-list-context.native';

// Deep clone an object to avoid mutating it later.
const cloneObject = ( obj ) => JSON.parse( JSON.stringify( obj ) );

describe( 'findBlockLayoutByClientId', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ function useContextSystemBridge( { value } ) {
// that this should be super safe to assume that `useMemo` will only run on actual
// changes to the two dependencies, therefore saving us calls to `merge` and `JSON.parse/stringify`!
const config = useMemo( () => {
// Deep clone `parentContext` to avoid mutating it later.
return merge( JSON.parse( JSON.stringify( parentContext ) ), value );
}, [ parentContext, value ] );

Expand Down
2 changes: 2 additions & 0 deletions packages/edit-site/src/components/global-styles/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export function useSetting( path, blockName, source = 'all' ) {

const setSetting = ( newValue ) => {
setUserConfig( ( currentConfig ) => {
// Deep clone `currentConfig` to avoid mutating it later.
const newUserConfig = JSON.parse( JSON.stringify( currentConfig ) );
const pathToSet = PATHS_WITH_MERGE[ path ]
? fullPath + '.custom'
Expand Down Expand Up @@ -109,6 +110,7 @@ export function useStyle( path, blockName, source = 'all' ) {

const setStyle = ( newValue ) => {
setUserConfig( ( currentConfig ) => {
// Deep clone `currentConfig` to avoid mutating it later.
const newUserConfig = JSON.parse( JSON.stringify( currentConfig ) );
set(
newUserConfig,
Expand Down

0 comments on commit 367e3ce

Please sign in to comment.