I want to create an interceptor (or any other solution that will affect all the services), that will hold the POST requests for ~300ms and in case there are new POST requests in that time gap, will perform switchMap
on them.
Something like that:
@Injectable()
export class DebounceInterceptorService implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (req.method != HttpMethod.Post) {
return next.handle(req);
} else {
return of().pipe(switchMap(() => next.handle(req)));
}
}
}
This doesn't work, but the agenda is to handle all requests in case the user double click a save button or any other case that shouldn't happen.
Edit:
That should work but the intercept()
function must have a returned value. Any thoughts on that would solve it
test: Subject<any>;
constructor() {
this.test.pipe(
debounceTime(5000),
switchMap(({ req, next }: { req: HttpRequest<any>; next: HttpHandler }) => next.handle(req))
);
}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (req.method != HttpMethod.Post) {
return next.handle(req);
} else {
this.test.next({ req, next });
}
}