Vulcan/packages/nova-lib/lib/client/store.js

26 lines
829 B
JavaScript
Raw Normal View History

2017-01-31 10:22:02 +08:00
import { createStore, applyMiddleware, compose, combineReducers } from 'redux';
import { getMiddlewares, getReducers } from '../redux.js';
function mainMiddleware(store) {
let chain;
return (next) => (action) => {
if (!chain || action.type === 'STORE_RELOADED' ) {
chain = getMiddlewares().map(middleware => middleware(store));
}
return compose(...chain)(next)(action);
}
}
export const store = createStore(s => s, {}, compose(
applyMiddleware(mainMiddleware),
typeof window !== "undefined" && window.devToolsExtension ? window.devToolsExtension() : f => f
));
store.reload = function reload(initialState = {}) {
// this.replaceReducer(() => initialState);
const rootReducer = combineReducers(getReducers());
this.replaceReducer(rootReducer);
this.dispatch({ type: "STORE_RELOADED" });
};