import * as EasyPeasy from "easy-peasy"; const isServer = typeof window === "undefined"; const __NEXT_REDUX_STORE__ = "__NEXT_REDUX_STORE__"; let storeCreator = (initialState = {}) => createStore(initialState); const createStore = (model, config = {}) => { const store = EasyPeasy.createStore(model, { ...config, name: "AppState" }); // support a callback so we can get an object // of all store actions. // if (config.setActions) { // config.setActions(store.getActions()); // } return store; }; export const getOrCreateStore = (initialState) => { if (isServer) { return storeCreator(initialState); } if (!window[__NEXT_REDUX_STORE__]) { window[__NEXT_REDUX_STORE__] = storeCreator(initialState); } return window[__NEXT_REDUX_STORE__]; }; const store = (model: StoreModel, config = {}) => { storeCreator = (initialState) => createStore(model, { ...config, initialState }); if (isServer) return null; return () => window[__NEXT_REDUX_STORE__] as EasyPeasy.Store; }; export default store;