基于异步事件管理订阅

问题描述 投票:0回答:1

我正在寻找一种简化方法,避免在管道内管理订阅。

一般想法买了我的代码:

this.start$ubject // waiting for users call to start
  .pipe(
    mergeMap(() => from(this.thirdPartyService.start())), // now i need to wait for smth to start
    tap(() => {
      // only after thirdPartyService started i can subscribe because prior to that they are undefined
      this.subscriptions.push(
        this.thirdPartyService.alfa$.subscribe(this.prizeTimerService.setTimerRunning$ubject),
        this.thirdPartyService.beta$.subscribe(this.prizeTimerService.setTimerRunning$ubject),
      );
    }),
  );

有什么办法可以解决?类似takeWhile,但要订阅吗?

asynchronous rxjs rxjs5 rxjs6
1个回答
0
投票

尝试这样:

// one subscription for all.
this.subscriptions.push(
  this.start$ubject.pipe(
    mergeMap(() => from(this.thirdPartyService.start())), // waiting start
    exhaustMap(() => merge( // listening on both of the streams
      this.thirdPartyService.alfa$,
      this.thirdPartyService.beta$,
    )),
  ).subscribe(),
);
© www.soinside.com 2019 - 2024. All rights reserved.