import { Plugin , Models } from '@rematch/core' ; import produce from 'immer' ; import Redux from 'redux' ; export type ImmerPluginConfig = { whitelist ? : string [ ] ; blacklist ? : string [ ] ; } ; function wrapReducerWithImmer ( reducer ) { return ( state , payload ) : any => { if ( state === undefined ) return reducer ( state , payload ) ; return produce ( state , ( draft ) => reducer ( draft , payload ) ) ; } ; } const immerPlugin = < TModels extends Models < TModels > , TExtraModels extends Models < TModels > = Record < string , never > > ( config ? ) : Plugin < TModels , TExtraModels > => ( { onReducer ( reducer , model ) { if ( ! config || ( ! config . whitelist && ! config . blacklist ) || ( config . whitelist && config . whitelist . includes ( model ) ) || ( config . blacklist && ! config . blacklist . includes ( model ) ) ) { return wrapReducerWithImmer ( reducer ) ; } return undefined ; } } ) ; export default immerPlugin ;