import { Plugin , Models } from '@rematch/core' ; import { persistReducer , persistStore , PersistConfig , Persistor , PersistorOptions , } from 'redux-persist' ; import Redux from 'redux' ; let persistor ; export const getPersistor = ( ) : Persistor => persistor ; export type NestedPersist < M extends Models < M > > = { [ modelKey in keyof M ] ? : PersistConfig < M [ modelKey ] [ 'state' ] > ; } ; export type PersistorStoreOptionsNotExposed = { manualPersist ? : boolean ; } ; const persistPlugin = < S , TModels extends Models < TModels > , TExtraModels extends Models < TModels > = Record < string , never > > ( persistConfig , nestedPersistConfig = { } , persistStoreConfig ? , callback ? ) : Plugin < TModels , TExtraModels > => { if ( ! persistConfig ) { throw new Error ( 'persist plugin is missing config object' ) ; } return { onReducer ( reducer , modelName ) { const reducerConfig = nestedPersistConfig [ modelName ] ; if ( reducerConfig ) { return persistReducer ( reducerConfig , reducer ) ; } return undefined ; } , onRootReducer ( rootReducer ) { return persistReducer ( persistConfig , rootReducer ) ; } , onStoreCreated ( store ) { persistor = persistStore ( store , persistStoreConfig , callback ) ; } } ; } ; export default persistPlugin ;