signal-desktop/ts/state/createStore.ts

59 lines
1.5 KiB
TypeScript
Raw Normal View History

// Copyright 2019-2022 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
/* eslint-disable no-console */
import type { Store } from 'redux';
import { applyMiddleware, createStore as reduxCreateStore } from 'redux';
2019-01-14 21:49:58 +00:00
import promise from 'redux-promise-middleware';
import thunk from 'redux-thunk';
2019-01-14 21:49:58 +00:00
import { createLogger } from 'redux-logger';
import type { StateType } from './reducer';
import { reducer } from './reducer';
2021-08-18 20:08:14 +00:00
import { dispatchItemsMiddleware } from '../shims/dispatchItemsMiddleware';
declare global {
// We want to extend `window`'s properties, so we need an interface.
2022-06-03 21:07:51 +00:00
// eslint-disable-next-line no-restricted-syntax
interface Console {
_log: Console['log'];
}
}
2019-01-14 21:49:58 +00:00
const env = window.getEnvironment();
// So Redux logging doesn't go to disk, and so we can get colors/styles
const directConsole = {
log: console._log,
groupCollapsed: console.groupCollapsed,
group: console.group,
groupEnd: console.groupEnd,
warn: console.warn,
error: console.error,
};
const logger = createLogger({
logger: directConsole,
predicate: (_getState, action) => {
if (action.type === 'network/CHECK_NETWORK_STATUS') {
return false;
}
return true;
},
2019-01-14 21:49:58 +00:00
});
const middlewareList = [
promise,
thunk,
2021-08-18 20:08:14 +00:00
dispatchItemsMiddleware,
...(env === 'production' ? [] : [logger]),
];
2019-01-14 21:49:58 +00:00
const enhancer = applyMiddleware(...middlewareList);
2019-01-14 21:49:58 +00:00
export const createStore = (
initialState: Readonly<StateType>
): Store<StateType> => reduxCreateStore(reducer, initialState, enhancer);