本文介绍了使用哪个合并运算符来侦听单个源而不是第二个流源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我知道这个问题看似简单,但并不是那么简单.我尝试过的所有运算符,例如combineLatestconcatswitchMap都会导致差异问题.

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.

这就是挑战:

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';
})

所以我有两个流,并且只希望在第一个流campaignSelected$发出时得到通知,这时我要切换并从第二个campaignsList$流中获取数据,到目前为止一切都很好

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.

但是,如果以后由于存储已更改而再次发出campaignsList$的消息,我将再次收到通知,并且我也不想收到通知,因为我只关心campaignSelected$中的更改何时发生.

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$.

请记住,我的第二个流不会在我的第一个流同时更改,因此诸如concat之类的运算符将无法工作.

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.

现在我已经尝试了combineLatest和diff合并运算符,但是它们都不足够,即:仅在第一个流发出时才需要通知,而在第二个流发出时则不需要,但是我们仍然需要从第二流,但仅当第一流发出时.

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.

推荐答案

如果要在第一个流发出时而不是第二个流发出时得到通知,请 withLatestFrom 运算符是您要查找的内容:

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) => { ... }
);

这篇关于使用哪个合并运算符来侦听单个源而不是第二个流源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 00:30