Skip to content

Commit 5a6b6d2

Browse files
committed
Rename PendingInterceptorService to PendingRequestsInterceptor && _isSpinnerVisible$ to _isVisible$
1 parent 9234a80 commit 5a6b6d2

11 files changed

+160
-158
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# Changelog
22

33
## v5.0.0
4-
Prior to this release, `NgHttpLoaderComponent#isSpinnerVisible` was a boolean. Because of unexpected behaviors when a component with `ChangeDetectionStrategy.OnPush` performed HTTP requests, it is now an `Observable<boolean>`.
4+
- Prior to this release, `NgHttpLoaderComponent#isSpinnerVisible` was a boolean. Because of unexpected behaviors when a component with `ChangeDetectionStrategy.OnPush` performed HTTP requests, it is now an `Observable<boolean>` and has been renamed to `NgHttpLoaderComponent#isVisible$`.
55
The associated template now uses an `async pipe` in order to perform the show/hide logic.
66

7+
- `PendingInterceptorService` has been renamed to `PendingRequestsInterceptor`.
8+
79
## v4.0.0
810
- `HttpClientModule` has been removed from imports.
911
This caused some issues when external modules were imported in an application, and those modules registered their own HTTP interceptors. See [this issue](https://github.com/angular/angular/issues/20575) for reference.

src/lib/components/ng-http-loader.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div id="spinner" *ngIf="isSpinnerVisible$ | async">
1+
<div id="spinner" *ngIf="isVisible$ | async">
22

33
<ng-container *ngComponentOutlet="entryComponent"></ng-container>
44

src/lib/components/ng-http-loader.component.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import { Component, Input, OnDestroy, OnInit } from '@angular/core';
1111
import { BehaviorSubject, merge, Observable, Subscription, timer } from 'rxjs';
1212
import { debounce, distinctUntilChanged, partition, switchMap, tap } from 'rxjs/operators';
13-
import { PendingInterceptorService } from '../services/pending-interceptor.service';
13+
import { PendingRequestsInterceptor } from '../services/pending-requests-interceptor.service';
1414
import { SpinnerVisibilityService } from '../services/spinner-visibility.service';
1515
import { Spinkit } from '../spinkits';
1616

@@ -21,7 +21,7 @@ import { Spinkit } from '../spinkits';
2121
})
2222
export class NgHttpLoaderComponent implements OnDestroy, OnInit {
2323
public spinkit = Spinkit;
24-
private _isSpinnerVisible$ = new BehaviorSubject<boolean>(false);
24+
private _isVisible$ = new BehaviorSubject<boolean>(false);
2525
private subscriptions: Subscription;
2626
private visibleUntil = Date.now();
2727

@@ -44,23 +44,23 @@ export class NgHttpLoaderComponent implements OnDestroy, OnInit {
4444
@Input()
4545
public entryComponent: any = null;
4646

47-
constructor(private pendingInterceptorService: PendingInterceptorService, private spinnerVisibilityService: SpinnerVisibilityService) {
48-
const [showSpinner$, hideSpinner$] = partition((h: boolean) => h)(this.pendingInterceptorService.pendingRequestsStatus$);
47+
constructor(private pendingRequestsInterceptor: PendingRequestsInterceptor, private spinnerVisibility: SpinnerVisibilityService) {
48+
const [showSpinner$, hideSpinner$] = partition((h: boolean) => h)(this.pendingRequestsInterceptor.pendingRequestsStatus$);
4949

5050
this.subscriptions = merge(
51-
this.pendingInterceptorService.pendingRequestsStatus$.pipe(
51+
this.pendingRequestsInterceptor.pendingRequestsStatus$.pipe(
5252
switchMap(() => showSpinner$.pipe(debounce(() => timer(this.debounceDelay))))
5353
),
5454
showSpinner$.pipe(
5555
switchMap(() => hideSpinner$.pipe(debounce(() => this.getVisibilityTimer$())))
5656
),
57-
this.spinnerVisibilityService.visibility$,
57+
this.spinnerVisibility.visibility$,
5858
)
5959
.pipe(
6060
distinctUntilChanged(),
61-
tap(h => this.updateVisibilityExpiration(h))
61+
tap(h => this.updateExpirationDelay(h))
6262
)
63-
.subscribe(h => this._isSpinnerVisible$.next(h));
63+
.subscribe(h => this._isVisible$.next(h));
6464
}
6565

6666
ngOnInit(): void {
@@ -72,8 +72,8 @@ export class NgHttpLoaderComponent implements OnDestroy, OnInit {
7272
this.subscriptions.unsubscribe();
7373
}
7474

75-
get isSpinnerVisible$(): Observable<boolean> {
76-
return this._isSpinnerVisible$.asObservable();
75+
get isVisible$(): Observable<boolean> {
76+
return this._isVisible$.asObservable();
7777
}
7878

7979
private nullifySpinnerIfEntryComponentIsDefined(): void {
@@ -95,7 +95,7 @@ export class NgHttpLoaderComponent implements OnDestroy, OnInit {
9595

9696
if (!!this.filteredUrlPatterns.length) {
9797
this.filteredUrlPatterns.forEach(e =>
98-
this.pendingInterceptorService.filteredUrlPatterns.push(new RegExp(e))
98+
this.pendingRequestsInterceptor.filteredUrlPatterns.push(new RegExp(e))
9999
);
100100
}
101101
}
@@ -104,17 +104,17 @@ export class NgHttpLoaderComponent implements OnDestroy, OnInit {
104104
if (!(this.filteredMethods instanceof Array)) {
105105
throw new TypeError('`filteredMethods` must be an array.');
106106
}
107-
this.pendingInterceptorService.filteredMethods = this.filteredMethods;
107+
this.pendingRequestsInterceptor.filteredMethods = this.filteredMethods;
108108
}
109109

110110
private initFilteredHeaders(): void {
111111
if (!(this.filteredHeaders instanceof Array)) {
112112
throw new TypeError('`filteredHeaders` must be an array.');
113113
}
114-
this.pendingInterceptorService.filteredHeaders = this.filteredHeaders;
114+
this.pendingRequestsInterceptor.filteredHeaders = this.filteredHeaders;
115115
}
116116

117-
private updateVisibilityExpiration(showSpinner: boolean): void {
117+
private updateExpirationDelay(showSpinner: boolean): void {
118118
if (showSpinner) {
119119
this.visibleUntil = Date.now() + this.minDuration;
120120
}

src/lib/ng-http-loader.module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import { CommonModule } from '@angular/common';
1111
import { ModuleWithProviders, NgModule } from '@angular/core';
1212
import { NgHttpLoaderComponent } from './components/ng-http-loader.component';
13-
import { PendingInterceptorServiceProvider } from './services/pending-interceptor.service';
13+
import { PendingRequestsInterceptorProvider } from './services/pending-requests-interceptor.service';
1414
import { SPINKIT_COMPONENTS } from './spinkits';
1515

1616
@NgModule({
@@ -31,7 +31,7 @@ export class NgHttpLoaderModule {
3131
return {
3232
ngModule: NgHttpLoaderModule,
3333
providers: [
34-
PendingInterceptorServiceProvider,
34+
PendingRequestsInterceptorProvider,
3535
],
3636
};
3737
}

src/lib/services/pending-interceptor.service.ts renamed to src/lib/services/pending-requests-interceptor.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { catchError, finalize, map } from 'rxjs/operators';
2222
@Injectable({
2323
providedIn: 'root'
2424
})
25-
export class PendingInterceptorService implements HttpInterceptor {
25+
export class PendingRequestsInterceptor implements HttpInterceptor {
2626
private _pendingRequests = 0;
2727
private _pendingRequestsStatus$ = new ReplaySubject<boolean>(1);
2828
private _filteredUrlPatterns: RegExp[] = [];
@@ -110,8 +110,8 @@ export class PendingInterceptorService implements HttpInterceptor {
110110
}
111111
}
112112

113-
export const PendingInterceptorServiceProvider: ExistingProvider[] = [{
113+
export const PendingRequestsInterceptorProvider: ExistingProvider[] = [{
114114
provide: HTTP_INTERCEPTORS,
115-
useExisting: PendingInterceptorService,
115+
useExisting: PendingRequestsInterceptor,
116116
multi: true
117117
}];

src/lib/services/spinner-visibility.service.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,28 @@
99

1010
import { Injectable } from '@angular/core';
1111
import { Observable, ReplaySubject } from 'rxjs';
12-
import { PendingInterceptorService } from './pending-interceptor.service';
12+
import { PendingRequestsInterceptor } from './pending-requests-interceptor.service';
1313

1414
@Injectable({
1515
providedIn: 'root'
1616
})
1717
export class SpinnerVisibilityService {
1818
private _visibility$ = new ReplaySubject<boolean>(1);
1919

20-
constructor(private pendingInterceptorService: PendingInterceptorService) {
20+
constructor(private pendingRequestsInterceptor: PendingRequestsInterceptor) {
2121
}
2222

2323
get visibility$(): Observable<boolean> {
2424
return this._visibility$.asObservable();
2525
}
2626

2727
public show(): void {
28-
this.pendingInterceptorService.forceByPass = true;
28+
this.pendingRequestsInterceptor.forceByPass = true;
2929
this._visibility$.next(true);
3030
}
3131

3232
public hide(): void {
3333
this._visibility$.next(false);
34-
this.pendingInterceptorService.forceByPass = false;
34+
this.pendingRequestsInterceptor.forceByPass = false;
3535
}
3636
}

src/public_api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export * from './lib/components/sk-wave/sk-wave.component';
1313
export * from './lib/components/ng-http-loader.component';
1414
export * from './lib/components/abstract.loader.component';
1515

16-
export * from './lib/services/pending-interceptor.service';
16+
export * from './lib/services/pending-requests-interceptor.service';
1717
export * from './lib/services/spinner-visibility.service';
1818

1919
export * from './lib/ng-http-loader.module';

src/test/components/ng-http-loader.component.outlet.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { BrowserDynamicTestingModule } from '@angular/platform-browser-dynamic/t
1414
import { BehaviorSubject } from 'rxjs';
1515
import { NgHttpLoaderComponent } from '../../lib/components/ng-http-loader.component';
1616
import { SkThreeBounceComponent } from '../../lib/components/sk-three-bounce/sk-three-bounce.component';
17-
import { PendingInterceptorServiceProvider } from '../../lib/services/pending-interceptor.service';
17+
import { PendingRequestsInterceptorProvider } from '../../lib/services/pending-requests-interceptor.service';
1818
import { SPINKIT_COMPONENTS } from '../../lib/spinkits';
1919

2020
describe('NgHttpLoaderComponentOutlet', () => {
@@ -25,7 +25,7 @@ describe('NgHttpLoaderComponentOutlet', () => {
2525
TestBed.configureTestingModule({
2626
declarations: [NgHttpLoaderComponent, ...SPINKIT_COMPONENTS],
2727
imports: [HttpClientTestingModule],
28-
providers: [PendingInterceptorServiceProvider]
28+
providers: [PendingRequestsInterceptorProvider]
2929
})
3030
.overrideModule(BrowserDynamicTestingModule, {
3131
set: { entryComponents: [SkThreeBounceComponent] }
@@ -39,7 +39,7 @@ describe('NgHttpLoaderComponentOutlet', () => {
3939
});
4040

4141
it('should be possible to specify an entryComponent', () => {
42-
spyOnProperty(component, 'isSpinnerVisible$')
42+
spyOnProperty(component, 'isVisible$')
4343
.and.returnValue(new BehaviorSubject(true).asObservable());
4444

4545
component.entryComponent = SkThreeBounceComponent;
@@ -54,7 +54,7 @@ describe('NgHttpLoaderComponentOutlet', () => {
5454
});
5555

5656
it('should force [spinner] to null if [entryComponent] is defined', () => {
57-
spyOnProperty(component, 'isSpinnerVisible$')
57+
spyOnProperty(component, 'isVisible$')
5858
.and.returnValue(new BehaviorSubject(true).asObservable());
5959

6060
component.spinner = 'whatever';

0 commit comments

Comments
 (0)