Search code examples
javascriptangularrxjspollingtakeuntil

how to take an action after takeUntil rxjs function


So I am trying to have a boolean be true if the function running runs long enough to trigger the takenUntil function which is running on a timer.

Here is the code

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;
        return 0;
      }
      ),
    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)
      )),
      takeUntil(endtimer)
    ).subscribe();

  }

essentially i want a boolean to be set to true if the takeUntil does get fired.

timeout = true;

I have been looking at this stackoverflow post

Do some action after takeUntil

but things are not as clear as I want it to be.


Solution

  • takeUntil completes the observable, so to perform an action upon completion there are a couple places you can do that:

    1. in the completion handler:
    this.start.pipe(
        switchMap(() => timer(0, 5000).pipe(
            tap(() => console.log('Polling every 5s')),
            mergeMap(() => this.poll))
        ),
        takeUntil(endtimer)
    )
    .subscribe(
        next  => console.log('handling next value'),
        error => console.log('handling error'),
        ()    => this.timeout = true    // <--- this fires after observable completes
    );
    
    1. using the finalize operator:
    this.start.pipe(
        switchMap(() => timer(0, 5000).pipe(
            tap(() => console.log('Polling every 5s')),
            mergeMap(() => this.poll))
        ),
        takeUntil(endtimer),
        finalize(() => this.timeout = true)
    )
    .subscribe();
    

    Note: these solutions are not exactly what you are asking for. It's true they will fire when takeUntil fires, but they will also fire for any other reason the stream completes. I don't think that distinction matters in your case, but wanted to mention in in the context of the overall question.

    As Sang Dang mentioned in the comments, you could also approach it from the angle of "when the timer goes off (rather than what I've mentioned thus far, "when the observable completes"), which you can accomplish by simply adding a tap to your timer.

    const endtimer = timer(60000).pipe(
        tap(() => this.timeout = true)
    );