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

Data: Avoid calling listeners on unchanging state #5339

Merged
merged 1 commit into from
Mar 5, 2018
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
14 changes: 13 additions & 1 deletion data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,19 @@ export function registerReducer( reducerKey, reducer ) {
}
const store = createStore( reducer, flowRight( enhancers ) );
stores[ reducerKey ] = store;
store.subscribe( globalListener );

// Customize subscribe behavior to call listeners only on effective change,
// not on every dispatch.
let lastState = store.getState();
store.subscribe( () => {
const state = store.getState();
const hasChanged = state !== lastState;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per curiosity, do we have a lot of use-cases where a dispatch is triggered without any change in the state?

Copy link
Member Author

@aduth aduth Mar 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many of the actions which incur side-effects often don't do anything to state on their own, e.g.

  • SETUP_EDITOR
  • INSERT_DEFAULT_BLOCK
  • AUTOSAVE
  • FETCH_REUSABLE_BLOCKS

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some of these, it could make sense to move the effect to be part of the action creator itself. We might not need a standalone INSERT_DEFAULT_BLOCK action type if the insertDefaultBlock action creator just returned the result of insertBlock.

lastState = state;

if ( hasChanged ) {
globalListener();
}
} );

return store;
}
Expand Down
30 changes: 30 additions & 0 deletions data/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,26 @@ describe( 'withSelect', () => {

store.dispatch( { type: 'increment' } );
} );

itWithExtraAssertions( 'should not rerun selection on unchanging state', () => {
const store = registerReducer( 'unchanging', ( state = {} ) => state );

registerSelectors( 'unchanging', {
getState: ( state ) => state,
} );

const mapSelectToProps = jest.fn();

const Component = compose( [
withSelectImplementation( mapSelectToProps ),
] )( () => <div /> );

wrapper = mount( <Component /> );

store.dispatch( { type: 'dummy' } );

expect( mapSelectToProps ).toHaveBeenCalledTimes( 1 );
} );
}

cases( withSelect );
Expand Down Expand Up @@ -380,6 +400,16 @@ describe( 'subscribe', () => {

expect( secondListener ).toHaveBeenCalled();
} );

it( 'does not call listeners if state has not changed', () => {
const store = registerReducer( 'unchanging', ( state = {} ) => state );
const listener = jest.fn();
subscribeWithUnsubscribe( listener );

store.dispatch( { type: 'dummy' } );

expect( listener ).not.toHaveBeenCalled();
} );
} );

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