0

I want to create a queue of all the http requests that are being made simultaneously, But the Catch is that we need to update request of next api after every success request, issue is the queue we create saves all the requests as a observables and i can not find a way to update the request from that observable

Request Queue Service

import { Injectable } from '@angular/core';
import { HttpRequest, HttpHandler, HttpEvent, HttpEventType } from '@angular/common/http';
import { BehaviorSubject, Observable, ReplaySubject } from 'rxjs';
import { tap, catchError, switchMap } from 'rxjs/operators'; 

@Injectable({
  providedIn: 'root'
})
export class RequestQueueService {
  public queue: ReplaySubject<any>[] = [];
  updatedRespose = new BehaviorSubject<any>(null);

  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const requestQueueItem$ = new ReplaySubject<any>();
    const result$ = requestQueueItem$.pipe(
      switchMap(() => next.handle(request).pipe(
        tap(req => {
          if (req.type == HttpEventType.Response) {
            this.processNextRequest();
          }
        }),
        catchError(err => {
          this.processNextRequest();
          throw err;
        })
      ))
    );

    this.queue.push(requestQueueItem$);
    if (this.queue.length <= 1) {
      this.dispatchRequest();
    }

    return result$;
  }

  private processNextRequest(): void {
    if (this.queue && this.queue.length > 0) {
      this.queue.shift();
    }
    this.dispatchRequest();
  }

  private dispatchRequest(): void {
    if (this.queue.length > 0) {
      const nextSub$ = this.queue[0];
      nextSub$.next();
      nextSub$.complete();
    }
  }
}

Queue Interceptor

@Injectable()
export class QueueInterceptorService implements HttpInterceptor {
  constructor(
    private queueService: RequestQueueService
  ) { }

  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return this.queueService.intercept(request, next);
  }
}

used this answer for queue

Thanks in advance

1
  • Can you add examples of pseudocode to share what is your goal? Commented Jan 17, 2022 at 13:07

1 Answer 1

-1

Just use rxjs map operator. As angular http returns observables youcan map every one of them and put the response of each to the request of the next. if i get what you want right.

Sign up to request clarification or add additional context in comments.

2 Comments

Actually all requests are running parallelly (by User), but we are creating a queue of all requests and running them synchronously with interceptors, so request would already be added to to queue before we reach to response
If thats the case. Just know that you can't change anything when observable is already created. You must create another one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.