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

Pass dispatch to higher-order middleware and re-implement thunk #120

Closed
wants to merge 1 commit into from
Closed
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/Redux.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default class Redux {
// A shortcut notation to use the default dispatcher
dispatcher = createDispatcher(
composeStores(dispatcher),
getState => [thunkMiddleware(getState)]
({ getState, dispatch }) => [thunkMiddleware(getState, dispatch)]
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/createDispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function createDispatcher(store, middlewares = []) {
}

const finalMiddlewares = typeof middlewares === 'function' ?
middlewares(getState) :
middlewares({ getState, dispatch }) :
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't the idea to pass the full dispatch middleware chain instead of the default fn?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

D'oh, you're right. I'll update.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Actually I'll update it if/when we merge #119 since there will be conflicts.

middlewares;

return composeMiddleware(...finalMiddlewares, dispatch);
Expand Down
14 changes: 5 additions & 9 deletions src/middleware/thunk.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
export default function thunkMiddleware(getState) {
return (next) => {
const recurse = (action) =>
typeof action === 'function' ?
action(recurse, getState) :
next(action);

return recurse;
};
export default function thunkMiddleware(getState, dispatch) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, so if the thunk ain't the first one, dispatch will re-start the chain right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yep!

return next => action =>
typeof action === 'function' ?
action(dispatch, getState) :
next(action);
}
2 changes: 1 addition & 1 deletion test/createDispatcher.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('createDispatcher', () => {
const dispatcher = createDispatcher(
composeStores({ todoStore }),
// we need this middleware to handle async actions
getState => [thunkMiddleware(getState)]
({ getState, dispatch }) => [thunkMiddleware(getState, dispatch)]
);

expect(dispatcher).toBeA('function');
Expand Down