-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds the ability to code-splitting reducers in order to use them when needed BREAKING CHANGE: initial store is replace by dynoStore improves #32
- Loading branch information
1 parent
c6146f6
commit 11d8576
Showing
5 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* eslint-disable implicit-arrow-linebreak */ | ||
// @flow strict | ||
import { | ||
createStore as createReduxStore, | ||
combineReducers, | ||
} from 'redux'; | ||
import type { Dispatch } from 'redux'; | ||
|
||
// import type { State } from '@/store/types/State'; | ||
import type { Actions } from '@/store/types/Actions'; | ||
|
||
import reduceReducers from './reduceReducers'; | ||
|
||
let store = {}; | ||
const reducerMap = {}; | ||
|
||
// $FlowFixMe | ||
const injectReducers = (_reducerMap): void => { | ||
Object.entries(_reducerMap).forEach(([name, reducer]) => { | ||
if (!reducerMap[name]) { | ||
reducerMap[name] = []; | ||
} | ||
reducerMap[name].push(reducer); | ||
}); | ||
}; | ||
|
||
const createRootReducer = () => ( | ||
// $FlowFixMe | ||
combineReducers(Object.keys(reducerMap).reduce((result, key) => ({ | ||
...result, | ||
[key]: reduceReducers(reducerMap[key]), | ||
}), {})) | ||
); | ||
|
||
// $FlowFixMe | ||
const createStore = (...args) => { | ||
store = createReduxStore<{}, Actions, Dispatch<Actions>>(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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// @flow strict | ||
|
||
// eslint-disable-next-line import/prefer-default-export | ||
export { default as dynoStore } from './dynoStore'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// @flow strict | ||
import type { State } from '@/store/types/State'; | ||
import type { Actions } from '@/store/types/Actions'; | ||
|
||
type ReducersType = Array<($Shape<State>, Actions) => {}>; | ||
|
||
const reduceReducers = (reducers: ReducersType) => (state: State, action: Actions) => ( | ||
reducers.reduce((result, reducer) => ( | ||
reducer(result, action) | ||
), state) | ||
); | ||
|
||
export default reduceReducers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// @flow strict | ||
import { compose, applyMiddleware } from 'redux'; | ||
import type { Dispatch, StoreEnhancer } from 'redux'; | ||
|
||
import middlewares from '@/store/config/middlewares'; | ||
|
||
import type { State } from '@/store/types/State'; | ||
import type { Actions } from '@/store/types/Actions'; | ||
|
||
type EnhancersType = StoreEnhancer<State, Actions, Dispatch<Actions>> | ||
/* eslint-disable */ | ||
const devtools = | ||
typeof window !== 'undefined' && | ||
typeof window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ === 'function' && | ||
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({ actionsBlacklist: [] }); | ||
/* eslint-enable */ | ||
const composeEnhancers = devtools || compose; | ||
// eslint-disable-next-line no-underscore-dangle | ||
const enhancer: EnhancersType = composeEnhancers(applyMiddleware(...middlewares)); | ||
|
||
export default enhancer; |