Skip to content

Commit

Permalink
improve augment store to avoid to update the state twice (#6098)
Browse files Browse the repository at this point in the history
  • Loading branch information
allyoucanmap authored Oct 26, 2020
1 parent 994b08c commit a663577
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 7 deletions.
17 changes: 11 additions & 6 deletions web/client/utils/StateUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,18 @@ export const updateStore = ({ rootReducer, rootEpic, reducers = {}, epics = {} }
export const augmentStore = ({ reducers = {}, epics = {} } = {}, store) => {
const rootReducer = fetchReducer();
const reducer = (state, action) => {
const initialStoreKeys = Object.keys(rootReducer({}, {}));
const newState = {...state, ...rootReducer(state, action)};
return Object.keys(reducers).reduce((previous, current) => {
return {
...previous,
[current]: reducers[current](previous[current], action)
};
}, newState);
return Object.keys(reducers)
// avoid to update the state twice
// if the original store contains the same reducers
.filter((key) => initialStoreKeys.indexOf(key) === -1)
.reduce((previous, current) => {
return {
...previous,
[current]: reducers[current](previous[current], action)
};
}, newState);
};
(store || getStore()).replaceReducer(reducer);
const rootEpic = fetchEpic();
Expand Down
56 changes: 55 additions & 1 deletion web/client/utils/__tests__/StateUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@
*/
import ReactDOM from 'react-dom';
import expect from 'expect';
import {setStore, getStore, createStore, updateStore} from '../StateUtils';
import {
PERSISTED_STORE_NAME,
setStore,
getStore,
createStore,
updateStore,
augmentStore
} from '../StateUtils';
import Rx from 'rxjs';
import { setConfigProp } from '../ConfigUtils';

describe('StateUtils', () => {
beforeEach((done) => {
Expand Down Expand Up @@ -149,4 +157,50 @@ describe('StateUtils', () => {
expect(spy2.calls.length > 0).toBe(true);
expect(spy1.calls.length).toBe(beforeUpdateCalls);
});
it('should use the new added reducers (augmentStore)', () => {
const rootReducer = () => ({});
setConfigProp(PERSISTED_STORE_NAME + '.rootReducer', rootReducer);
const store = {
replaceReducer: (reducer) => {
reducer();
}
};
let reducersKeys = [];
augmentStore({ reducers: {
map: () => {
reducersKeys.push('map');
return {};
},
controls: () => {
reducersKeys.push('controls');
return {};
}
} }, store);
expect(reducersKeys).toEqual([ 'map', 'controls' ]);
setConfigProp(PERSISTED_STORE_NAME + '.rootReducer', undefined);
});
it('should not use the new added reducers if they are available in the root reducer (augmentStore)', () => {
const rootReducer = () => ({
map: () => ({})
});
setConfigProp(PERSISTED_STORE_NAME + '.rootReducer', rootReducer);
const store = {
replaceReducer: (reducer) => {
reducer();
}
};
let reducersKeys = [];
augmentStore({ reducers: {
map: () => {
reducersKeys.push('map');
return {};
},
controls: () => {
reducersKeys.push('controls');
return {};
}
} }, store);
expect(reducersKeys).toEqual([ 'controls' ]);
setConfigProp(PERSISTED_STORE_NAME + '.rootReducer', undefined);
});
});

0 comments on commit a663577

Please sign in to comment.