问题
So I know the question may seem simple, but it's not that simple.
All the operators I have tried such as combineLatest
, concat
, and switchMap
result in diff issues.
So here is the challenge:
var campaignSelected$ = this.store.select(store => store.appDb.uiState.campaign.campaignSelected)
var campaignsList$ = this.store.select(store => store.msDatabase.sdk.table_campaigns);
return campaignSelected$.switchMap(i_campaignSelected => campaignsList$, (campaignId, campaigns) => {
return 'foo';
})
so I have two streams, and I only want to be notified when my first stream campaignSelected$
emits, at which point I want to switch over and grab data from my second stream of campaignsList$
, so far all is great.
However, if at a later time, campaignsList$
emits again because the store has changed, I will be notified again, and I do not want to be notified, as I only care about when changes occur in campaignSelected$
.
Keep in mind that my 2nd stream does NOT change at the same time my first stream does, so operators such as concat will not work.
Now I have tried combineLatest
and diff merge opertors and none of them will suffice, i.e.: needing to ONLY be notified when my first stream emits and NOT when my 2nd stream emits, but we still need to grab data from the 2nd stream, but if and only if, the 1st stream emits.
回答1:
If you want to be notified when the first stream emits, but not when the second emits, the withLatestFrom operator is what you are looking for:
import 'rxjs/add/operator/withLatestFrom';
var campaignSelected$ = this.store.select(
store => store.appDb.uiState.campaign.campaignSelected
);
var campaignsList$ = this.store.select(
store => store.msDatabase.sdk.table_campaigns
);
return campaignSelected$.withLatestFrom(
campaignsList$,
(campaignId, campaigns) => { ... }
);
来源:https://stackoverflow.com/questions/41885283/which-merge-operator-to-use-to-listen-to-single-source-and-not-2nd-stream-source