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

Introduce new middleware API #29

Merged
merged 7 commits into from
May 21, 2018
Merged

Introduce new middleware API #29

merged 7 commits into from
May 21, 2018

Conversation

LPGhatguy
Copy link
Contributor

@LPGhatguy LPGhatguy commented May 7, 2018

This changes the middleware signature and makes it line up more closely with Redux. It solves a problem we hit of not being able to get the store's state until an action was dispatched.

It also reverses the order that middleware runs in to be left-to-right. Previously, it was right-to-left and a lot of people were really confused, including us. Now people should be less confused, and we can use more precise language to describe middleware execution order.

This is a breaking change.

Old signature:

(nextDispatch) => (store, action) => any

New signature:

(nextDispatch, store) => (action) => any

In addition, the nextDispatch function (previously called next, which collided with Lua's standard library) no longer accepts the store as the first argument, ie it is bound to the store instance.

TODO:

  • CHANGELOG
  • Tests
  • Docs

@LPGhatguy LPGhatguy requested a review from ZoteTheMighty May 7, 2018 20:56
lib/Store.lua Outdated
local dispatch = function(...)
return unboundDispatch(self, ...)
end

for _, middleware in ipairs(middlewares) do
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want to do anything with #28 to make middleware ordering line up with Redux?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This would be the time to do it, but I'm unsure if we want to change the order. Because the middleware call sequence depends entirely on what the middleware actually do, I don't think either behavior is particularly intuitive...

Copy link
Contributor

Choose a reason for hiding this comment

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

They're not very intuitive, but at the same time I'm not sure there's a nicer way of specifying middleware order besides an array >.<

I think if we're going to stick to an array for middlewares then invoking them from the front of the list forward makes the most sense, but that's just me

Copy link
Contributor

@ZoteTheMighty ZoteTheMighty May 18, 2018

Choose a reason for hiding this comment

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

I agree that it's not intuitive either way.

I think the best approach is to probably document the way that the middleware is applied, because if you ever write any middleware there's a good chance you'll need to know one way or another. As long as the info is easy to find that should be enough.

Also, this doesn't necessarily need to be added in this PR.

@coveralls
Copy link

coveralls commented May 7, 2018

Coverage Status

Coverage increased (+0.009%) to 99.351% when pulling 93b6c54 on new-middleware-api into d1161cd on master.

@LPGhatguy
Copy link
Contributor Author

LPGhatguy commented May 7, 2018

What. The tests pass still, that's probably not a good sign.

@AmaranthineCodices
Copy link
Contributor

Most test cases should have passed still, actually...but the Store tests for middleware invocation never test the arguments. That's on me x.x

@LPGhatguy
Copy link
Contributor Author

Tests are updated, and I updated the CHANGELOG, since it was completely empty!

@LPGhatguy LPGhatguy changed the title WIP: Introduce new middleware API Introduce new middleware API May 18, 2018
lib/Store.lua Outdated
local dispatch = function(...)
return unboundDispatch(self, ...)
end

for _, middleware in ipairs(middlewares) do
Copy link
Contributor

@ZoteTheMighty ZoteTheMighty May 18, 2018

Choose a reason for hiding this comment

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

I agree that it's not intuitive either way.

I think the best approach is to probably document the way that the middleware is applied, because if you ever write any middleware there's a good chance you'll need to know one way or another. As long as the info is easy to find that should be enough.

Also, this doesn't necessarily need to be added in this PR.

A single middleware is just a function with the following signature:

```
(next) -> (store, action) -> result
(nextDispatch, store) -> (action) -> result
```

That is, middleware is a function that accepts the next middleware to apply and returns a new function. That function takes the `Store` and the current action and can dispatch more actions, log to output, or do network requests!
Copy link
Contributor

Choose a reason for hiding this comment

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

This probably needs to be updated as well:
"That is, middleware is a function that accepts the Store and the next middleware to apply and returns a new function. That function takes the current action and can dispatch more actions, log to output, or do network requests!"

More detail wouldn't hurt either, but can be added later.

@ZoteTheMighty
Copy link
Contributor

ZoteTheMighty commented May 18, 2018

After our discussion, I think this middleware sequence makes the most sense

@LPGhatguy LPGhatguy merged commit abe2847 into master May 21, 2018
@LPGhatguy LPGhatguy deleted the new-middleware-api branch May 21, 2018 23:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants