简体   繁体   English

当 observable 需要更多时间时,未调用 switchMap 的订阅

[英]Subscription for switchMap not called when observable takes more time

startPolling() {
timer(1, 5000).pipe(
  switchMap(() => this.gatewayService.get(this.id)),
  retry(),
  share(),
  takeUntil(this.stopPolling)
).subscribe((gateway) => {
  this.status = gateway.status;
  this.stopPollingIfImageDownloaded();
});

in the above code subscription doesnt work if gatewayService.get call takes more than 5 sec.如果 gatewayService.get 调用超过 5 秒,则上述代码订阅不起作用。 So the switchMap cancelling the prev subscription.所以switchMap取消了prev订阅。

Any solution to this problem.这个问题的任何解决方案。

Basically i want to do polling for status and the call may take time基本上我想对状态进行轮询,通话可能需要时间

You could either use mergeMap or exhaustMap .您可以使用mergeMapexhaustMap

The difference is that:不同之处在于:

  • mergeMap will subscribe every 5 seconds without unsubscribing (cancelling) the previous API call. mergeMap将每 5 秒订阅一次,而不会取消订阅(取消)之前的 API 调用。 This means you could have many concurrent subscriptions.这意味着您可以有许多并发订阅。
  • exhaustMap will not subscribe again, unless the current API call has finished, but it also will not cancel the running API call.除非当前 API 调用已完成, exhaustMap不会再次订阅,但它也不会取消正在运行的 API 调用。 In this case, you may need to wait up to 5 seconds after a successful response until it re-subscribes.在这种情况下,您可能需要在成功响应后最多等待 5 秒,直到它重新订阅。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM