Update all non-major dependencies
[ProtonMail-WebClient.git] / applications / calendar / src / app / store / store.ts
blob6f7b6a157b56ce7cd8c4889443363e050f794141
1 import type { TypedStartListening } from '@reduxjs/toolkit';
2 import { configureStore, createListenerMiddleware } from '@reduxjs/toolkit';
4 import { getPersistedState } from '@proton/redux-shared-store/persist';
5 import { ignoredActions, ignoredPaths } from '@proton/redux-shared-store/sharedSerializable';
7 import { start } from './listener';
8 import { type CalendarState, persistReducer, rootReducer } from './rootReducer';
9 import { type CalendarThunkArguments, extraThunkArguments } from './thunk';
11 export type { CalendarState };
13 export const setupStore = ({
14     preloadedState,
15     persist,
16 }: {
17     preloadedState?: Partial<CalendarState>;
18     persist?: Boolean;
19 }) => {
20     const listenerMiddleware = createListenerMiddleware({ extra: extraThunkArguments });
22     const store = configureStore({
23         preloadedState,
24         reducer: rootReducer,
25         devTools: process.env.NODE_ENV !== 'production',
26         middleware: (getDefaultMiddleware) =>
27             getDefaultMiddleware({
28                 serializableCheck: {
29                     ignoredActions: [...ignoredActions],
30                     ignoredPaths: [...ignoredPaths],
31                 },
32                 thunk: { extraArgument: extraThunkArguments },
33             }).prepend(listenerMiddleware.middleware),
34     });
36     const startListening = listenerMiddleware.startListening as AppStartListening;
37     const getCalendarPersistedState = persist
38         ? (state: CalendarState) => getPersistedState(state, persistReducer)
39         : undefined;
40     start({ startListening, persistTransformer: getCalendarPersistedState });
42     if (process.env.NODE_ENV !== 'production' && module.hot) {
43         module.hot.accept('./rootReducer', () => store.replaceReducer(rootReducer));
44         module.hot.accept('./listener', () => {
45             listenerMiddleware.clearListeners();
46             start({ startListening, persistTransformer: getCalendarPersistedState });
47         });
48     }
50     return Object.assign(store, {
51         unsubscribe: () => {
52             listenerMiddleware.clearListeners();
53         },
54     });
57 export const extendStore = (newThunkArguments: Partial<CalendarThunkArguments>) => {
58     Object.assign(extraThunkArguments, newThunkArguments);
61 export type CalendarStore = ReturnType<typeof setupStore>;
62 export type CalendarDispatch = CalendarStore['dispatch'];
63 type ExtraArgument = typeof extraThunkArguments;
64 export type CalendarThunkExtra = {
65     state: CalendarState;
66     dispatch: CalendarDispatch;
67     extra: ExtraArgument;
70 export type AppStartListening = TypedStartListening<CalendarState, CalendarDispatch, ExtraArgument>;