Update middleware api #213
Merged
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.
This PR updates the middleware API in several ways.
All middleware is now smart middleware
The API becomes much simpler if we just assume that all middleware is "smart" middleware. I know previously I argued against something like this:
because it's fragile, hard to compose, and awkward to use from the call site. I also figured most middleware wouldn't need either
dispatch()
orgetState()
. But as we discovered with the thunk middleware, any asynchronous middleware is going to needdispatch()
so that it can dispatch from the beginning of the chain. So let's just make all middleware "smart" like this:where
methods
is a subset of the store, currently{ dispatch, getState }
. Now it's easy to compose using just function composition. And if users have already written their own "dumb" middleware, all they need to do to update is wrap it in one more function:applyMiddleware(...middlewares)
Now that all middleware is smart middleware, we don't have to deal with this nonsense. We get a nice, simple API for applying middleware:
applyMiddleware()
is a higher-order store, just like @gaearon's devtools (I assume... haven't seen the final version yet.). So, it composes!Caveat:
thunkMiddleware()
is no longer built-in tocreateStore()
This is because middleware needs to come before any other higher-order stores, since middleware has the potential to be asynchronous.
However! It is still the default when using
applyMiddleware()
. It's additional step versus today, but I imagine most users will be usingapplyMiddleware
, anyway:Updated
composeMiddleware()
I updated
composeMiddleware()
to support the new signature. In the process, I also discovered a bug with the previous implementation where it only works if dispatch is the last argument. It did not combine multiple middlewares into a single middlewares as advertised; it only combined multiple middlewares with dispatch to create a new dispatch.The reason is because it was simply doing function composition, where
compose(a, b, c)
becomesa(b(c))
. But this isn't right, because thisis not the same this
Which means previously we couldn't do this, as advertised in the docs (and by, ya know, the name of the function):
I fixed the issue and updated the test. We'll need to update the docs in master, too, as they're currently incorrect.
I've renamed the previous method to just
compose()
, since it's still useful elsewhere.