UNPKG

@shopgate/pwa-common

Version:

Common library for the Shopgate Connect PWA.

18 lines 2.51 kB
import{createStore,applyMiddleware}from'redux';import thunk from'redux-thunk';import{composeWithDevTools}from'@redux-devtools/extension';import{persistState}from'@virtuous/redux-persister';import benchmarkMiddleware from'@shopgate/pwa-benchmark/profilers/redux';import benchmarkController from'@shopgate/pwa-benchmark';import persistedReducers from"../collections/PersistedReducers";import initSubscribers from"../subscriptions";import appConfig,{themeName,shopNumber}from"../helpers/config";import makeRootReducer from"../reducers";import streams from"./middelwares/streams";import logger from"./middelwares/logger";/** * The current version of the state created by this reducer. * @type {string} */var STATE_VERSION='v3';var storeKey="shopgate-connect_".concat(shopNumber,"-").concat(themeName,"_").concat(STATE_VERSION);/** * Returns a normalized initialState from the local storage. * @returns {Object} */function getInitialState(){if(!window.localStorage){return undefined;}var storedState=window.localStorage.getItem(storeKey);if(!storedState){return undefined;}var normalizedState=storedState.replace(new RegExp('"isFetching":true','g'),'"isFetching":false');return JSON.parse(normalizedState);}/** * Configures the redux store with all it's middleware and enhancers. * @param {Function} reducers The reducers from the theme. * @param {Array} subscribers The subscribers to the streams middleware. * @return {Object} The redux store. */export function configureStore(reducers,subscribers){// Starts benchmark controller BEFORE adding the middleware. if(appConfig.benchmark){benchmarkController.startup();}var store=createStore(makeRootReducer(reducers),getInitialState(),composeWithDevTools(applyMiddleware.apply(void 0,[thunk].concat(appConfig.benchmark?[benchmarkMiddleware]:[],[streams,logger])),persistState({key:storeKey,paths:persistedReducers.getAll()})));initSubscribers(subscribers);if(window.Cypress){window.store=store;}return store;}/** * Configures a mocked store for tests. * @param {Function} reducers The reducers to use in the mock store. * @param {Array} subscribers Streams subscribers to initialize. * @returns {Store} */export function createMockStore(){var reducers=arguments.length>0&&arguments[0]!==undefined?arguments[0]:function(){};var subscribers=arguments.length>1&&arguments[1]!==undefined?arguments[1]:[];var store=createStore(reducers,undefined,applyMiddleware(thunk,streams));if(subscribers.length!==0){initSubscribers([].concat(subscribers));}return store;}