Skip to content

Commit

Permalink
Reduce rerendering on startup (#12847)
Browse files Browse the repository at this point in the history
* Ensure viewport data store changes happen immediately on startup.

We need to call `flush`, so that the changes happen synchronously,
rather than asynchronously, on the next event loop.
This avoids the re-rendering of all `BlockListBlock` instances
during startup.

* Ensure `state.hasUploadPermissions` is always a boolean.

The reducer for this property defaults to an empty object, which
not only is incorrect (since this is a boolean), but also causes
unnecessary re-renders by changing reference. By defaulting to
`true` instead, we fix both problems.

* Add a couple of tests for hasUploadPermissions reducer.

* Core Data: Add CHANGELOG entry for hasUplaodPermissions boolean fix

* Viewport: Add CHANGELOG entry for synchronous assignment
  • Loading branch information
sgomes authored and youknowriad committed Dec 21, 2018
1 parent d97a373 commit 73b1a4f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
6 changes: 6 additions & 0 deletions packages/core-data/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 2.0.16 (Unreleased)

### Bug Fixes

- Fixed the `hasUploadPermissions` selector to always return a boolean. Previously, it may have returned an empty object. This should have no impact for most consumers, assuming usage as a [truthy value](https://developer.mozilla.org/en-US/docs/Glossary/Truthy) in conditions.

## 2.0.15 (2018-12-12)

## 2.0.14 (2018-11-20)
Expand Down
2 changes: 1 addition & 1 deletion packages/core-data/src/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ export function embedPreviews( state = {}, action ) {
*
* @return {Object} Updated state.
*/
export function hasUploadPermissions( state = {}, action ) {
export function hasUploadPermissions( state = true, action ) {
switch ( action.type ) {
case 'RECEIVE_UPLOAD_PERMISSIONS':
return action.hasUploadPermissions;
Expand Down
21 changes: 20 additions & 1 deletion packages/core-data/src/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { filter } from 'lodash';
/**
* Internal dependencies
*/
import { terms, entities, embedPreviews } from '../reducer';
import { terms, entities, embedPreviews, hasUploadPermissions } from '../reducer';

describe( 'terms()', () => {
it( 'returns an empty object by default', () => {
Expand Down Expand Up @@ -117,3 +117,22 @@ describe( 'embedPreviews()', () => {
} );
} );
} );

describe( 'hasUploadPermissions()', () => {
it( 'returns true by default', () => {
const state = hasUploadPermissions( undefined, {} );

expect( state ).toEqual( true );
} );

it( 'returns with updated upload permissions value', () => {
const originalState = true;

const state = hasUploadPermissions( originalState, {
type: 'RECEIVE_UPLOAD_PERMISSIONS',
hasUploadPermissions: false,
} );

expect( state ).toEqual( false );
} );
} );
6 changes: 6 additions & 0 deletions packages/viewport/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 2.1.0 (Unreleased)

### Improvements

- The initial viewport state is assigned synchronously, rather than on the next process tick. This should have an impact of fewer callbacks made to data subscribers.

## 2.0.13 (2018-12-12)

## 2.0.12 (2018-11-20)
Expand Down
1 change: 1 addition & 0 deletions packages/viewport/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,4 @@ window.addEventListener( 'orientationchange', setIsMatching );

// Set initial values
setIsMatching();
setIsMatching.flush();

0 comments on commit 73b1a4f

Please sign in to comment.