Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

concatMap not getting subsequent store emits

Tags:

angular

rxjs

ngrx

I have this setup:

public listenCampaignSelected(){
  var campaignSelected$ = this.store
    .select(store => store.appDb.uiState.campaign.campaignSelected)
  var campaigns$ = this.store
    .select(store => store.msDatabase.sdk.table_campaigns);

  return campaignSelected$.concatMap(
    v => campaigns$, 
    (campaignId,campaigns) => {
      return campaigns.find((i_campaign: CampaignsModelExt) => {
        return i_campaign.getCampaignId() == campaignId;
      });
  })
}

and nothing emits except on the first subscribtion. now values ARE changing in $campaignSelected, I can see the store being updated.

this is how I subscribe:

this.yp.listenCampaignSelected().subscribe(
  (campaign:CampaignsModelExt) => {
    this.campaignModel = campaign;
  this.renderFormInputs();
});

Now here is the interesting thing, if I change concatMap to switchMap all works.

so I am trying to understand why switchMap will work in this case and concatMap does not

like image 564
born2net Avatar asked Nov 01 '25 06:11

born2net


1 Answers

So the answer is, concatMap will not work since the 2nd stream does not change change / fire. Thus you want to use switchMap which switches over the new stream regardless of any changes on that stream:

  var campaignSelected$ = this.store.select(store => store.appDb.uiState.campaign.campaignSelected)
        var campaigns$ = this.store.select(store => store.msDatabase.sdk.table_campaigns);
        return campaignSelected$.switchMap(v => campaigns$, (campaignId,campaigns)=> {
            return campaigns.find((i_campaign: CampaignsModelExt) => {
                return i_campaign.getCampaignId() == campaignId;
            });
        })
like image 182
born2net Avatar answered Nov 03 '25 22:11

born2net



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!