@@ -59,18 +59,17 @@ function createModulesReducer() {
59
59
case constants . MODULE_INIT : {
60
60
const name = action . payload . module ;
61
61
console . debug ( `module ${ name } initialized` ) ;
62
- let changed = false ;
63
62
const reducer = modulesReducers [ name ] ;
64
- if ( reducer ) {
65
- try {
66
- state [ name ] = reducer ( state [ name ] , action ) ;
67
- changed = true ;
68
- } catch ( error ) {
69
- warning ( `module ${ name } reducer failed to reduce on action ${ action . type } ` , error ) ;
70
- }
63
+ if ( ! reducer ) {
64
+ return state ;
71
65
}
72
- if ( changed ) {
73
- return { ...state } ;
66
+ try {
67
+ return {
68
+ ...state ,
69
+ [ name ] : reducer ( state [ name ] , action ) ,
70
+ } ;
71
+ } catch ( error ) {
72
+ warning ( `module ${ name } reducer failed to reduce on action ${ action . type } ` , error ) ;
74
73
}
75
74
return state ;
76
75
}
@@ -82,34 +81,36 @@ function createModulesReducer() {
82
81
}
83
82
case constants . MODULE_UNLOADED : {
84
83
const name = action . payload . module ;
85
- let changed = false ;
86
- if ( state [ name ] ) {
84
+ let changed = name in state ;
85
+ if ( changed ) {
87
86
console . debug ( `module ${ name } evicting redux state` ) ;
88
- delete state [ name ] ;
89
- changed = true ;
90
87
}
91
88
console . debug ( `module ${ name } unloaded` ) ;
92
89
console . info ( `- module ${ name } ` ) ;
93
90
if ( changed ) {
94
- return { ...state } ;
91
+ const nextState = { ...state } ;
92
+ delete nextState [ name ] ;
93
+ return nextState ;
95
94
}
96
95
return state ;
97
96
}
98
97
default : {
99
- let changed = false ;
98
+ let nextState = null ;
100
99
for ( const [ name , reducer ] of modulesReducers ) {
101
100
try {
102
- const nextState = reducer ( state [ name ] , action ) ;
103
- if ( state [ name ] !== nextState ) {
104
- state [ name ] = nextState ;
105
- changed = true ;
101
+ const fragment = reducer ( state [ name ] , action ) ;
102
+ if ( state [ name ] !== fragment ) {
103
+ if ( ! nextState ) {
104
+ nextState = { ...state } ;
105
+ }
106
+ nextState [ name ] = fragment ;
106
107
}
107
108
} catch ( error ) {
108
109
warning ( `module ${ name } reducer failed to reduce on action ${ action . type } ` , error ) ;
109
110
}
110
111
}
111
- if ( changed ) {
112
- return { ... state } ;
112
+ if ( nextState ) {
113
+ return nextState ;
113
114
}
114
115
return state ;
115
116
}
0 commit comments