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

Redux over the wire? #211

Closed
sergey-lapin opened this issue Jul 3, 2015 · 5 comments
Closed

Redux over the wire? #211

sergey-lapin opened this issue Jul 3, 2015 · 5 comments

Comments

@sergey-lapin
Copy link

This is a great way of using flux on mobile, with web workers or whatever - splitting it on the client and server.
Could this be done in redux? This seems to me more magical as redux has stateless stores. Typically the simplest solution of this is to keep state in stores and stream stores changes to components which are located in other context.

@sergey-lapin
Copy link
Author

As I understood from middleware docs it is pretty easy to intercept and even redirect actions.

So in this scheme direction from Component to Store Producer is manageable so I can log actions to some other context for example.

          +----------------> onChange ---------------+
          |                                          v
    Store Producer/Action Consumer          Store Consumer/Action Producer
       "SSoT" or "Dispatcher"                  "View" or "Component"
          ^                                          |
          +---------------- onDispatch --------------+

But is that possible to intercept store changes in the same way?

@sergey-lapin
Copy link
Author

I am looking for such configuration:

client reponsibility:
generate actions
consume store changes

server reponsibility:
generate store changes
consume actions

As I comprehend redux API only action generation is interceptable for now.

@sergey-lapin
Copy link
Author

Figured out solution, thanks to @raydeo.
redux has public methods to subscribe/get notified on new state (subscribe, getState),
(which allows to generate store changes)
and dispatch
(which allows to consume actions)

To consume store changes I need to create special store-reducer that set new state on some action, and
dispatch({type: SET_STATE, newState: stateFromServer})
(which allows to consume store changes)

middleware allows to intercept and generate actions.

@sergey-lapin
Copy link
Author

Here is implemention of this approach and demo

@gaearon
Copy link
Contributor

gaearon commented Jul 31, 2015

Let's continue this discussion in #313.

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

2 participants