Skip to content

Commit

Permalink
fix(interceptor): Fix #14
Browse files Browse the repository at this point in the history
  • Loading branch information
kKen94 committed Aug 7, 2021
1 parent 977815b commit 5faca89
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 116 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"semantic-release": "semantic-release",
"start:demo": "ng serve demo --open --port=3310",
"build": "yarn run build:lib && yarn run build:demo",
"build:lib": "ng build lib --prod && yarn run copy:files",
"build:demo": "ng build demo --prod --base-href /ngx-progress/",
"build:lib": "ng build lib -c production && yarn run copy:files",
"build:demo": "ng build demo -c production --base-href /ngx-progress/",
"publish:lib": "npx semantic-release",
"publish:demo": "npx angular-cli-ghpages --dir=./dist/demo",
"copy:files": "yarn run copy:readme && yarn run copy:license && yarn run copy:conduct",
Expand Down
90 changes: 45 additions & 45 deletions projects/lib/src/lib/bar/bar.service.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
import { Injectable } from '@angular/core';

@Injectable({ providedIn: 'root' })
export class BarService {
value = 0;
progress = 0;
interval!: number;

start(): void {
this.interval = setInterval(() => {
this.value += 0.015;
this.progress =
Math.round((Math.atan(this.value) / (Math.PI / 2)) * 100 * 1000) / 1000;
if (this.progress >= 100) {
clearInterval(this.interval);
}
}, 150);
}

stop(): void {
clearInterval(this.interval);
}

async complete(): Promise<unknown> {
this.set(99);
return new Promise(resolve => {
setTimeout(() => {
clearInterval(this.interval);
this.progress = 0;
this.value = 0;
resolve();
}, 200);
});
}

set(value: number): void {
this.progress = value;
this.value = Math.tan((value / 100) * (Math.PI / 2));
}

increment(value: number): void {
this.progress += value;
this.value = Math.tan((this.progress / 100) * (Math.PI / 2));
}
}
import { Injectable } from '@angular/core';

@Injectable({ providedIn: 'root' })
export class BarService {
value = 0;
progress = 0;
interval!: number;

start(): void {
this.interval = setInterval(() => {
this.value += 0.015;
this.progress =
Math.round((Math.atan(this.value) / (Math.PI / 2)) * 100 * 1000) / 1000;
if (this.progress >= 100) {
clearInterval(this.interval);
}
}, 150);
}

stop(): void {
clearInterval(this.interval);
}

async complete(): Promise<unknown> {
this.set(99);
return new Promise<void>(resolve => {
setTimeout(() => {
clearInterval(this.interval);
this.progress = 0;
this.value = 0;
resolve();
}, 200);
});
}

set(value: number): void {
this.progress = value;
this.value = Math.tan((value / 100) * (Math.PI / 2));
}

increment(value: number): void {
this.progress += value;
this.value = Math.tan((this.progress / 100) * (Math.PI / 2));
}
}
138 changes: 69 additions & 69 deletions projects/lib/src/lib/ngx-progress.interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,69 @@
import {
HttpEvent,
HttpHandler,
HttpInterceptor,
HttpRequest,
HttpResponse,
} from '@angular/common/http';
import { Inject, Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
import { NgxProgressService } from './ngx-progress.service';
import { WHITELIST } from './symbols';

@Injectable()
export class NgxProgressInterceptor implements HttpInterceptor {
// tslint:disable-next-line:variable-name
#_regexUrl: RegExp[] = [];
/**
* Is done the mapping from string to regex on variable assignment
* @param patterns: the whitelist
*/
set regexUrl(patterns: string[]) {
this.#_regexUrl = patterns.map(p => new RegExp(p));
}

constructor(
private readonly progressService: NgxProgressService,
@Inject(WHITELIST) private readonly whitelist: string[],
) {
this.regexUrl = whitelist;
}

intercept(
request: HttpRequest<any>,
next: HttpHandler,
): Observable<HttpEvent<any>> {
for (const regexUrlItem of this.#_regexUrl) {
if (regexUrlItem.test(request.url)) {
return next.handle(request);
}
}
this.progressService.start();
return next.handle(request).pipe(
map(event => {
if (event instanceof HttpResponse) {
this.progressService.end();
}
return event;
}),
);
}
}

@Injectable()
export class ErrorInterceptor implements HttpInterceptor {
constructor(private readonly progressService: NgxProgressService) {}

intercept(
request: HttpRequest<any>,
next: HttpHandler,
): Observable<HttpEvent<any>> {
return next.handle(request).pipe(
catchError(err => {
this.progressService.terminate();
return of(err);
}),
);
}
}
import {
HttpEvent,
HttpHandler,
HttpInterceptor,
HttpRequest,
HttpResponse,
} from '@angular/common/http';
import { Inject, Injectable } from '@angular/core';
import { Observable, of, throwError } from "rxjs";
import { catchError, map } from 'rxjs/operators';
import { NgxProgressService } from './ngx-progress.service';
import { WHITELIST } from './symbols';

@Injectable()
export class NgxProgressInterceptor implements HttpInterceptor {
// tslint:disable-next-line:variable-name
#_regexUrl: RegExp[] = [];
/**
* Is done the mapping from string to regex on variable assignment
* @param patterns: the whitelist
*/
set regexUrl(patterns: string[]) {
this.#_regexUrl = patterns.map(p => new RegExp(p));
}

constructor(
private readonly progressService: NgxProgressService,
@Inject(WHITELIST) private readonly whitelist: string[],
) {
this.regexUrl = whitelist;
}

intercept(
request: HttpRequest<any>,
next: HttpHandler,
): Observable<HttpEvent<any>> {
for (const regexUrlItem of this.#_regexUrl) {
if (regexUrlItem.test(request.url)) {
return next.handle(request);
}
}
this.progressService.start();
return next.handle(request).pipe(
map(event => {
if (event instanceof HttpResponse) {
this.progressService.end();
}
return event;
}),
);
}
}

@Injectable()
export class ErrorInterceptor implements HttpInterceptor {
constructor(private readonly progressService: NgxProgressService) {}

intercept(
request: HttpRequest<any>,
next: HttpHandler,
): Observable<HttpEvent<any>> {
return next.handle(request).pipe(
catchError(err => {
this.progressService.terminate();
return throwError(err);
}),
);
}
}

0 comments on commit 5faca89

Please sign in to comment.