2016-07-16 02:06:10 +00:00
|
|
|
import {createStore, applyMiddleware} from 'redux';
|
2016-10-21 17:20:36 +00:00
|
|
|
import createLogger from 'redux-logger';
|
2016-03-16 05:49:42 +00:00
|
|
|
import thunkMiddleware from 'redux-thunk'
|
2016-07-16 02:06:10 +00:00
|
|
|
import localStorageMiddleware, {getState} from './middleware/localstorage';
|
|
|
|
import rootReducer from './reducer';
|
2016-10-02 20:57:00 +00:00
|
|
|
import {LOCALSTORAGE_KEY} from '../../backend/constants';
|
2016-03-16 05:49:42 +00:00
|
|
|
|
2016-04-29 05:58:37 +00:00
|
|
|
|
2016-04-29 03:37:49 +00:00
|
|
|
export default function configureStore () {
|
2016-04-29 05:58:37 +00:00
|
|
|
const middleware = [
|
|
|
|
thunkMiddleware,
|
|
|
|
localStorageMiddleware(LOCALSTORAGE_KEY)
|
|
|
|
];
|
|
|
|
|
|
|
|
if (__DEV__) {
|
2016-04-30 05:01:57 +00:00
|
|
|
// middleware.push(createLogger({collapsed: true}));
|
2016-04-29 05:58:37 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 03:37:49 +00:00
|
|
|
// Create the store and apply middleware
|
2016-10-21 17:20:36 +00:00
|
|
|
const restoredState = getState(LOCALSTORAGE_KEY);
|
|
|
|
|
|
|
|
// Remove unused keys from restored state (migrate it)
|
|
|
|
const initialState = rootReducer(undefined, {type: 'insomnia-init'});
|
|
|
|
for (const key of Object.keys(restoredState)) {
|
|
|
|
if (!initialState.hasOwnProperty(key)) {
|
|
|
|
// TODO: Make this recursive
|
|
|
|
console.warn('Deleting unused key from restored state', key);
|
|
|
|
delete restoredState[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-20 04:00:40 +00:00
|
|
|
const store = createStore(
|
|
|
|
rootReducer,
|
2016-10-21 17:20:36 +00:00
|
|
|
restoredState,
|
2016-04-29 05:58:37 +00:00
|
|
|
applyMiddleware(...middleware)
|
2016-03-20 04:00:40 +00:00
|
|
|
);
|
2016-03-16 05:49:42 +00:00
|
|
|
|
2016-03-20 04:00:40 +00:00
|
|
|
if (module.hot) {
|
2016-04-23 06:16:23 +00:00
|
|
|
module.hot.accept('./reducer', () => {
|
2016-10-21 17:20:36 +00:00
|
|
|
const nextReducer = require('./reducer');
|
2016-03-20 04:00:40 +00:00
|
|
|
store.replaceReducer(nextReducer);
|
|
|
|
})
|
|
|
|
}
|
2016-03-16 05:49:42 +00:00
|
|
|
|
2016-04-26 07:29:24 +00:00
|
|
|
return store;
|
2016-03-20 04:00:40 +00:00
|
|
|
}
|