Skip to content

Pass action/type to subscribed listener? #347

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

Closed
nd0ut opened this issue Jul 28, 2015 · 7 comments
Closed

Pass action/type to subscribed listener? #347

nd0ut opened this issue Jul 28, 2015 · 7 comments

Comments

@nd0ut
Copy link

nd0ut commented Jul 28, 2015

It would be cool if the subscribers will know what action has caused changes. What do you think about it?

@gaearon
Copy link
Contributor

gaearon commented Jul 28, 2015

Can you expand on the use case?

@acstll
Copy link

acstll commented Jul 28, 2015

I use it for logging (in my own subset of Redux), but that's a typical case for middleware.

@nd0ut
Copy link
Author

nd0ut commented Jul 28, 2015

I wanna integrate my redux app with openlayers map.

For example, action CHECK_OBJECT dispatched with object_ id as payload. Then reducer handles that action and changes state. After that I need to show that checked object on the map. One way I see is to catch up actions after all reducers are applied (via subscribe with action as argument), get object from new state and then add that object on the map.

Openlayers is not react-compatible through such integration will be kind of hacking anyway. Maybe there are much better way to do that integration?

@gaearon
Copy link
Contributor

gaearon commented Jul 28, 2015

@nd0ut

What data do you need to grab from action? In other words why doesn't this work for you:

this.props.dispatch(checkObject(id));
this.props.dispatch(showOnTheMap(id));

@nd0ut
Copy link
Author

nd0ut commented Jul 29, 2015

@gaearon I think it is bad idea because then the code managing openlayers should be in the action creator or in the reducer.

Also the openlayers map and the react component are logically different and I think that the react component should not calling showOnTheMap(id), it should know nothing about the map and just render the item list with the check buttons. And that chain item checked -> item showed on the map should be processed somewhere inside the app separately from react components.

My example below was a bit simplified. In fact I also need to synchronize the map state (zoom, extent and other properties) and the redux state. In other words the redux state changes should cause the map state changes and otherwise.

In my mind there should be some kind of central hub between redux and openlayers. If the hub receives an event from the openlayers then the hub dispatches action to the store. And otherwise, if the hub (subscriber) receives action then it send change to openlayers. Here is a little dirty diagram.
Image of Yaktocat

I think all the communications between redux and openlayers should going through that map manager. It is easy to control and maintain. And if subscriber will receive dispatched action then it will be possible to realize such functionality.

As to the middleware, it handles the action before applying the reducers. And I need to handle action after applying reducers to get new modified state. Actually I can apply the reducers to the cloned state manually but it's kind of freaking :)

@gaearon
Copy link
Contributor

gaearon commented Jul 29, 2015

As to the middleware, it handles the action before applying the reducers.

No, you can write middleware to execute code after the dispatch occurs.

Example from the new docs:

function logger({ getState }) {
  return (next) => (action) => {
    console.log('will dispatch', action);

    // Call the next dispatch method in the middleware chain.
    let returnValue = next(action);

    console.log('state after dispatch', getState());

    // This will likely be the action itself, unless
    // a middleware further in chain changed it.
    return returnValue;
  };
}

@nd0ut
Copy link
Author

nd0ut commented Jul 29, 2015

Oh sorry I overlooked it. Exactly what I need. Thanks.

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

3 participants