store icon indicating copy to clipboard operation
store copied to clipboard

store.dispatch(...).subscribe(...) called twice for async Actions

Open xmlking opened this issue 8 years ago • 12 comments

store.dispatch(...).subscribe(...) called twice when Action with async method implementation dispatched. This is leading bugs as we have some business logic executed twice with in subscribe method.

if you remove async , this will print once as expected.

This issue got introduced in 2.0 release. it was working fine in 1.x

stackblitz: https://stackblitz.com/edit/ngxs-router-sumo

import { State, Action, StateContext } from 'ngxs';

export class Add {}

@State({
  name:'app',
  defaults: {
    count: 0
  }
})
export class AppState {
  @Action(Add)
  async add({ getState, setState }: StateContext<any>) {
    await new Promise(resolve => setTimeout(resolve, 1000));
    console.log("in add")
    setState({
      count: getState().count + 1
    })
  }
}
  onClick() {
    this.store.dispatch(new Add()).subscribe( () => {
      console.log('... this will print twice')
    });
  }

xmlking avatar Mar 20 '18 04:03 xmlking

@xmlking can you confirm this only happens twice this way? it doesn't happen when not using way or when using an observable?

deebloo avatar Mar 20 '18 12:03 deebloo

It happened when “async” is used. If I remove async it worked normally. I haven’t tested observable yet. Will do ...

xmlking avatar Mar 20 '18 14:03 xmlking

It happens with an observable as well. Even in the dev tools I see double entries (sometimes triple) entries for my async actions. I'm also seeing "Object" entries. No idea what they are why they are there. On top of this, the entries in the dev tools are out of order. The action being dispatched comes after a dispatched action inside another.

Example: I subscribe a dispatched CreateRecipe action inside my component (only dispatched once). The action then sets initial state (pending: true) and then calls the backend via observable form the RecipeService. It then dispatches a CreateRecipeSuccess with the created object to set the state otherwise it dispatches CreateRecipeFail. In the dev tools, CreateRecipeSuccess shows up first, followed by two entries of "Object" and then 3 entries of CreateRecipe.

image

I believe one of the entries for CreateRecipe is me subscribing to the dispatched action. If I remove the subscription, only two entries for CreateRecipe are in the dev tools.

trevor-hackett avatar Mar 20 '18 15:03 trevor-hackett

ok I can reproduce in the example and in a test, if you return a promise it happens. @amcdnl any ideas? Is it trying to dispatch a new action?

deebloo avatar Mar 20 '18 17:03 deebloo

Ya I think i know.... Old code here: https://github.com/amcdnl/ngxs/blob/master/src/store.ts#L122

amcdnl avatar Mar 20 '18 17:03 amcdnl

@jschwarty ^

I think it’s because the observable you are creating in store.dispatch is unicast. So each subscribe gets its own execution context. Maybe that needs to be a multicast (like a Subject or BehaviorSubject)

amcdnl avatar Mar 20 '18 20:03 amcdnl

I believe this fixes it - https://github.com/amcdnl/ngxs/commit/ce4e6638fb2d88dffdf49c5bc063c72109f097bf

@jschwarty - can u sanity check this for me?

amcdnl avatar Mar 20 '18 20:03 amcdnl

That mostly fixed it. However, this doesn't fix subscribing to the observable returned from Store.dispatch(). If you subscribe to that observable, it causes the action to be dispatched again.

trevor-hackett avatar Mar 20 '18 21:03 trevor-hackett

I guess this may be a different issue. I'll open up a new issue.

trevor-hackett avatar Mar 20 '18 21:03 trevor-hackett

Hi @yarrgh Having same issue. If I'm subscribing to observable from Store.dispatch() I see my action twice in a log: 2018-05-22_01-09-57

However console.log(); inside action prints in console only once.

biased-badger avatar May 21 '18 22:05 biased-badger

This is still an issue in version 18.0.0

image

conradbekondo avatar Jun 21 '24 09:06 conradbekondo

Hi, Please make sure that the action types in your actions.ts are all unique. In my case, I had double actions dispatch because two actions had the same string assigned.

dpietruc avatar Jul 11 '24 14:07 dpietruc