RXJS CombinAtest仅将计时器添加到一个可观察的
我有两个实体, games 和大奖:
游戏界面
{
id: string,
name: string
}
奖金接口
{
game: string,
amount: number
}
首先,我有两个可观察,一个可以获得游戏,一个获得 jackpots 。
在获取数据后,我想将 jackpots 的数据合并到基于 id 的 Games 中。而且,由于jackpots observale应该在每秒钟内获取数据,所以我使用操作员计时器
来做到这一点。因此,这是我的实现:
private getGames = (category: string): Observable<IGame[]> => {
return timer(0, 3000).pipe(
mergeMap(() => combineLatest([this.gameService.getGames(), this.gameService.getJackpots()])),
map(([games, jackpots]) => {
return games?.filter(game => game?.categories?.includes(category))?.map(game => {
return { ...game, jackpot: jackpots?.find(jackpot => jackpot?.game === game?.id) };
})
})
);
};
此实现效果很好,并且每3秒获取数据。现在,正如您可以看到 Games 和 jackpots 可观察到的可观察每3秒钟,我的问题是:有没有办法只运行 jackpots 每3秒钟可观察一次,并从该计时器中排除游戏可观察到的。
I have two entities, Games and Jackpots:
Game interface
{
id: string,
name: string
}
Jackpot interface
{
game: string,
amount: number
}
First I have two observables, one to get games and one to get jackpots.
And after getting the data i want to merge the data of jackpots into games based on id. And since the jackpots observale should get the data each seconds i used the operator timer
to do so. So here's my implementation:
private getGames = (category: string): Observable<IGame[]> => {
return timer(0, 3000).pipe(
mergeMap(() => combineLatest([this.gameService.getGames(), this.gameService.getJackpots()])),
map(([games, jackpots]) => {
return games?.filter(game => game?.categories?.includes(category))?.map(game => {
return { ...game, jackpot: jackpots?.find(jackpot => jackpot?.game === game?.id) };
})
})
);
};
This implementation works fine and the data is fetched every 3 seconds. Now as you can see both Games and Jackpots observable get fetched every 3 seconds, My question is: is there a way to only run the Jackpots observable every 3 seconds, and exclude the Games observable from that timer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
上下文
combineLatest
具有两个重要属性:解决方案
您只能为 Games 可观察的
计时器
设置,并将其与 jackpots One组合。Context
combineLatest
has two important properties:Solution
You can set the
timer
only for the games Observable and combine it with the jackpots one.