簡短的回答是:你不知道。 你可以實現一些解決方法,但是這真的很醜,我會建議重新考慮用例,爲什麼你需要這個,也許如果你可以改變架構。還請記住,函數的第一次執行將在所有三個觀測值發出至少一個值之後進行。
反正 - 一個可能的解決方法可能是:
let trigger = "";
Observable.combineLatest(
this.tournamentsService.getUpcoming().do(() => trigger = "tournament"),
this.favoriteService.getFavoriteTournaments().do(() => trigger = "favTournament"),
this.teamsService.getTeamRanking().do(() => trigger = "teamRanking"),
(tournament, favorite, team) => {
console.log(`triggered by ${trigger}`);
}).subscribe();
如果你想在此基礎上可觀察到觸發,你應該使用每個可測量的執行特定操作,並利用它們作爲單獨的觸發器,該交換機一個聯合觸發器,它可能是稍微多一點的代碼,但它更清潔,並且不會以醜陋的if/else,switch/case-mess與一些hacky解決方法結束 - 加上您甚至可以使用oportunity async
- 管道而不是手動訂閱所有內容並更新局部變量(反正這是一個不好的做法):
下面是如何可能看起來像一些示例代碼:
let upcoming$ = this.tournamentsService.getUpcoming();
let favorite$ = this.favoriteService.getFavoriteTournaments();
let rankings$ = this.teamsService.getTeamRanking();
let allData$ = Observable.combineLatest(
upcoming$, favorite$, rankings$,
(tournament, favorite, team) => {
return {tournament, favorite, team};
}
);
// initial call -> this SHOULD be redundant,
// but since I don't know your code in detail
// i've put this in - if you can remove it or not
// depends on the order your data coming in
allData$
.take(1)
.do(({tournament, favorite, team}) => {
this.displayMatches(...);
this.sortByFavorites(...);
this.fillWithRanking(...);
})
.subscribe();
// individual update triggers
upcoming$
.switchMapTo(allData$.take(1))
.do(({tournament, favorite, team}) => this.displayMatches(...))
.subscribe();
favorite$
.switchMapTo(allData$.take(1))
.do(({tournament, favorite, team}) => this.sortByFavorites(...))
.subscribe();
rankings$
.switchMapTo(allData$.take(1))
.do(({tournament, favorite, team}) => this.fillWithRanking(...))
.subscribe();
訂閱observable就像調用函數一樣。 – Manish
好的,但沒有回答我的問題。 – Alexandr