Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid creating new state references if unchanged #1063

Merged
merged 1 commit into from
Jun 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 47 additions & 5 deletions editor/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import { combineReducers, applyMiddleware, createStore } from 'redux';
import refx from 'refx';
import { keyBy, first, last, omit, without, flowRight } from 'lodash';
import { reduce, keyBy, first, last, omit, without, flowRight } from 'lodash';

/**
* Internal dependencies
Expand All @@ -30,11 +30,27 @@ export const editor = combineUndoableReducers( {
switch ( action.type ) {
case 'EDIT_POST':
case 'SETUP_NEW_POST':
return {
...state,
...action.edits,
};
return reduce( action.edits, ( result, value, key ) => {
// Only assign into result if not already same value
if ( value !== state[ key ] ) {
// Avoid mutating original state by creating shallow
// clone. Should only occur once per reduce.
if ( result === state ) {
result = { ...state };
}

result[ key ] = value;
}

return result;
}, state );

case 'CLEAR_POST_EDITS':
// Don't return a new object if there's not any edits
if ( ! Object.keys( state ).length ) {
return state;
}

return {};
}

Expand Down Expand Up @@ -66,6 +82,32 @@ export const editor = combineUndoableReducers( {
return keyBy( action.blocks, 'uid' );

case 'UPDATE_BLOCK':
// Ignore updates if block isn't known
if ( ! state[ action.uid ] ) {
return state;
}

// Consider as updates only changed values
const nextBlock = reduce( action.updates, ( result, value, key ) => {
if ( value !== result[ key ] ) {
// Avoid mutating original block by creating shallow clone
if ( result === state[ action.uid ] ) {
result = { ...state[ action.uid ] };
}

result[ key ] = value;
}

return result;
}, state[ action.uid ] );

// Skip update if nothing has been changed. The reference will
// match the original block if `reduce` had no changed values.
if ( nextBlock === state[ action.uid ] ) {
return state;
}

// Otherwise merge updates into state
return {
...state,
[ action.uid ]: {
Expand Down
117 changes: 96 additions & 21 deletions editor/test/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,6 @@ describe( 'state', () => {
expect( state.blockOrder ).to.eql( [ 'bananas' ] );
} );

it( 'should return with block updates', () => {
const original = editor( undefined, {
type: 'RESET_BLOCKS',
blocks: [ {
uid: 'kumquat',
attributes: {},
} ],
} );
const state = editor( original, {
type: 'UPDATE_BLOCK',
uid: 'kumquat',
updates: {
attributes: {
updated: true,
},
},
} );

expect( state.blocksByUid.kumquat.attributes.updated ).to.be.true();
} );

it( 'should insert block', () => {
const original = editor( undefined, {
type: 'RESET_BLOCKS',
Expand Down Expand Up @@ -363,6 +342,25 @@ describe( 'state', () => {
} );
} );

it( 'should return same reference if no changed properties', () => {
const original = editor( undefined, {
type: 'EDIT_POST',
edits: {
status: 'draft',
title: 'post title',
},
} );

const state = editor( original, {
type: 'EDIT_POST',
edits: {
status: 'draft',
},
} );

expect( state.edits ).to.equal( original.edits );
} );

it( 'should save modified properties', () => {
const original = editor( undefined, {
type: 'EDIT_POST',
Expand Down Expand Up @@ -405,6 +403,19 @@ describe( 'state', () => {
expect( state.edits ).to.eql( {} );
} );

it( 'should return same reference if clearing non-edited', () => {
const original = editor( undefined, {
type: 'EDIT_POST',
edits: {},
} );

const state = editor( original, {
type: 'CLEAR_POST_EDITS',
} );

expect( state.edits ).to.equal( original.edits );
} );

it( 'should save initial post state', () => {
const state = editor( undefined, {
type: 'SETUP_NEW_POST',
Expand Down Expand Up @@ -484,6 +495,70 @@ describe( 'state', () => {
expect( state.dirty ).to.be.false();
} );
} );

describe( 'blocksByUid', () => {
it( 'should return with block updates', () => {
const original = editor( undefined, {
type: 'RESET_BLOCKS',
blocks: [ {
uid: 'kumquat',
attributes: {},
} ],
} );
const state = editor( original, {
type: 'UPDATE_BLOCK',
uid: 'kumquat',
updates: {
attributes: {
updated: true,
},
},
} );

expect( state.blocksByUid.kumquat.attributes.updated ).to.be.true();
} );

it( 'should ignore updates to non-existant block', () => {
const original = editor( undefined, {
type: 'RESET_BLOCKS',
blocks: [],
} );
const state = editor( original, {
type: 'UPDATE_BLOCK',
uid: 'kumquat',
updates: {
attributes: {
updated: true,
},
},
} );

expect( state.blocksByUid ).to.equal( original.blocksByUid );
} );

it( 'should return with same reference if no changes in updates', () => {
const original = editor( undefined, {
type: 'RESET_BLOCKS',
blocks: [ {
uid: 'kumquat',
attributes: {
updated: true,
},
} ],
} );
const state = editor( original, {
type: 'UPDATE_BLOCK',
uid: 'kumquat',
updates: {
attributes: {
updated: true,
},
},
} );

expect( state.blocksByUid ).to.equal( state.blocksByUid );
} );
} );
} );

describe( 'currentPost()', () => {
Expand Down