refractor: core module (#289)
* Shift services into their own folders. * Add comments. Fixes #288
This commit is contained in:
@@ -1,29 +1,48 @@
|
||||
/** Angular Imports */
|
||||
import { Injectable, EventEmitter } from '@angular/core';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
/**
|
||||
* Progress bar service.
|
||||
*/
|
||||
@Injectable()
|
||||
export class ProgressBarService {
|
||||
|
||||
/** Progress bar update event. */
|
||||
public updateProgressBar: EventEmitter<any>;
|
||||
/** Denotes the number of requests currently running. */
|
||||
private requestsRunning = 0;
|
||||
|
||||
/**
|
||||
* Initializes progress bar update event.
|
||||
*/
|
||||
constructor() {
|
||||
this.updateProgressBar = new EventEmitter();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of Http requests currently running.
|
||||
* @returns {number} Number of Http requests currently running.
|
||||
*/
|
||||
public getRequestsRunning(): number {
|
||||
return this.requestsRunning;
|
||||
}
|
||||
|
||||
public increase(): void {
|
||||
/**
|
||||
* Increments the number of Http requests currently running
|
||||
* and emits a progress bar update event `indeterminate` when the first request is run.
|
||||
*/
|
||||
public increase() {
|
||||
this.requestsRunning++;
|
||||
if (this.requestsRunning === 1) {
|
||||
this.updateProgressBar.emit('indeterminate');
|
||||
}
|
||||
}
|
||||
|
||||
public decrease(): void {
|
||||
/**
|
||||
* Decrements the number of Http requests currently running
|
||||
* and emits a progress bar update event `none` when no request is running.
|
||||
*/
|
||||
public decrease() {
|
||||
if (this.requestsRunning > 0) {
|
||||
this.requestsRunning--;
|
||||
if (this.requestsRunning === 0) {
|
||||
|
||||
@@ -1,16 +1,29 @@
|
||||
/** Angular Imports */
|
||||
import { Injectable } from '@angular/core';
|
||||
import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse} from '@angular/common/http';
|
||||
|
||||
/** rxjs Imports */
|
||||
import { Observable } from 'rxjs';
|
||||
import { tap, catchError } from 'rxjs/operators';
|
||||
|
||||
/** Custom Services */
|
||||
import { ProgressBarService } from './progress-bar.service';
|
||||
|
||||
/**
|
||||
* Http Request interceptor to start/stop loading the progress bar.
|
||||
*/
|
||||
@Injectable()
|
||||
export class ProgressInterceptor implements HttpInterceptor {
|
||||
|
||||
/**
|
||||
* @param {ProgressBarService} progressBarService Progress Bar Service.
|
||||
*/
|
||||
constructor(private progressBarService: ProgressBarService) { }
|
||||
|
||||
/**
|
||||
* Intercepts a Http request to start loading the progress bar for a pending request
|
||||
* and stop when a response or error is received.
|
||||
*/
|
||||
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
||||
this.progressBarService.increase();
|
||||
return next.handle(request)
|
||||
|
||||
Reference in New Issue
Block a user