// src/store/index.js import { configureStore, combineReducers } from '@reduxjs/toolkit'; import communityDataReducer from './slices/communityDataSlice'; // ⚡ PostHog 延迟加载:移除同步导入,首屏减少 ~180KB // import posthogReducer from './slices/posthogSlice'; // import posthogMiddleware from './middleware/posthogMiddleware'; import industryReducer from './slices/industrySlice'; import stockReducer from './slices/stockSlice'; import authModalReducer from './slices/authModalSlice'; import subscriptionReducer from './slices/subscriptionSlice'; import deviceReducer from './slices/deviceSlice'; // ✅ 设备检测状态管理 import { eventsApi } from './api/eventsApi'; // ✅ RTK Query API // ⚡ 基础 reducers(首屏必需) const staticReducers = { communityData: communityDataReducer, industry: industryReducer, // ✅ 行业分类数据管理 stock: stockReducer, // ✅ 股票和事件数据管理 authModal: authModalReducer, // ✅ 认证弹窗状态管理 subscription: subscriptionReducer, // ✅ 订阅信息状态管理 device: deviceReducer, // ✅ 设备检测状态管理(移动端/桌面端) [eventsApi.reducerPath]: eventsApi.reducer, // ✅ RTK Query 事件 API }; // ⚡ 动态 reducers 注册表 const asyncReducers = {}; // ⚡ 创建根 reducer 的工厂函数 const createRootReducer = () => combineReducers({ ...staticReducers, ...asyncReducers, }); export const store = configureStore({ reducer: createRootReducer(), middleware: (getDefaultMiddleware) => getDefaultMiddleware({ serializableCheck: { // 忽略这些 action types 的序列化检查 ignoredActions: [ 'communityData/fetchPopularKeywords/fulfilled', 'communityData/fetchHotEvents/fulfilled', 'posthog/trackEvent/fulfilled', // ✅ PostHog 事件追踪(延迟加载后仍需) 'stock/fetchEventStocks/fulfilled', 'stock/fetchStockQuotes/fulfilled', ], }, }) // ⚡ PostHog 中间件延迟加载,首屏不再需要 .concat(eventsApi.middleware), // ✅ RTK Query 中间件(自动缓存、去重、重试) }); /** * ⚡ 动态注入 reducer(用于延迟加载模块) * @param {string} key - reducer 的键名 * @param {Function} reducer - reducer 函数 */ export const injectReducer = (key, reducer) => { if (asyncReducers[key]) { return; // 已注入,避免重复 } asyncReducers[key] = reducer; store.replaceReducer(createRootReducer()); }; /** * @typedef {typeof store.dispatch} AppDispatch * @typedef {ReturnType} RootState */ export default store;