Skip to content

Commit

Permalink
suport redux enhancer with extraEnhancers
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc committed Dec 27, 2016
1 parent 4102f34 commit 5fa07b6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/createDva.js
Expand Up @@ -191,6 +191,13 @@ export default function createDva(createOpts) {
'app.start: extraReducers is conflict with other reducers',
);

// extra enhancers
const extraEnhancers = plugin.get('extraEnhancers');
invariant(
Array.isArray(extraEnhancers),
'app.start: extraEnhancers should be array',
);

// create store
const extraMiddlewares = plugin.get('onAction');
const reducerEnhancer = plugin.get('onReducer');
Expand All @@ -206,6 +213,7 @@ export default function createDva(createOpts) {
const enhancers = [
applyMiddleware(...middlewares),
devtools(),
...extraEnhancers,
];
const store = this._store = createStore(
createReducer(),
Expand Down
7 changes: 6 additions & 1 deletion src/plugin.js
Expand Up @@ -12,6 +12,7 @@ class Plugin {
onReducer: [],
onEffect: [],
extraReducers: [],
extraEnhancers: [],
};
}

Expand All @@ -21,7 +22,11 @@ class Plugin {
for (const key in plugin) {
if (Object.prototype.hasOwnProperty.call(plugin, key)) {
invariant(hooks[key], `plugin.use: unknown plugin property: ${key}`);
hooks[key].push(plugin[key]);
if (key === 'extraEnhancers') {
hooks[key] = plugin[key];
} else {
hooks[key].push(plugin[key]);
}
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions test/dva-test.js
Expand Up @@ -114,4 +114,25 @@ describe('dva', () => {
app._store.dispatch({ type: 'test' });
expect(count).toEqual(3);
});

it('opts.extraEnhancers', () => {
let count = 0;
const countEnhancer = storeCreator => (reducer, preloadedState, enhancer) => {
const store = storeCreator(reducer, preloadedState, enhancer);
const oldDispatch = store.dispatch;
store.dispatch = (action) => {
count += 1;
oldDispatch(action);
};
return store;
};
const app = dva({
extraEnhancers: [countEnhancer],
});
app.router(() => 1);
app.start();

// @@router/LOCATION_CHANGE
expect(count).toEqual(1);
});
});

0 comments on commit 5fa07b6

Please sign in to comment.