Skip to content

Commit e72661a

Browse files
Max Kostowjayphelps
authored andcommitted
feat(createEpicMiddleware): warn about reusing middleware
Warn about #389
1 parent 90f1928 commit e72661a

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

src/createEpicMiddleware.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ export function createEpicMiddleware(rootEpic, options = defaultOptions) {
2929
let store;
3030

3131
const epicMiddleware = _store => {
32+
if (process.env.NODE_ENV !== 'production' && store) {
33+
// https://github.com/redux-observable/redux-observable/issues/389
34+
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');
35+
}
3236
store = _store;
3337

3438
return next => {

src/utils/console.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,9 @@ export const deprecate = (typeof console === 'object' && typeof console.warn ===
88
}
99
}
1010
: () => {};
11+
12+
export const warn = (typeof console === 'object' && typeof console.warn === 'function')
13+
? msg => {
14+
console.warn(`redux-observable | WARNING: ${msg}`);
15+
}
16+
: () => {};

test/createEpicMiddleware-spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@ describe('createEpicMiddleware', () => {
2929
store.dispatch({ type: 'FIRST_ACTION_TO_TRIGGER_MIDDLEWARE' });
3030
});
3131

32+
it('should warn about reusing the epicMiddleware', () => {
33+
sinon.spy(console, 'warn');
34+
const reducer = (state = [], action) => state.concat(action);
35+
const epic = (action$, store) => action$
36+
.ofType('PING')
37+
::map(() => store.dispatch({ type: 'PONG' }))
38+
::ignoreElements();
39+
40+
const middleware = createEpicMiddleware(epic);
41+
createStore(reducer, applyMiddleware(middleware));
42+
createStore(reducer, applyMiddleware(middleware));
43+
expect(console.warn.callCount).to.equal(1);
44+
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');
45+
console.warn.restore();
46+
});
47+
3248
it('should warn about improper use of dispatch function', () => {
3349
sinon.spy(console, 'warn');
3450
const reducer = (state = [], action) => state.concat(action);

0 commit comments

Comments
 (0)