mattermost-redux
Version:
Common code (API client, Redux stores, logic, utility functions) for building a Mattermost client
56 lines (55 loc) • 2.98 kB
JavaScript
;
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = configureStore;
const extension_1 = require("@redux-devtools/extension");
const redux_1 = require("redux");
const redux_thunk_1 = __importDefault(require("redux-thunk"));
const helpers_1 = require("./helpers");
const initial_state_1 = __importDefault(require("./initial_state"));
const reducer_registry_1 = __importDefault(require("./reducer_registry"));
const reducers_1 = __importDefault(require("../reducers"));
/**
* Configures and constructs the redux store. Accepts the following parameters:
* preloadedState - Any preloaded state to be applied to the store after it is initially configured.
* appReducer - An object containing any app-specific reducer functions that the client needs.
* getAppReducers - A function that returns the appReducer as defined above. Only used in development to enable hot reloading.
*/
function configureStore({ appReducers, getAppReducers, preloadedState, }) {
const baseState = {
...initial_state_1.default,
...preloadedState,
};
const composeEnhancers = (0, extension_1.composeWithDevTools)({
shouldHotReload: false,
trace: true,
traceLimit: 25,
autoPause: true,
});
const middleware = (0, redux_1.applyMiddleware)(
// @hmhealey I've added this extra argument to Thunks to store information related to the store that can't be
// part of Redux state itself. At the moment, this is so that I can attach let DataLoaders dispatch actions.
// If you want to make use of this, talk to me first since I want to know more.
redux_thunk_1.default.withExtraArgument({ loaders: {} }));
const enhancers = composeEnhancers(middleware);
const baseReducer = (0, helpers_1.createReducer)(reducers_1.default, appReducers);
const store = (0, redux_1.legacy_createStore)(baseReducer, baseState, enhancers);
reducer_registry_1.default.setChangeListener((reducers) => {
store.replaceReducer((0, helpers_1.createReducer)(reducers, reducers_1.default, appReducers));
});
if (module.hot) {
// Enable Webpack hot module replacement for reducers
module.hot.accept(() => {
const registryReducers = reducer_registry_1.default.getReducers();
const nextServiceReducers = require('../reducers').default; // eslint-disable-line global-require
const nextAppReducers = getAppReducers();
// Ensure registryReducers comes first so that stored service/app reducers are replaced by the new ones
store.replaceReducer((0, helpers_1.createReducer)(registryReducers, nextServiceReducers, nextAppReducers));
});
}
return store;
}