Skip to content

Commit

Permalink
Only set document if changed
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Dec 13, 2023
1 parent d0cc253 commit d042da7
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ export const prePersistPostType = ( persistedRecord, edits ) => {
return newEdits;
};

const serialisableBlocksCache = new WeakMap();

function makeBlockAttributesSerializable( attributes ) {
const newAttributes = { ...attributes };
for ( const [ key, value ] of Object.entries( attributes ) ) {
Expand Down Expand Up @@ -341,14 +343,21 @@ async function loadPostTypeEntities() {
const document = doc.getMap( 'document' );

Object.entries( changes ).forEach( ( [ key, value ] ) => {
if (
document.get( key ) !== value &&
typeof value !== 'function'
) {
if ( typeof value !== 'function' ) {
if ( key === 'blocks' ) {
value = makeBlocksSerializable( value );
if ( ! serialisableBlocksCache.has( value ) ) {
serialisableBlocksCache.set(
value,
makeBlocksSerializable( value )
);
}

value = serialisableBlocksCache.get( value );
}

if ( document.get( key ) !== value ) {
document.set( key, value );
}
document.set( key, value );
}
} );
},
Expand Down

0 comments on commit d042da7

Please sign in to comment.