1 import type { TypedStartListening } from '@reduxjs/toolkit';
2 import { configureStore, createListenerMiddleware } from '@reduxjs/toolkit';
4 import { start } from './listeners';
5 import { rootReducer } from './rootReducer';
6 import { type WalletThunkArguments, extraThunkArguments } from './thunk';
8 export type WalletState = ReturnType<typeof rootReducer>;
10 export const setupStore = () => {
11 const listenerMiddleware = createListenerMiddleware({ extra: extraThunkArguments });
13 const store = configureStore({
15 middleware: (getDefaultMiddleware) =>
16 getDefaultMiddleware({
17 serializableCheck: false,
18 thunk: { extraArgument: extraThunkArguments },
19 }).prepend(listenerMiddleware.middleware),
22 const startListening = listenerMiddleware.startListening as AppStartListening;
23 start(startListening);
25 if (process.env.NODE_ENV !== 'production' && module.hot) {
26 module.hot.accept('./rootReducer', () => store.replaceReducer(rootReducer));
27 module.hot.accept('./listeners', () => {
28 listenerMiddleware.clearListeners();
29 start(startListening);
33 return Object.assign(store, {
34 unsubscribe: () => {},
38 export const extendStore = (newThunkArguments: Partial<WalletThunkArguments>) => {
39 Object.assign(extraThunkArguments, newThunkArguments);
42 export type WalletStore = ReturnType<typeof setupStore>;
43 export type WalletDispatch = WalletStore['dispatch'];
44 type ExtraArgument = typeof extraThunkArguments;
45 export type WalletThunkExtra = {
47 dispatch: WalletDispatch;
51 export type AppStartListening = TypedStartListening<WalletState, WalletDispatch, ExtraArgument>;