-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
Proposal: Global action enhancers - WIP #929
Conversation
Hi @deini. Thanks for your contribution! One thing I'm concerning is that global action enhancer seems a bit overkill to me. It potentially allows users to mutate all the dispatched actions and can complicates the actions flow. As we just want to listen the function actionLoggerPlugin(store) {
// Not sure how we should call this method...
store.subscribeAction(({ type, payload }, state) => {
console.log(`Before dispatching "${type}" with ${JSON.stringify(payload)}`
})
}
const store = new Vuex.Store({
// ...
plugins: [actionLoggerPlugin]
}) |
@ktsn I agree, global action enhancers are overkill for #908, however it could open up for more middleware use cases. It indeed complicates things more and you are right that a user can mutate / discard actions. I like your proposal, as its all we need for #908. Not sure if you guys had any other use cases for GAE, but if not, feel free to close and I can give |
|
I'm currently playing around with the ideas from This pattern uses actions as triggers for route navigation, so these proposed actionEnhancers would actually be perfect to implement navigation guards, redirects etc. Currently I monkeypatch a |
63373df
to
81602c6
Compare
@LinusBorg Thanks for the idea! I didn't know I'm thinking how we can implement that concept in Vuex and it could be solved without enhancer/middleware feature. PoC is https://github.com/ktsn/vuex-first-router For example, if we have I'm not sure if we need to use the same action name to achieve this concept. But to my understanding, the implementation would be simpler by separating the actions because we don't need to branch the further processing where the action comes from (changing url on address bar or directly dispatching it). Maybe I'm a bit harsh with this topic 😅 . But I'd say the action already can do various things out of the box, not sure the trade off between the benefit and additional complexity. |
I initially asked on stackoverflow, but I am looking for to trigger various mutations when actions are dispatched.
Any update on this PR, or anything I can do to help out? |
Any progress on this? I'm currently looking for a way to :
I got this to work to overwrite vuex's dispatch and commit methods. However, it would be nice if I could achieve both in a supported and standardized way. |
Bump. Ran into another use case where this would be very useful. While creating a small ORM to handle mutations of the state with all internal relations in a standard way, it would be very handy if I could pass the state to the ORM manager before every mutation. Currently, I'd have to either (1) call the function at the beginning of every mutation manually or (2) overwrite the dispatch/commit methods again. Option 1 is an unacceptable amount of boilerplate that I couldn't afford in any professional project. |
I posted earlier asking for a way to subscribe to actions which was indeed added in 2.5 - @jannesiera couldn't you write a plugin that just hooks into function plugin() {
return store => {
store.subscribeAction(action, state) => {
// do stuff
}
}
}
new Vuex.Store({
plugins: [plugin()]
}) Something along those lines. |
@lmiller1990 |
How we can cancel action or modify payload inside store.subscribeAction hook? |
@jannesiera
But if I understand correctly, the middleware logic can be put just at the beginning of an action:
|
Currently, Vuex has I'll keep this issue open for now to gather feedbacks if there's any. |
Closing due to inactivity. I think this can be done by |
Proposal to bring Global action enhancers.
I wanted a way to log all actions and their payload when dispatched. Following the discussion in #908 seems like there is no way right now to do cleanly.
The proposal is to bring global action enhancers that work just like redux's middleware.
Using the count example and some inline actionEnhancers:
If we can get something like this, then adding an actions tab in devtools would be pretty straight forward.
TODO: