-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update middleware api #213
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import compose from './compose'; | ||
import composeMiddleware from './composeMiddleware'; | ||
import thunk from '../middleware/thunk'; | ||
|
||
/** | ||
* Creates a higher-order store that applies middleware to a store's dispatch. | ||
* Because middleware is potentially asynchronous, this should be the first | ||
* higher-order store in the composition chain. | ||
* @param {...Function} ...middlewares | ||
* @return {Function} A higher-order store | ||
*/ | ||
export default function applyMiddleware(...middlewares) { | ||
const finalMiddlewares = middlewares.length ? | ||
middlewares : | ||
[thunk]; | ||
|
||
return next => (...args) => { | ||
const store = next(...args); | ||
const methods = { | ||
dispatch: store.dispatch, | ||
getState: store.getState | ||
}; | ||
return { | ||
...store, | ||
dispatch: compose( | ||
composeMiddleware(...finalMiddlewares)(methods), | ||
store.dispatch | ||
) | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* Composes functions from left to right | ||
* @param {...Function} funcs - Functions to compose | ||
* @return {Function} | ||
*/ | ||
export default function compose(...funcs) { | ||
return funcs.reduceRight((composed, f) => f(composed)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
import compose from './compose'; | ||
|
||
/** | ||
* Compose middleware from left to right | ||
* @param {...Function} middlewares | ||
* @return {Function} | ||
*/ | ||
export default function composeMiddleware(...middlewares) { | ||
return middlewares.reduceRight((composed, m) => m(composed)); | ||
return methods => next => compose(...middlewares.map(m => m(methods)), next); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import expect from 'expect'; | ||
import { createStore, applyMiddleware } from '../src/index'; | ||
import * as reducers from './helpers/reducers'; | ||
import { addTodo, addTodoAsync, addTodoIfEmpty } from './helpers/actionCreators'; | ||
import thunk from '../src/middleware/thunk'; | ||
|
||
describe('applyMiddleware', () => { | ||
it('wraps dispatch method with middleware', () => { | ||
function test(spy) { | ||
return methods => next => action => { | ||
spy(methods); | ||
return next(action); | ||
}; | ||
} | ||
|
||
const spy = expect.createSpy(() => {}); | ||
const store = applyMiddleware(test(spy), thunk)(createStore)(reducers.todos); | ||
store.dispatch(addTodo('Use Redux')); | ||
|
||
expect(Object.keys(spy.calls[0].arguments[0])).toEqual([ | ||
'dispatch', | ||
'getState' | ||
]); | ||
expect(store.getState()).toEqual([ { id: 1, text: 'Use Redux' } ]); | ||
}); | ||
|
||
it('uses thunk middleware by default', done => { | ||
const store = applyMiddleware()(createStore)(reducers.todos); | ||
|
||
store.dispatch(addTodoIfEmpty('Hello')); | ||
expect(store.getState()).toEqual([{ | ||
id: 1, | ||
text: 'Hello' | ||
}]); | ||
|
||
store.dispatch(addTodoIfEmpty('Hello')); | ||
expect(store.getState()).toEqual([{ | ||
id: 1, | ||
text: 'Hello' | ||
}]); | ||
|
||
store.dispatch(addTodo('World')); | ||
expect(store.getState()).toEqual([{ | ||
id: 1, | ||
text: 'Hello' | ||
}, { | ||
id: 2, | ||
text: 'World' | ||
}]); | ||
|
||
store.dispatch(addTodoAsync('Maybe')).then(() => { | ||
expect(store.getState()).toEqual([{ | ||
id: 1, | ||
text: 'Hello' | ||
}, { | ||
id: 2, | ||
text: 'World' | ||
}, { | ||
id: 3, | ||
text: 'Maybe' | ||
}]); | ||
done(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import expect from 'expect'; | ||
import { compose } from '../src'; | ||
|
||
describe('Utils', () => { | ||
describe('compose', () => { | ||
it('composes functions from left to right', () => { | ||
const a = next => x => next(x + 'a'); | ||
const b = next => x => next(x + 'b'); | ||
const c = next => x => next(x + 'c'); | ||
const final = x => x; | ||
|
||
expect(compose(a, b, c, final)('')).toBe('abc'); | ||
expect(compose(b, c, a, final)('')).toBe('bca'); | ||
expect(compose(c, a, b, final)('')).toBe('cab'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we consider having the default thunk middleware send the same
methods
type object toaction
instead of splitting them into separate parameters like this? i.e.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about that, too, but 9 times out of 10 a thunk just needs
dispatch()
, and single argument form is easier.