rxjs - 什么触发了 combineLatest?

标签 rxjs observable combinelatest

我有一些观察结果。我需要知道哪个触发了订阅。

Observable.combineLatest(
      this.tournamentsService.getUpcoming(),
      this.favoriteService.getFavoriteTournaments(),
      this.teamsService.getTeamRanking(),
(tournament, favorite, team) => {
//what triggered combinelatest to run?
}).subscribe()

最佳答案

实现这一点的一种非常干净和“rx”的方式是使用时间戳运算符 http://reactivex.io/documentation/operators/timestamp.html

示例代码

sourceObservable
  .pipe(
    timestamp(),  // wraps the source items in object with timestamp of emit
    combineLatest( otherObservable.pipe( timestamp() ), function( source, other ) {

      if( source.timestamp > other.timestamp ) {

        // source emitted and triggered combineLatest
        return source.value;
      }
      else {

        // other emitted and triggered combineLatest
        return other.value;
      }

    } ),
  )

如果 combineLatest() 中涉及两个以上的可观察量按时间戳对它们进行排序将能够检测哪个触发了 combineLatest() .

关于rxjs - 什么触发了 combineLatest?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40804674/

相关文章:

javascript - RxJS5: .combineLatest() 有投影功能?

javascript - 以 Rx 方式定期轮询

java - RxJava 手动发出项目

swift - 如何过滤 combineLatest 仅在一项更改时触发?

Angular如何使用combineLatest组合来自网站网址栏的路线和查询

angular - 使用 props 在 createSelector ngrx 中过滤

java - 端到端响应式(Reactive)流式处理 RESTful 服务

android - 如果我的 RxJava 2 调用返回 Single 或 Maybe,我还需要使用 CompositeDisposable 吗?

angular - 无效管道参数 : '[object Object]' for pipe 'AsyncPipe'

rxjs - 如何为 combineLatest rxjs Angular 编写 Jasmine 单元测试用例