Skip to content

Commit

Permalink
feat(createEpicMiddleware): warn about reusing middleware
Browse files Browse the repository at this point in the history
Warn about #389
  • Loading branch information
Max Kostow authored and jayphelps committed Mar 2, 2018
1 parent 90f1928 commit e72661a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/createEpicMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export function createEpicMiddleware(rootEpic, options = defaultOptions) {
let store;

const epicMiddleware = _store => {
if (process.env.NODE_ENV !== 'production' && store) {
// https://github.com/redux-observable/redux-observable/issues/389
require('./utils/console').warn('this middleware is already associated with a store. createEpicMiddleware should be called for every store.\n\n See https://goo.gl/2GQ7Da');
}
store = _store;

return next => {
Expand Down
6 changes: 6 additions & 0 deletions src/utils/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ export const deprecate = (typeof console === 'object' && typeof console.warn ===
}
}
: () => {};

export const warn = (typeof console === 'object' && typeof console.warn === 'function')
? msg => {
console.warn(`redux-observable | WARNING: ${msg}`);
}
: () => {};
16 changes: 16 additions & 0 deletions test/createEpicMiddleware-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ describe('createEpicMiddleware', () => {
store.dispatch({ type: 'FIRST_ACTION_TO_TRIGGER_MIDDLEWARE' });
});

it('should warn about reusing the epicMiddleware', () => {
sinon.spy(console, 'warn');
const reducer = (state = [], action) => state.concat(action);
const epic = (action$, store) => action$
.ofType('PING')
::map(() => store.dispatch({ type: 'PONG' }))
::ignoreElements();

const middleware = createEpicMiddleware(epic);
createStore(reducer, applyMiddleware(middleware));
createStore(reducer, applyMiddleware(middleware));
expect(console.warn.callCount).to.equal(1);
expect(console.warn.getCall(0).args[0]).to.equal('redux-observable | WARNING: this middleware is already associated with a store. createEpicMiddleware should be called for every store.\n\n See https://goo.gl/2GQ7Da');
console.warn.restore();
});

it('should warn about improper use of dispatch function', () => {
sinon.spy(console, 'warn');
const reducer = (state = [], action) => state.concat(action);
Expand Down

0 comments on commit e72661a

Please sign in to comment.