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

Create middleware to assure actions dispatched in right order #22

Open
bognix opened this issue May 25, 2017 · 1 comment
Open

Create middleware to assure actions dispatched in right order #22

bognix opened this issue May 25, 2017 · 1 comment

Comments

@bognix
Copy link

bognix commented May 25, 2017

I worked on simple application which uses the redux-dsm library and I stumbled upon one problem.
Example code: #21

The app is very simple. I asynchronously load data from the server and then I render the component with the list of items fetched from the server. I have 4 states defined: IDLE, SUCCESS, ERROR and FETCHING. I basically copied the configuration from https://github.com/ericelliott/redux-dsm#usage-example. I use the reducer and action creators provided by the library (https://github.com/bognix/redux-dsm/blob/example-usage/examples/simpleFetch/dsm.js).

There is one part I feel uncomfortable with in my example:

const mapStateToProps = (state) => {
    const {list: {payload: {messages} = {}, status} = {}} = state;

    switch (status) {
        case STATUS.IDLE:
        case STATUS.SUCCESS:
            return {
                messages
            };
        case STATUS.ERROR: {
            return {
                error: true
            };
        }
        case STATUS.FETCHING:
        default:
            return {
                loading: true
            };
    }
};

When mapping state to props in redux connect I ended up with something that looks like a part of code that belongs to the reducer. However, because I use the reducer created by the library there is no obvious way (for me at least) to combine two reducers so they work on the same part of store. The reducer created by redux-dsm is responsible for assuring that actions are dispatched in proper order and update state only in such case. My reducer would be responsible for populating the state with correctly shaped data.

I thought about it and for me it seems that the redux-dsm reducer responsibility could be replaced with a middleware. So the redux-dsm library would return a middleware which when applied would assure that actions are dispatched in right order.

Before I dig into the implementation I would like to know what you think about it and maybe how to solve the problem I mentioned with different means.

@dash-
Copy link
Collaborator

dash- commented Jun 9, 2017

@ericelliott What are your thoughts on this?

@dash- dash- closed this as completed Jun 9, 2017
@dash- dash- reopened this Jun 9, 2017
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