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

toggle hooks in middleware #653

Merged

Conversation

robinfehr
Copy link
Contributor

@robinfehr robinfehr commented Feb 12, 2018

  • makes it optional if the hooks are piped to the middleware.
    we might wan't to fine grain it so we can choose which hooks.
    the default is 'included' to not break current implementations.
addMiddleware(node, middleware: (actionDescription, next) => any, includeHooks = true)
  • throws on omitting next() within a middleware

const next = handler(call, runNextMiddleware)
if (!next && index < middlewares.length && process.env.NODE_ENV !== "production") {
const node = getStateTreeNode(call.tree)
fail(
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure about this check, there are cases where it is fine that next() isn't called, for example if you have middleware that prevents modifications, or only applies it to a copy, etc. I had a few cases before. I am aware it is pretty tricky if you forget it in your middleware, but I think that is an inherent risk about writing your own middleware and hopefully docs help preventing mistakes there :)

Copy link
Contributor Author

@robinfehr robinfehr Feb 15, 2018

Choose a reason for hiding this comment

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

fair point about aborting the middleware queue.
what about returning false if it the abort was intended and only throw on undefined?
or something similar more expressive

Copy link
Contributor Author

Choose a reason for hiding this comment

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

e.g. an abort() next to next()

API.md Outdated
@@ -124,7 +124,9 @@ For more details, see the [middleware docs](docs/middleware.md)
**Parameters**

- `target` **IStateTreeNode**
- `middleware`
- `middleware` **IMiddleware**
- `includeHooks` **([boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean) | any)** indicates whether the hooks should be piped to the middleware.
Copy link
Member

Choose a reason for hiding this comment

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

elobarote; that is, should the middleware be run for built in methods like applySnapshot`

@mweststrate mweststrate merged commit 4a52a04 into mobxjs:master Mar 7, 2018
@mweststrate
Copy link
Member

Merged!

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.

2 participants