amis-rpc-design/src/store.ts
2023-10-07 19:42:30 +08:00

54 lines
1.5 KiB
TypeScript

import { configureStore } from '@reduxjs/toolkit';
import createSagaMiddleware from 'redux-saga';
import sagaGenerator from './sagas';
import user from './features/user';
import cache from './features/cache';
import tokenReducer from './features/token-slice';
import modalReducer from './features/modal-slice';
import themeReducer from './features/modal-slice';
const sagaMiddleware = createSagaMiddleware();
const tokenMiddleware = (store: any) => {
return (next: Function) => {
return (action: any) => {
if (action?.meta?.token && !localStorage.getItem('Z-AUTHORIZATION')) {
return next({type: 'token/showToken'});
}
return next(action);
}
}
}
const middleware = [tokenMiddleware, sagaMiddleware];
// export const dispatchEnhancer = (createStore) => {
// return (rootReducer, preloadedState) => {
// const store = createStore(rootReducer, preloadedState)
// function dispatch(action) {
// const result = store.dispatch(action)
// console.log('发送了' + action.type + '事件');
// return result
// }
// return { ...store, dispatch }
// }
// }
const store = configureStore({
reducer: {
theme: themeReducer,
token: tokenReducer,
modal: modalReducer,
user,
cache,
},
// enhancers: [dispatchEnhancer],
middleware
});
sagaMiddleware.run(sagaGenerator);
export type AppDispatch = typeof store.dispatch;
export type RootState = ReturnType<typeof store.getState>;
export default store;