Skip to content

Instantly share code, notes, and snippets.

@quirinpa
Last active August 29, 2015 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quirinpa/dca56c13fe90bd3991d8 to your computer and use it in GitHub Desktop.
Save quirinpa/dca56c13fe90bd3991d8 to your computer and use it in GitHub Desktop.
/* global __DEVELOPMENT__, __CLIENT__, __DEVTOOLS__ */
import { createStore, combineReducers, applyMiddleware, compose } from 'redux';
import alsoMiddleware from 'middleware/alsoMiddleware';
import deferMiddleware from 'middleware/deferMiddleware';
import clientMiddlewareCreator from 'middleware/clientMiddlewareCreator';
import waitMiddleware from 'middleware/waitMiddleware';
import { routerStateReducer } from 'redux-react-router';
import * as reducers from 'ducks';
import ApiClient from './ApiClient';
let lastCreatedStore;
export default function(req, data, location) {
const createReducer = () => combineReducers({
// action: function action(state = false, action) {
// console.log('ACTION:', action.type);
// return state;
// },
router: routerStateReducer,
...reducers
});
let reducer = createReducer();
if (module.hot) {
module.hot.accept('ducks', () => {
reducer = createReducer();
lastCreatedStore.replaceReducer(reducer);
});
}
const client = req ?
new ApiClient(req) :
new ApiClient();
const applyMiddlewares = applyMiddleware(
clientMiddlewareCreator(client),
deferMiddleware,
alsoMiddleware,
waitMiddleware
);
const finalCreateStore = (() => {
if (__SERVER__) return createStore;
let storeParts = [applyMiddlewares];
if (window.__session.logged) {
const adapter = require('redux-localstorage/lib/adapters/localStorage');
const persistState = require('redux-localstorage')
.default(adapter(window.localStorage), 'redux');
storeParts.push(persistState);
}
if (__DEVELOPMENT__ && __DEVTOOLS__) {
const { devTools, persistState: devPersistState } = require('redux-devtools');
storeParts = storeParts.concat([
devTools(),
devPersistState(window.location.href.match(/[?&]debug_session=([^&]+)\b/))
]);
}
return compose(
...storeParts,
createStore
);
})();
const store = finalCreateStore(reducer, __SERVER__ && req.session.redux ? {
...data,
...req.session.redux,
router: location
} : data);
store.client = client;
lastCreatedStore = store;
return store;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment