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

Make the store.subscribe pass the action to the listener #1971

Closed
andywer opened this issue Sep 20, 2016 · 13 comments
Closed

Make the store.subscribe pass the action to the listener #1971

andywer opened this issue Sep 20, 2016 · 13 comments

Comments

@andywer
Copy link

andywer commented Sep 20, 2016

tldr; Is there a special reason why store.subscribe() does not pass the dispatched action to the listeners? I will open a PR if you feel comfortable passing down the action.

Use case

We are using the react-router-redux and I would like to subscribe to the store for a short time, so I get to know if a route change is triggered. It would be highly desirable to not only be informed that some action has just been dispatched, but what action has been dispatched, so I can immediately check if this action is of any interest to me (route change) and react to that event if so.

Cheers!

@jimbolla
Copy link
Contributor

The design philosophy is that subscribers react (no pun intended) to changes to state, not to specific actions.

@gaearon
Copy link
Contributor

gaearon commented Sep 20, 2016

This has been discussed about seven times before. Could you please be so kind to search the issues? I could drop some links when I'm home—I'm on the cell right now.

Basically we don't want to do this because there is actually no guarantee that we will call subscribers after every action. For example, if a subscriber causes a nested dispatch, we reserve the right to skip the nested notifications and only notify the subscribers when the outermost dispatch ends. Which means that if the action was passed, you would miss one.

I hope this helps! If you search the issues you'll find some advice about how to approach problems that require this. Middleware is a common solution to having logic depend on actions rather than the state.

@gaearon gaearon closed this as completed Sep 20, 2016
@andywer
Copy link
Author

andywer commented Sep 20, 2016

Thanks a lot and sorry for the inconvenience!

I already searched the issues before opening this one, but with almost 2k of them in stock it's pretty hard to find rather generic topic like this 🙈

@markerikson
Copy link
Contributor

markerikson commented Sep 20, 2016

Yeah, the FAQ really needs a "Design Decisions" section for stuff like this (which is in my list of updates to add, at #1785 ).

@gaearon
Copy link
Contributor

gaearon commented Sep 20, 2016

#347
#580
#1057
#1243
#1300
#1786

@ghost
Copy link

ghost commented Aug 24, 2017

@andywer - +1

@gaearon - Is being a bit obtuse towards guys who have recently being exposed to this. There are plenty of issues @gaearon, please be patient. Guessing the right keyword is not natural to most of us.

I ran into this here: #2584

I get what you are saying about not having to deal nested dispatches because you basically don't want to become a message bus, but you should at least allow the meta data to flow through. Let's please just make sure we are grounded in the fact that we are still coding in javascript and not haskell.

@TrySound
Copy link
Contributor

@fir3pho3nixx Man, there should be a place for things. It's called separation of concerns. If you want to pass do all in one place write own lib for all your needs. Redux should be clean and simple and it solves its use cases.

@ghost
Copy link

ghost commented Aug 24, 2017

@TrySound - My use case is because of angular and the fact that view binding to our store is triggered on each state change(admittedly further down the line). Our listener is being debounced and we use reselect on the view. We are building a calculation engine, so we care about purity of data and we also use Saga's. We also have complex graphs in our store because we trace each calculation. I am thinking along the same lines as you, I want to separate things even more. I need a new hook.

@TrySound
Copy link
Contributor

It's not a hook. It's a hack. I use redux with angular.js and it's okay. Nothing special here. Want to react on actions - use middleware. Want event bus, use streams (most or rx). You may even use 'em together like most.from(store) or rx.fromObservable(store) (store is also observable under the hood).

@markerikson
Copy link
Contributor

@fir3pho3nixx : per the prior comments, the intended usage of Redux is that subscribers should be reacting to the current state and changes from the previous state, not the actions themselves.

As @TrySound just commented, if you do want to have logic that hooks into dispatched actions, the right place is middleware. Existing general-purpose side effects libraries like redux-saga, redux-observable, and redux-logic all make it easy to do work in response to specific actions, there's many other more specific action handling middleware, or you can always write your own.

@ghost
Copy link

ghost commented Aug 24, 2017

@TrySound and @markerikson

First of all, thanks for replying, secondly thanks for putting up with me. I am not sure you got this right you know? You have a functionally pure store that pummels the bejesus out of angular for irrelevant data updates when using complex graphs. I inherited this paradigm. I just have pureist views on the runtime. If you are doing this in javascript, you're using the wrong language.

@ghost
Copy link

ghost commented Aug 24, 2017

Time for Fable anyone. :)

@iofjuupasli
Copy link

For anyone still looking for that feature
I found redux-action-watch

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

No branches or pull requests

6 participants