From 6c3dcc34343e24b91c8aa739ad198544cb7db90d Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Wed, 13 Dec 2017 13:06:05 +0100 Subject: [PATCH] Data Module: Update the dynamicReducer implementation and fix inline JS docs --- data/index.js | 26 ++++++++++++++------------ editor/store/persist.js | 3 ++- editor/store/reducer.js | 1 - editor/store/store.js | 5 +++-- 4 files changed, 19 insertions(+), 16 deletions(-) diff --git a/data/index.js b/data/index.js index d6ceded617eea7..2a48b3f9bbe15d 100644 --- a/data/index.js +++ b/data/index.js @@ -12,10 +12,9 @@ const enhancers = []; if ( window.__REDUX_DEVTOOLS_EXTENSION__ ) { enhancers.push( window.__REDUX_DEVTOOLS_EXTENSION__() ); } -const store = createStore( dynamicReducer, {}, flowRight( enhancers ) ); /** - * Reducer function combining the dynamic "reducers" array + * Combines the dynamic "reducers" array to create one reducer * * @param {Object} state Global state * @param {Object} action Action @@ -23,19 +22,22 @@ const store = createStore( dynamicReducer, {}, flowRight( enhancers ) ); * @return {Object} Updated global state */ function dynamicReducer( state = {}, action ) { - let hasChanges = false; - const newState = {}; - reducers.forEach( ( { key, reducer } ) => { - const newSubState = reducer( state[ key ] || {}, action ); - hasChanges = hasChanges || newSubState !== state[ key ]; - newState[ key ] = newSubState; - } ); - - return hasChanges ? newState : state; + let hasChanged = false; + const nextState = {}; + for ( let i = 0; i < reducers.length; i++ ) { + const { key, reducer } = reducers[ i ]; + const previousStateForKey = state[ key ]; + const nextStateForKey = reducer( previousStateForKey, action ); + nextState[ key ] = nextStateForKey; + hasChanged = hasChanged || nextStateForKey !== previousStateForKey; + } + return hasChanged ? nextState : state; } +const store = createStore( dynamicReducer, {}, flowRight( enhancers ) ); + /** - * Register a new sub reducer to the global sate + * Registers a new sub reducer to the global state * * @param {String} key Reducer key * @param {Object} reducer Reducer function diff --git a/editor/store/persist.js b/editor/store/persist.js index b614b2d559fd1e..0dac962c6e5fa1 100644 --- a/editor/store/persist.js +++ b/editor/store/persist.js @@ -35,7 +35,8 @@ export function withRehydratation( reducer, reducerKey ) { } /** - * Load the initial state and persist on changes + * Loads the initial state and persist on changes + * * This should be executed after the reducer's registration * * @param {String} reducerKey The reducer key to persist (example: reducerKey.subReducerKey) diff --git a/editor/store/reducer.js b/editor/store/reducer.js index fb784cb98c2bbb..74025122900729 100644 --- a/editor/store/reducer.js +++ b/editor/store/reducer.js @@ -709,7 +709,6 @@ export function metaBoxes( state = defaultMetaBoxState, action ) { } } -// Create responsive reducer with the breakpoints imported from the scss variables file. export function browser( state = {}, action ) { if ( action.type === 'BROWSER_RESIZE' ) { return { width: action.width, height: action.height }; diff --git a/editor/store/store.js b/editor/store/store.js index 377274bb6d099b..1d26d312d14a94 100644 --- a/editor/store/store.js +++ b/editor/store/store.js @@ -17,8 +17,9 @@ import { mobileMiddleware } from '../utils/mobile'; import effects from './effects'; /** - * This function applies the custom middlewares used specifically in the editor moodule - * It also restricts the getState call to the module's partial state only. + * Applies the custom middlewares used specifically in the editor module + * + * It also restricts the getState call to the module's partial state only * * @return {Object} Redux custom store */