我有一些观察结果。我需要知道哪个触发了订阅。
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/