简体   繁体   English

在轮询服务器的这段代码中,您会将 takeUntil RXJS 放在哪里?

[英]where would you put takeUntil RXJS in this code that polls a server?

So the code below has been developed off the answer I got in this stack overflow question.所以下面的代码是根据我在这个堆栈溢出问题中得到的答案开发的。

the code is designed to poll my server until either a condition on the server is true, or polling has occurred for a minute.该代码旨在轮询我的服务器,直到服务器上的条件为真或轮询发生一分钟。

I understand I can stop the polling after a minute using the takeUntil RXJS function.我知道我可以在一分钟后使用takeUntil RXJS function 停止轮询。 However, I have no idea where in the code I would put it in. As every place I put it in that I thought it would go, the code has errored out.但是,我不知道我会把它放在代码的哪个位置。因为我把它放在我认为它会 go 的每个地方,代码都出错了。

I am also using this tutorial from the learnrxjs website https://www.learnrxjs.io/learn-rxjs/operators/filtering/takeuntil我也在使用 learnrxjs 网站https://www.learnrxjs.io/learn-rxjs/operators/filtering/takeuntil的本教程

You'll notice the first line of the startastream() function is您会注意到startastream() function 的第一行是

const endtimer = timer(60000);

This is the condition that would fill the takeUntil() argument.这是填充takeUntil()参数的条件。 So takeUntil(endtimer)所以takeUntil(endtimer)

    start$ = this.http.get(environment.shochat_content_creator_set_valid_stream_start).pipe(
    tap(() => console.log('Stream start'))
  );
  poll$ = this.http.get(environment.check_if_stream_is_active_on_mux).pipe(
    tap(() => (this.streamready = true)),
    catchError(error => {
      console.log(error);
      return EMPTY;
    })
  );

  startastream(){
    const endtimer = timer(60000);
    this.streampollsubscription = this.start$.pipe(
      switchMap(() => timer(0, 5000).pipe(
        tap(() => console.log('Polling every 5s')),
        mergeMap(() => this.poll$)
      ))
    ).subscribe();

  }

You can simply place it in the pipe after switchMap:您可以简单地将其放在 switchMap 之后的 pipe 中:

    this.streampollsubscription = this.start$.pipe(
      switchMap(() => timer(0, 5000).pipe(
        tap(() => console.log('Polling every 5s')),
        mergeMap(() => this.poll$)
      )),
      takeUntil(endtimer)
    ).subscribe();

Check out this StackBlitz .看看这个StackBlitz

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

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