import produce , { enableES5 } from 'immer' ; import { combineReducers , ReducersMapObject } from 'redux' ; import * as T from '../types' ; enableES5 ( ) ; export interface ImmerConfig { blacklist ? : string [ ] ; } function createCombineReducersWithImmer ( blacklist = [ ] ) { return function ( reducers ) { const reducersWithImmer = { } ; Object . keys ( reducers ) . forEach ( ( key ) => { const reducerFn = reducers [ key ] ; reducersWithImmer [ key ] = ( state , payload ) => typeof state === 'object' && ! blacklist . includes ( key ) ? produce ( state , ( draft ) => { const next = reducerFn ( draft , payload ) ; if ( typeof next === 'object' ) return next ; } ) : reducerFn ( state , payload ) ; } ) ; return combineReducers ( reducersWithImmer ) ; } ; } const immerPlugin = ( config = { } ) : T . Plugin => ( { config : { redux : { combineReducers : createCombineReducersWithImmer ( config . blacklist ) , } , } , } ) ; export default immerPlugin ;