我正在寻找一种简化方法,避免在管道内管理订阅。
一般想法买了我的代码:
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
,但要订阅吗?
尝试这样:
// 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(),
);