abc123s / redux-batch-enhancer

Batch subscriber notification for an array of actions (including complex actions, e.g. thunks).
25 stars 8 forks source link

thunk actions with promises that use batchActions has some issues #3

Open jonashartwig opened 8 years ago

jonashartwig commented 8 years ago

Hi,

is nesting batchActions supported? I am experiencing some issues which i cannot pinpoint right now.

jonashartwig commented 8 years ago
export function deleteDashboard() {
    return (dispatch, getState) => {
        const store = getState(),
            dashboardId = store.dashboards.active.model.id,
            favorite = store.favorites.items.find(favorite => favorite.identifier === dashboardId);

                if(favorite)
                    dispatch(
                        batchActions([
                            closeDashboard(),
                            showSystemMessage("dashboard.delete.success"),
                            removeFavorite(favorite)
                        ])
                    );
                else
                    dispatch(
                        batchActions([
                            closeDashboard(),
                            showSystemMessage("dashboard.delete.success")
                        ])
                    );
    };
}

export function closeDashboard() {
    return batchActions([
        removeActiveRecentVisit(),
        navigateToDashboardHome(),
        {
            type: DASHBOARD_ACTIONS.CLOSE
        }
    ]);
}

this is the example code that works. if i add a promise to it it somehow explodes because closeDashboard removes some store state that is required by some views. I cnat figure out how. This might not even be an issue of this api.

// for some reason not working
export function deleteDashboard() {
    return (dispatch, getState) => {
        const store = getState(),
            dashboardId = store.dashboards.active.model.id,
            favorite = store.favorites.items.find(favorite => favorite.identifier === dashboardId);

        return Promise.resolve()
            .then(() => {
                if(favorite)
                    dispatch(
                        batchActions([
                            closeDashboard(),
                            showSystemMessage("dashboard.delete.success"),
                            removeFavorite(favorite)
                        ])
                    );
                else
                    dispatch(
                        batchActions([
                            closeDashboard(),
                            showSystemMessage("dashboard.delete.success")
                        ])
                    );
            });
    };
}

if i remove the batched action from the promise it works:

export function deleteDashboard() {
    return (dispatch, getState) =>
        serviceDeleteDashboard(getState().dashboards.active.model.id)
            .then(() => {
                        dispatch(removeActiveRecentVisit());
                        dispatch(navigateToDashboardHome());
                        dispatch({
                            type: DASHBOARD_ACTIONS.CLOSE
                        });
                        dispatch(showSystemMessage("dashboard.delete.success")
                );
            });
}

a bit more info: removeActiveRecentVisit(), DASHBOARD_ACTIONS.CLOSE, showSystemMessage are plain store mutations, navigateToDashboardHome however changes the url route using redux-router.