K
K
Konstantin2020-04-27 20:05:53
Angular
Konstantin, 2020-04-27 20:05:53

How to run a subject in interception after a while?

I have an intersepsh that catches outgoing requests and puts them in a list. As long as the list is not empty, the spinner is turned on. How to add requests to the list - only if the answer from them did not come within 2 seconds?

intercept(
    req: HttpRequest<any>,
    next: HttpHandler
  ): Observable<HttpEvent<any>> {
    this.requests.push(req);
    this.spinnerService.isLoading.next(true);

    return new Observable((observer) => {
      next
        .handle(req)
        .pipe(debounceTime(1000))
        .subscribe(
          (event) => {
            if (event instanceof HttpResponse) {
              this.removeRequest(req);
              observer.next(event);
            }
          },
          (err) => {
            this.removeRequest(req);
          }
        );
    });
  }


Adding requests happens here:

this.requests.push(req);
  this.spinnerService.isLoading.next(true);

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question