RXJS CombinAtest仅将计时器添加到一个可观察的

发布于 2025-02-10 09:28:57 字数 1212 浏览 1 评论 0原文

我有两个实体, 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安人多梦 2025-02-17 09:28:57

上下文

combineLatest 具有两个重要属性:

  • 每当观察物发出值时,它会发出。
  • 它要求所有源可观察到至少发出一个值。

解决方案

您只能为 Games 可观察的计时器设置,并将其与 jackpots One组合。

private getGames = (category: string): Observable<IGame[]> => {
  return combineLatest([
    timer(0, 3000).pipe(mergeMap(() => this.gameService.getGames())),
    this.gameService.getJackpots(),
  ]).pipe(
    map(([games, jackpots]) => {
      return games
        ?.filter((game) => game?.categories?.includes(category))
        ?.map((game) => {
          return { ...game, jackpot: jackpots?.find((jackpot) => jackpot?.game === game?.id) };
        });
    })
  );
};

Context

combineLatest has two important properties:

  • it emits every time any of the Observables emits a value.
  • it requires all source Observables to emit at least one value.

Solution

You can set the timer only for the games Observable and combine it with the jackpots one.

private getGames = (category: string): Observable<IGame[]> => {
  return combineLatest([
    timer(0, 3000).pipe(mergeMap(() => this.gameService.getGames())),
    this.gameService.getJackpots(),
  ]).pipe(
    map(([games, jackpots]) => {
      return games
        ?.filter((game) => game?.categories?.includes(category))
        ?.map((game) => {
          return { ...game, jackpot: jackpots?.find((jackpot) => jackpot?.game === game?.id) };
        });
    })
  );
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文