Skip to content

Commit 5fa07b6

Browse files
committed
suport redux enhancer with extraEnhancers
1 parent 4102f34 commit 5fa07b6

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

src/createDva.js

+8
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,13 @@ export default function createDva(createOpts) {
191191
'app.start: extraReducers is conflict with other reducers',
192192
);
193193

194+
// extra enhancers
195+
const extraEnhancers = plugin.get('extraEnhancers');
196+
invariant(
197+
Array.isArray(extraEnhancers),
198+
'app.start: extraEnhancers should be array',
199+
);
200+
194201
// create store
195202
const extraMiddlewares = plugin.get('onAction');
196203
const reducerEnhancer = plugin.get('onReducer');
@@ -206,6 +213,7 @@ export default function createDva(createOpts) {
206213
const enhancers = [
207214
applyMiddleware(...middlewares),
208215
devtools(),
216+
...extraEnhancers,
209217
];
210218
const store = this._store = createStore(
211219
createReducer(),

src/plugin.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Plugin {
1212
onReducer: [],
1313
onEffect: [],
1414
extraReducers: [],
15+
extraEnhancers: [],
1516
};
1617
}
1718

@@ -21,7 +22,11 @@ class Plugin {
2122
for (const key in plugin) {
2223
if (Object.prototype.hasOwnProperty.call(plugin, key)) {
2324
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+
}
2530
}
2631
}
2732
}

test/dva-test.js

+21
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,25 @@ describe('dva', () => {
114114
app._store.dispatch({ type: 'test' });
115115
expect(count).toEqual(3);
116116
});
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+
});
117138
});

0 commit comments

Comments
 (0)