Skip to content

Commit

Permalink
feat(store): set initial dynoStore
Browse files Browse the repository at this point in the history
closes #32, #33, #34, #35
  • Loading branch information
aneurysmjs committed Oct 3, 2019
1 parent 0a70f7f commit 2d6bba3
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/app/store/config/dynoStore/dynoStore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* eslint-disable implicit-arrow-linebreak */
import {
createStore as createReduxStore,
combineReducers,
} from 'redux';

import reduceReducers from './reduceReducers';

let store = {};
const reducerMap = {};

const injectReducers = (_reducerMap): void => {
Object.entries(_reducerMap).forEach(([name, reducer]) => {
if (!reducerMap[name]) {
reducerMap[name] = [];
}
reducerMap[name].push(reducer);
});
};

const createRootReducer = () => (
combineReducers(Object.keys(reducerMap).reduce((result, key) => ({
...result,
[key]: reduceReducers(reducerMap[key]),
}), {}))
);

const createStore = (...args) => {
store = createReduxStore(createRootReducer(), ...args);
return store;
};

const reloadStore = (): void => {
store.replaceReducer(createRootReducer());
// store.dispatch({ type: '@@replace-reducer' });
};

export const dynoStore = {
injectReducers,
createRootReducer,
createStore,
reloadStore,
};

export const withReloadStore = (importPromise: *): Promise<*> => (
importPromise
.then((module) => {
dynoStore.reloadStore();
return module;
},
(error) => {
throw error;
})
);

export default dynoStore;
1 change: 1 addition & 0 deletions src/app/store/config/dynoStore/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './dynoStore';
13 changes: 13 additions & 0 deletions src/app/store/config/dynoStore/reduceReducers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @flow strict
import type { State } from '@/shared/types/State';
import type { Actions } from '@/shared/types/Actions';

type ReducersType = Array<(State, Actions) => {}>;

const reduceReducers = (reducers: ReducersType) => (state: State, action: Actions) => (
reducers.reduce((result, reducer) => (
reducer(result, action)
), state)
);

export default reduceReducers;

0 comments on commit 2d6bba3

Please sign in to comment.