Skip to content

Commit

Permalink
Data Module: Update the dynamicReducer implementation and fix inline …
Browse files Browse the repository at this point in the history
…JS docs
  • Loading branch information
youknowriad committed Dec 13, 2017
1 parent 4f3753d commit 6c3dcc3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
26 changes: 14 additions & 12 deletions data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,32 @@ 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
*
* @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
Expand Down
3 changes: 2 additions & 1 deletion editor/store/persist.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion editor/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
5 changes: 3 additions & 2 deletions editor/store/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 6c3dcc3

Please sign in to comment.