Skip to content

Commit

Permalink
Merge pull request #57 from IKatsuba/bugfix/timeout
Browse files Browse the repository at this point in the history
fix(ngx-ssr-timeout): timeout only after HttpSentEvent
  • Loading branch information
IKatsuba authored Jan 25, 2021
2 parents b8c2921 + 97f4c79 commit 746247b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion libs/ngx-ssr/timeout/src/lib/timeout.interceptor.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fakeAsync, flush, TestBed } from '@angular/core/testing';
import { TestBed } from '@angular/core/testing';
import {
HttpClientTestingModule,
HttpTestingController,
Expand Down
18 changes: 13 additions & 5 deletions libs/ngx-ssr/timeout/src/lib/timeout.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { Inject, Injectable, InjectionToken } from '@angular/core';
import {
HttpErrorResponse,
HttpEvent,
HttpEventType,
HttpHandler,
HttpInterceptor,
HttpRequest,
} from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError, timeout } from 'rxjs/operators';
import { NEVER, Observable, of, throwError } from 'rxjs';
import { catchError, startWith, switchMap, timeout } from 'rxjs/operators';

export interface TimeoutOptions {
timeout: number;
Expand All @@ -33,9 +34,16 @@ export class TimeoutInterceptor implements HttpInterceptor {
next: HttpHandler
): Observable<HttpEvent<unknown>> {
return next.handle(request).pipe(
this.timeoutOptions.timeout
? timeout(this.timeoutOptions.timeout)
: (source) => source,
switchMap((event) => {
if (event.type === HttpEventType.Sent && this.timeoutOptions.timeout) {
return NEVER.pipe(
startWith(event),
timeout(this.timeoutOptions.timeout)
);
}

return of(event);
}),
catchError((error) => {
if (error && error.name === 'TimeoutError') {
return throwError(
Expand Down

0 comments on commit 746247b

Please sign in to comment.