Last active
August 29, 2015 14:28
-
-
Save quirinpa/dca56c13fe90bd3991d8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* 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