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

HTTP request send twice if route changed back #196

Open
AndrejSperling opened this issue Jun 20, 2017 · 3 comments
Open

HTTP request send twice if route changed back #196

AndrejSperling opened this issue Jun 20, 2017 · 3 comments

Comments

@AndrejSperling
Copy link

I have a strange behavior maybe someone can help.
Currently I have a router component, that is loading all pages.
On the StartPage I have a form that can POST some entries.

Now, if I made an POST request and switched from '/start' to '/feed' and back to '/start' the POST request is send a second time.

const routedComponent = (sources) => ({path, value}) => value({...sources, router: sources.router.path(path)});

const routes = {
    '/start': StartPage,
    '/feed': UnderConstructionPage,
    '*': NotFoundPage,
};

export function Router(sources: AppSources): AppSinks {
 const routes$: Stream<SwitchPathReturn> = sources.router.define(routes);
    const page$ = routes$.map(routedComponent(sources));

    const redirectStartpage$ = sources.router.history$
        .filter(loc => loc.pathname === '/')
        .mapTo('/start');

    return {
        DOM: page$.map(c => c.DOM || xs.empty()).flatten(),
        HTTP: page$.map(c => c.HTTP || xs.empty()).flatten(),
        router: xs.merge(page$.map(c => c.router || xs.empty()).flatten(), redirectStartpage$),
        onion: page$.map(c => c.onion || xs.empty()).flatten(),
    };
}

On the StartPage I have a intent that is listen to a HTTP stream.
And if the response is successfull I show a message.

export function intent(sources: AppSources) {

    const {DOM,HTTP} = sources;

    return {
        newSetClick$: DOM.select(ID_NEW_SET_BTN).events('click').debug('CLICK'),
        refreshSetList$: xs.of(GetSetsApi.buildRequest()),
        newSetResponse$: HTTP.select(PostSetApi.ID).flatten()
    };

}

function reducer(action: any) {

    const initReducer$ = xs.of(function initReducer(state) {
        return {
            showNewCardMessage: false,
            newCardMessage: {}
        }
    });

    const newCardMessageReducer$ = action.newSetResponse$
        .filter(response => response.ok)
        .map(res => ({
            id: res.body._id,
            title: res.body.title
        }))
        .map(res => function cardMessageReducer(state) {
            return {
                ...state,
                showNewCardMessage: true,
                newCardMessage: {
                    id: res.id,
                    title: res.title
                }
            }
        });


    return xs.merge(initReducer$, newCardMessageReducer$);
}

The request is send by Startpage itself but only if the submit button was clicked.
Now if I remove the "Intent" the behavior disapears.

@ntilwalli
Copy link
Collaborator

Is it this issue? cyclejs/cyclejs#592

@ntilwalli
Copy link
Collaborator

ntilwalli commented Jun 20, 2017

Just FYI, if you'd like to test if that issue is what's causing the problem you can look at the changes I made in @cycle/run and @cycle/rxjs-run to hack a fix for it. The change can be reviewed here:
https://github.com/ntilwalli/cyclejs/blob/stumplog/run/src/index.ts#L83

If you are using RxJS you can even use my forked (workaround) version or rxjs-run on npm called stump-rxjs-run and see if that fixes your issue.

@AndrejSperling
Copy link
Author

@ntilwalli It sounds like the same issue, I tried your hack and it worked, but I have to test my app more to be sure that there are no other side effects.

But thanks for your help!

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