简体   繁体   English

Rxjs 将多个可观察对象组合成单个 boolean 可观察对象

[英]Rxjs Combine multiple observables to a single boolean observable

I am trying to Combine multiple observables to a single boolean observable.我正在尝试将多个可观察对象组合到单个 boolean 可观察对象。

Currently I'm using scan and it's running using the previously emitted value for the accumulator when I need it to use true every time.目前我正在使用扫描,当我需要它每次都使用 true 时,它正在使用之前为累加器发出的值运行。 I'm not sure how to achieve this.我不确定如何实现这一目标。

    this.partnershipFundInvalid$ = merge(
      this.partnershipFundId$.pipe(map(id => id === null)),
      this.partnershipsFileUploader.dataImportState$.pipe(map(state => state.file !== null)),
    ).pipe(
      scan((acc, val) => {
        return acc && val === true;
      }, true),
    );

Scan, but ignore the accumulated value扫描,但忽略累加值

    this.partnershipFundInvalid$ = merge(
      this.partnershipFundId$.pipe(map(id => id === null)),
      this.partnershipsFileUploader.dataImportState$.pipe(map(state => state.file !== null)),
    ).pipe(
      scan((acc, val) => {
        return true && val === true; // Replaced acc with "true"
      }, true),
    );

That's semantically the same as map这在语义上与 map 相同

    this.partnershipFundInvalid$ = merge(
      this.partnershipFundId$.pipe(map(id => id === null)),
      this.partnershipsFileUploader.dataImportState$.pipe(map(state => state.file !== null)),
    ).pipe(
      map(val => {
        return true && val === true;
      }),
    );

map doesn't emit until all the streams are closed map 在所有流关闭之前不会发出

That's not how map works, it doesn't deal with error or completion emissions.这不是 map 的工作方式,它不处理错误或完成排放。


I would suggest updating your question to include what you're trying to accomplish as 'I am trying to "reduce" a Boolean' is ambiguous.我建议更新您的问题以包括您要完成的工作,因为“我正在尝试“减少”布尔值”是模棱两可的。 There are many ways to "reduce" a Boolean有很多方法可以“减少”一个 Boolean

A friend suggested this, i was completely over thinking it.一个朋友建议这个,我完全想多了。

this.partnershipFundInvalid$ = combineLatest(
  this.partnershipFundId$,
  this.partnershipsFileUploader.dataImportState$,
).pipe(
  map(([fundId, partnershipsFile]) => {
    return fundId == null && partnershipsFile.file !== null;
  }),
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM