File tree 3 files changed +35
-1
lines changed
3 files changed +35
-1
lines changed Original file line number Diff line number Diff line change @@ -191,6 +191,13 @@ export default function createDva(createOpts) {
191
191
'app.start: extraReducers is conflict with other reducers' ,
192
192
) ;
193
193
194
+ // extra enhancers
195
+ const extraEnhancers = plugin . get ( 'extraEnhancers' ) ;
196
+ invariant (
197
+ Array . isArray ( extraEnhancers ) ,
198
+ 'app.start: extraEnhancers should be array' ,
199
+ ) ;
200
+
194
201
// create store
195
202
const extraMiddlewares = plugin . get ( 'onAction' ) ;
196
203
const reducerEnhancer = plugin . get ( 'onReducer' ) ;
@@ -206,6 +213,7 @@ export default function createDva(createOpts) {
206
213
const enhancers = [
207
214
applyMiddleware ( ...middlewares ) ,
208
215
devtools ( ) ,
216
+ ...extraEnhancers ,
209
217
] ;
210
218
const store = this . _store = createStore (
211
219
createReducer ( ) ,
Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ class Plugin {
12
12
onReducer : [ ] ,
13
13
onEffect : [ ] ,
14
14
extraReducers : [ ] ,
15
+ extraEnhancers : [ ] ,
15
16
} ;
16
17
}
17
18
@@ -21,7 +22,11 @@ class Plugin {
21
22
for ( const key in plugin ) {
22
23
if ( Object . prototype . hasOwnProperty . call ( plugin , key ) ) {
23
24
invariant ( hooks [ key ] , `plugin.use: unknown plugin property: ${ key } ` ) ;
24
- hooks [ key ] . push ( plugin [ key ] ) ;
25
+ if ( key === 'extraEnhancers' ) {
26
+ hooks [ key ] = plugin [ key ] ;
27
+ } else {
28
+ hooks [ key ] . push ( plugin [ key ] ) ;
29
+ }
25
30
}
26
31
}
27
32
}
Original file line number Diff line number Diff line change @@ -114,4 +114,25 @@ describe('dva', () => {
114
114
app . _store . dispatch ( { type : 'test' } ) ;
115
115
expect ( count ) . toEqual ( 3 ) ;
116
116
} ) ;
117
+
118
+ it ( 'opts.extraEnhancers' , ( ) => {
119
+ let count = 0 ;
120
+ const countEnhancer = storeCreator => ( reducer , preloadedState , enhancer ) => {
121
+ const store = storeCreator ( reducer , preloadedState , enhancer ) ;
122
+ const oldDispatch = store . dispatch ;
123
+ store . dispatch = ( action ) => {
124
+ count += 1 ;
125
+ oldDispatch ( action ) ;
126
+ } ;
127
+ return store ;
128
+ } ;
129
+ const app = dva ( {
130
+ extraEnhancers : [ countEnhancer ] ,
131
+ } ) ;
132
+ app . router ( ( ) => 1 ) ;
133
+ app . start ( ) ;
134
+
135
+ // @@router /LOCATION_CHANGE
136
+ expect ( count ) . toEqual ( 1 ) ;
137
+ } ) ;
117
138
} ) ;
You can’t perform that action at this time.
0 commit comments