Skip to content

Commit 60b2119

Browse files
committed
Merge pull request #256 from gaearon/remove-thunk-middleware
Remove thunk middleware from the core
2 parents 18aceee + 0b5207f commit 60b2119

File tree

4 files changed

+27
-21
lines changed

4 files changed

+27
-21
lines changed

examples/counter/containers/App.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@ import { createStore, applyMiddleware } from 'redux';
44
import { Provider } from 'react-redux';
55
import * as reducers from '../reducers';
66

7-
const createStoreWithMiddleware = applyMiddleware()(createStore);
7+
// TODO: move into a separate project
8+
function thunk({ dispatch, getState }) {
9+
return next => action =>
10+
typeof action === 'function' ?
11+
action(dispatch, getState) :
12+
next(action);
13+
}
14+
15+
const createStoreWithMiddleware = applyMiddleware(thunk)(createStore);
816
const store = createStoreWithMiddleware(reducers);
917

1018
export default class App {

src/utils/applyMiddleware.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import compose from './compose';
22
import composeMiddleware from './composeMiddleware';
3-
import thunk from '../middleware/thunk';
43

54
/**
65
* Creates a higher-order store that applies middleware to a store's dispatch.
@@ -10,24 +9,25 @@ import thunk from '../middleware/thunk';
109
* @return {Function} A higher-order store
1110
*/
1211
export default function applyMiddleware(...middlewares) {
13-
const finalMiddlewares = middlewares.length ?
14-
middlewares :
15-
[thunk];
16-
1712
return next => (...args) => {
1813
const store = next(...args);
19-
const middleware = composeMiddleware(...finalMiddlewares);
14+
const middleware = composeMiddleware(...middlewares);
15+
16+
function dispatch(action) {
17+
const methods = {
18+
dispatch,
19+
getState: store.getState
20+
};
21+
22+
return compose(
23+
middleware(methods),
24+
store.dispatch
25+
)(action);
26+
}
2027

2128
return {
2229
...store,
23-
dispatch: function dispatch(action) {
24-
const methods = { dispatch, getState: store.getState };
25-
26-
return compose(
27-
middleware(methods),
28-
store.dispatch
29-
)(action);
30-
}
30+
dispatch
3131
};
3232
};
3333
}

test/applyMiddleware.spec.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import expect from 'expect';
22
import { createStore, applyMiddleware } from '../src/index';
33
import * as reducers from './helpers/reducers';
44
import { addTodo, addTodoAsync, addTodoIfEmpty } from './helpers/actionCreators';
5-
import thunk from '../src/middleware/thunk';
5+
import { thunk } from './helpers/middleware';
66

77
describe('applyMiddleware', () => {
88
it('wraps dispatch method with middleware', () => {
@@ -34,17 +34,15 @@ describe('applyMiddleware', () => {
3434
}
3535

3636
const spy = expect.createSpy(() => {});
37-
3837
const store = applyMiddleware(test(spy), thunk)(createStore)(reducers.todos);
3938

4039
return store.dispatch(addTodoAsync('Use Redux')).then(() => {
4140
expect(spy.calls.length).toEqual(2);
4241
});
43-
4442
});
4543

46-
it('uses thunk middleware by default', done => {
47-
const store = applyMiddleware()(createStore)(reducers.todos);
44+
it('works with thunk middleware', done => {
45+
const store = applyMiddleware(thunk)(createStore)(reducers.todos);
4846

4947
store.dispatch(addTodoIfEmpty('Hello'));
5048
expect(store.getState()).toEqual([{

src/middleware/thunk.js renamed to test/helpers/middleware.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default function thunkMiddleware({ dispatch, getState }) {
1+
export function thunk({ dispatch, getState }) {
22
return next => action =>
33
typeof action === 'function' ?
44
action(dispatch, getState) :

0 commit comments

Comments
 (0)