Files
jellyfin-audio-player/src/store/index.ts

69 lines
2.3 KiB
TypeScript
Raw Normal View History

2020-06-17 14:58:04 +02:00
import { configureStore, getDefaultMiddleware, combineReducers } from '@reduxjs/toolkit';
2021-04-24 14:50:43 +02:00
import { useSelector, TypedUseSelectorHook, useDispatch } from 'react-redux';
2022-06-12 21:51:20 +02:00
import AsyncStorage from '@react-native-async-storage/async-storage';
2022-01-03 09:07:30 +01:00
import { persistStore, persistReducer, PersistConfig, createMigrate } from 'redux-persist';
2020-08-09 17:49:36 +02:00
import autoMergeLevel2 from 'redux-persist/es/stateReconciler/autoMergeLevel2';
2020-06-17 14:58:04 +02:00
2022-01-03 09:07:30 +01:00
import settings from './settings';
import music, { initialState as musicInitialState } from './music';
import downloads, { initialState as downloadsInitialState } from './downloads';
import { PersistState } from 'redux-persist/es/types';
const persistConfig: PersistConfig<Omit<AppState, '_persist'>> = {
2020-06-17 14:58:04 +02:00
key: 'root',
storage: AsyncStorage,
version: 2,
2022-01-03 09:07:30 +01:00
stateReconciler: autoMergeLevel2,
migrate: createMigrate({
// @ts-expect-error migrations are poorly typed
1: (state: AppState & PersistState) => {
return {
...state,
settings: state.settings,
downloads: downloadsInitialState,
music: musicInitialState
};
},
// @ts-expect-error migrations are poorly typed
2: (state: AppState) => {
return {
...state,
downloads: {
...state.downloads,
queued: []
}
};
2022-01-03 09:07:30 +01:00
}
})
2020-06-17 14:58:04 +02:00
};
const reducers = combineReducers({
settings,
music: music.reducer,
2022-01-02 02:28:52 +01:00
downloads: downloads.reducer,
2020-06-17 14:58:04 +02:00
});
const persistedReducer = persistReducer(persistConfig, reducers);
2022-01-03 09:07:30 +01:00
const middlewares = [];
if (__DEV__) {
middlewares.push(require('redux-flipper').default());
}
2020-06-17 14:58:04 +02:00
const store = configureStore({
reducer: persistedReducer,
2020-06-17 15:09:19 +02:00
middleware: getDefaultMiddleware({ serializableCheck: false, immutableCheck: false }).concat(
2022-01-02 02:28:52 +01:00
// logger,
2022-01-03 09:07:30 +01:00
...middlewares,
2020-06-17 15:09:19 +02:00
),
2020-06-17 14:58:04 +02:00
});
export type AppState = ReturnType<typeof reducers> & { _persist: PersistState };
2020-06-17 14:58:04 +02:00
export type AppDispatch = typeof store.dispatch;
export type AsyncThunkAPI = { state: AppState, dispatch: AppDispatch };
export const useTypedSelector: TypedUseSelectorHook<AppState> = useSelector;
2021-04-24 14:50:43 +02:00
export const useAppDispatch = () => useDispatch<AppDispatch>();
2020-06-17 14:58:04 +02:00
export const persistedStore = persistStore(store);
export default store;