what triggered combineLatest?

后端 未结 4 860
情深已故
情深已故 2020-12-30 07:55

I have a few observables. And I need to know which one triggered the subscribe.

Observable.combineLatest(
      this.tournamentsService.getUpcoming(),
               


        
4条回答
  •  庸人自扰
    2020-12-30 08:35

    A quite clean and "rx"-way of achieving this is by using the timestamp operator http://reactivex.io/documentation/operators/timestamp.html

    Example code

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

    If more than two observables are involved in the combineLatest() sorting them by timestamp would enable to detect which one triggered the combineLatest().

提交回复
热议问题