Skip to content
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

Make middleware API dispatch pass through all call arguments #2560

Merged
merged 1 commit into from
Aug 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/applyMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default function applyMiddleware(...middlewares) {

const middlewareAPI = {
getState: store.getState,
dispatch: (action) => dispatch(action)
dispatch: (...args) => dispatch(...args)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just return dispatch?

const middlewareAPI = {
  getState: store.getState,
  dispatch,
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey! Can someone have an explanation for this change?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FranciscoMSM This particular change is to allow middleware to change the arguments of dispatch and pass that through.

The reason for the extra function is to remove direct access to dispatch so middleware doesn't try to attach properties or other hidden metadata to it. Each middleware gets its own unique copy of dispatch this way, and there is less chance of interference between middlewares.

}
chain = middlewares.map(middleware => middleware(middlewareAPI))
dispatch = compose(...chain)(store.dispatch)
Expand Down
20 changes: 20 additions & 0 deletions test/applyMiddleware.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,26 @@ describe('applyMiddleware', () => {
})
})

it('passes through all arguments of dispatch calls from within middleware', () => {
const spy = jest.fn()
const testCallArgs = ['test']
function multiArgMiddleware() {
return next => (action, callArgs) => {
if (Array.isArray(callArgs)) {
return action(...callArgs)
}
return next(action)
}
}
function dummyMiddleware({ dispatch }) {
return next => action => dispatch(action, testCallArgs)
}

const store = createStore(reducers.todos, applyMiddleware(multiArgMiddleware, dummyMiddleware))
store.dispatch(spy)
expect(spy.mock.calls[0]).toEqual(testCallArgs)
})

it('keeps unwrapped dispatch available while middleware is initializing', () => {
// This is documenting the existing behavior in Redux 3.x.
// We plan to forbid this in Redux 4.x.
Expand Down