@@ -2,29 +2,35 @@ import produce from 'immer';
22import { combineReducers , ReducersMapObject } from 'redux' ;
33import * as T from '../types' ;
44
5- function combineReducersWithImmer ( reducers : ReducersMapObject ) {
6- const reducersWithImmer : ReducersMapObject < any , T . Action < any > > = { } ;
7- // reducer must return value because literal don't support immer
5+ export interface ImmerConfig {
6+ blacklist ?: string [ ] ;
7+ }
8+
9+ function createCombineReducersWithImmer ( blacklist : string [ ] = [ ] ) {
10+ return function ( reducers : ReducersMapObject ) {
11+ const reducersWithImmer : ReducersMapObject < any , T . Action < any > > = { } ;
12+ // reducer must return value because literal don't support immer
813
9- Object . keys ( reducers ) . forEach ( ( key ) => {
10- const reducerFn = reducers [ key ] ;
11- reducersWithImmer [ key ] = ( state , payload ) =>
12- typeof state === 'object'
13- ? produce ( state , ( draft : T . Models ) => {
14- const next = reducerFn ( draft , payload ) ;
15- if ( typeof next === 'object' ) return next ;
16- } )
17- : reducerFn ( state , payload ) ;
18- } ) ;
14+ Object . keys ( reducers ) . forEach ( ( key ) => {
15+ const reducerFn = reducers [ key ] ;
16+ reducersWithImmer [ key ] = ( state , payload ) =>
17+ typeof state === 'object' && ! blacklist . includes ( key )
18+ ? produce ( state , ( draft : T . Models ) => {
19+ const next = reducerFn ( draft , payload ) ;
20+ if ( typeof next === 'object' ) return next ;
21+ } )
22+ : reducerFn ( state , payload ) ;
23+ } ) ;
1924
20- return combineReducers ( reducersWithImmer ) ;
25+ return combineReducers ( reducersWithImmer ) ;
26+ } ;
2127}
2228
2329// icestore plugin
24- const immerPlugin = ( ) : T . Plugin => ( {
30+ const immerPlugin = ( config : ImmerConfig = { } ) : T . Plugin => ( {
2531 config : {
2632 redux : {
27- combineReducers : combineReducersWithImmer ,
33+ combineReducers : createCombineReducersWithImmer ( config . blacklist ) ,
2834 } ,
2935 } ,
3036} ) ;
0 commit comments