2017-06-29 12:54:02 -06:00
|
|
|
import { createStore } from "redux";
|
|
|
|
import { Store } from "./interfaces";
|
|
|
|
import { rootReducer } from "./root_reducer";
|
|
|
|
import { registerSubscribers } from "./subscribers";
|
|
|
|
import { getMiddleware } from "./middlewares";
|
2018-01-14 20:58:20 -07:00
|
|
|
import { set } from "lodash";
|
2017-06-29 12:54:02 -06:00
|
|
|
|
2018-01-14 20:58:20 -07:00
|
|
|
function dev(): Store {
|
2019-12-10 12:53:20 -07:00
|
|
|
return createStore(rootReducer,
|
2017-06-29 12:54:02 -06:00
|
|
|
maybeFetchOldState(),
|
|
|
|
getMiddleware("development"));
|
|
|
|
}
|
|
|
|
|
2018-01-14 20:58:20 -07:00
|
|
|
function prod(): Store {
|
2018-08-08 08:44:12 -06:00
|
|
|
// tslint:disable-next-line:no-any
|
2018-01-14 20:58:20 -07:00
|
|
|
return createStore(rootReducer, ({} as any), getMiddleware("production"));
|
2017-06-29 12:54:02 -06:00
|
|
|
}
|
|
|
|
|
2018-03-13 16:37:24 -06:00
|
|
|
export function configureStore() {
|
2018-01-19 10:49:07 -07:00
|
|
|
const ENV = process.env.NODE_ENV || "development";
|
2017-08-28 06:01:11 -06:00
|
|
|
const store2: Store = (ENV === "production" ? prod() : dev());
|
2017-07-20 12:26:44 -06:00
|
|
|
// Make store global in case I need to probe it.
|
2018-01-14 20:58:20 -07:00
|
|
|
set(window, "store", store2);
|
2017-08-28 06:01:11 -06:00
|
|
|
registerSubscribers(store2);
|
|
|
|
return store2;
|
2017-06-29 12:54:02 -06:00
|
|
|
}
|
|
|
|
|
2019-12-10 12:53:20 -07:00
|
|
|
export const store = configureStore();
|
2017-06-29 12:54:02 -06:00
|
|
|
|
|
|
|
/** Tries to fetch previous state from `sessionStorage`.
|
|
|
|
* Returns {} if nothing is found. Used mostly for hot reloading. */
|
|
|
|
function maybeFetchOldState() {
|
|
|
|
try {
|
2018-08-02 16:18:54 -06:00
|
|
|
return JSON.parse(sessionStorage.getItem("lastState") || "{}");
|
2017-06-29 12:54:02 -06:00
|
|
|
} catch (e) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|