refractor: core module (#289)

* Shift services into their own folders.
* Add comments.

Fixes #288
This commit is contained in:
Abhay Chawla
2018-08-11 21:52:44 +05:30
committed by GitHub
parent 1526235e2b
commit 90087bf248
37 changed files with 1330 additions and 1136 deletions
@@ -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)