Skip to content

Commit 7c1b7c7

Browse files
authored
Merge pull request #117 from mpalourdio/subscription_to_observable
Replace Subscription by Observable
2 parents 45f28cb + 998c565 commit 7c1b7c7

15 files changed

+1158
-1570
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## v6.0.0
4+
- Angular 8.x is now the default supported version.
5+
- The `subscription` in `NgHttpLoaderComponent` has completely been removed in favor of an `observable`. As there's no need to unsubscribe anymore, `ngOnDestroy` has been removed.
6+
- The `observable` initialization has been moved to `ngOnInit`.
7+
- The tests suite could fail (and succeed) in some unexpected ways. This was caused by some race conditions introduced in v5.0.0. Those are now (hopefully) fixed.
8+
39
## v5.1.0
410
- This release introduces 2 new options:
511
- **backdrop** (`true` by default): If set to `false`, the spinner background elements will remain clickable, without any background color.

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ The HTTP interceptor listens to all HTTP requests and shows a spinner / loader i
3434
| >=0.1.0 <0.4.0 | ^4.3.0 |
3535
| >=0.4.0 <1.0.0 | ^5.0.0 |
3636
| >=1.0.0 <3.2.0 | ^6.0.0 |
37-
| >=3.2.0 | ^7.0.0 |
37+
| >=3.2.0 <5.1.0 | ^7.0.0 |
38+
| >=6.0.0 | ^8.0.0 |
3839

3940
If you experience errors like below, **please double check the version you use.**
4041

browserslist

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
# This file is currently used by autoprefixer to adjust CSS to support the below specified browsers
1+
# This file is used by the build system to adjust CSS and JS output to support the specified browsers below.
22
# For additional information regarding the format and rule options, please see:
33
# https://github.com/browserslist/browserslist#queries
4-
#
5-
# For IE 9-11 support, please remove 'not' from the last line of the file and adjust as needed
4+
5+
# You can see what browsers were selected by your queries by running:
6+
# npx browserslist
67

78
> 0.5%
89
last 2 versions
910
Firefox ESR
1011
not dead
11-
not IE 9-11
12+
not IE 9-11 # For IE 9-11 support, remove 'not'.

package.json

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ng-http-loader",
3-
"version": "5.1.0",
3+
"version": "6.0.0",
44
"scripts": {
55
"ng": "ng",
66
"build": "ng build",
@@ -25,6 +25,7 @@
2525
"ng5",
2626
"ng6",
2727
"ng7",
28+
"ng8",
2829
"loader",
2930
"progressbar",
3031
"spinner",
@@ -36,44 +37,43 @@
3637
"url": "https://github.com/mpalourdio/ng-http-loader/issues"
3738
},
3839
"peerDependencies": {
39-
"@angular/common": "^7.0.0",
40-
"@angular/core": "^7.0.0",
41-
"rxjs": "^6.3.3"
40+
"@angular/common": "^8.0.0",
41+
"@angular/core": "^8.0.0",
42+
"rxjs": "^6.4.0"
4243
},
4344
"devDependencies": {
44-
"@angular-devkit/build-angular": "~0.13.0",
45-
"@angular-devkit/build-ng-packagr": "~0.13.0",
46-
"@angular/cli": "~7.3.0",
47-
"@angular/common": "^7.0.0",
48-
"@angular/compiler": "^7.0.0",
49-
"@angular/compiler-cli": "^7.0.0",
50-
"@angular/core": "^7.0.0",
51-
"@angular/language-service": "^7.0.0",
52-
"@angular/platform-browser": "^7.0.0",
53-
"@angular/platform-browser-dynamic": "^7.0.0",
54-
"@types/jasmine": "~2.8.8",
45+
"@angular-devkit/build-angular": "~0.800.0",
46+
"@angular-devkit/build-ng-packagr": "~0.800.0",
47+
"@angular/cli": "~8.0.0",
48+
"@angular/common": "^8.0.0",
49+
"@angular/compiler": "^8.0.0",
50+
"@angular/compiler-cli": "^8.0.0",
51+
"@angular/core": "^8.0.0",
52+
"@angular/language-service": "^8.0.0",
53+
"@angular/platform-browser": "^8.0.0",
54+
"@angular/platform-browser-dynamic": "^8.0.0",
55+
"@types/jasmine": "~3.3.8",
5556
"@types/jasminewd2": "~2.0.3",
5657
"@types/node": "~8.9.4",
57-
"codelyzer": "~4.5.0",
58-
"core-js": "^2.5.4",
58+
"codelyzer": "^5.0.0",
5959
"coveralls": "^3.0.1",
60-
"jasmine-core": "~2.99.1",
60+
"jasmine-core": "~3.4.0",
6161
"jasmine-spec-reporter": "~4.2.1",
62-
"karma": "~4.0.0",
62+
"karma": "~4.1.0",
6363
"karma-chrome-launcher": "~2.2.0",
6464
"karma-coverage-istanbul-reporter": "~2.0.1",
6565
"karma-firefox-launcher": "^1.1.0",
66-
"karma-jasmine": "~1.1.2",
67-
"karma-jasmine-html-reporter": "^0.2.2",
68-
"ng-packagr": "^4.2.0",
69-
"rxjs": "^6.3.3",
66+
"karma-jasmine": "~2.0.1",
67+
"karma-jasmine-html-reporter": "^1.4.0",
68+
"ng-packagr": "^5.1.0",
69+
"rxjs": "^6.4.0",
7070
"rxjs-tslint-rules": "^4.13.0",
7171
"ts-node": "~7.0.0",
72-
"tsickle": ">=0.34.0",
72+
"tsickle": "^0.35.0",
7373
"tslib": "^1.9.0",
74-
"tslint": "~5.11.0",
75-
"typescript": "~3.2.2",
76-
"zone.js": "~0.8.26"
74+
"tslint": "~5.15.0",
75+
"typescript": "~3.4.3",
76+
"zone.js": "~0.9.1"
7777
},
7878
"engines": {
7979
"node": ">= 8.9.0",

polyfills.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121
/** IE10 and IE11 requires the following for NgClass support on SVG elements */
2222
// import 'classlist.js'; // Run `npm install --save classlist.js`.
2323

24-
/** IE10 and IE11 requires the following for the Reflect API. */
25-
// import 'core-js/es6/reflect';
26-
2724
/**
2825
* Web Animations `@angular/platform-browser/animations`
2926
* Only required if AnimationBuilder is used within the application and using IE/Edge or Safari.
@@ -46,7 +43,7 @@
4643
*
4744
* (window as any).__Zone_disable_requestAnimationFrame = true; // disable patch requestAnimationFrame
4845
* (window as any).__Zone_disable_on_property = true; // disable patch onProperty such as onclick
49-
* (window as any).__zone_symbol__BLACK_LISTED_EVENTS = ['scroll', 'mousemove']; // disable patch specified eventNames
46+
* (window as any).__zone_symbol__UNPATCHED_EVENTS = ['scroll', 'mousemove']; // disable patch specified eventNames
5047
*
5148
* in IE/Edge developer tools, the addEventListener will also be wrapped by zone.js
5249
* with the following flag, it will bypass `zone.js` patch for IE/Edge

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

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
88
*/
99

10-
import { Component, Input, OnDestroy, OnInit } from '@angular/core';
11-
import { BehaviorSubject, merge, Observable, Subscription, timer } from 'rxjs';
12-
import { debounce, distinctUntilChanged, partition, switchMap, tap } from 'rxjs/operators';
10+
import { Component, Input, OnInit } from '@angular/core';
11+
import { merge, Observable, partition, timer } from 'rxjs';
12+
import { debounce, distinctUntilChanged, switchMap, tap } from 'rxjs/operators';
1313
import { PendingRequestsInterceptor } from '../services/pending-requests-interceptor.service';
1414
import { SpinnerVisibilityService } from '../services/spinner-visibility.service';
1515
import { Spinkit } from '../spinkits';
@@ -19,10 +19,10 @@ import { Spinkit } from '../spinkits';
1919
templateUrl: './ng-http-loader.component.html',
2020
styleUrls: ['./ng-http-loader.component.scss']
2121
})
22-
export class NgHttpLoaderComponent implements OnDestroy, OnInit {
22+
export class NgHttpLoaderComponent implements OnInit {
23+
2324
public spinkit = Spinkit;
24-
private _isVisible$ = new BehaviorSubject<boolean>(false);
25-
private subscriptions: Subscription;
25+
public isVisible$: Observable<boolean>;
2626
private visibleUntil = Date.now();
2727

2828
@Input() public backdrop = true;
@@ -38,37 +38,29 @@ export class NgHttpLoaderComponent implements OnDestroy, OnInit {
3838
@Input() public spinner = Spinkit.skWave;
3939

4040
constructor(private pendingRequestsInterceptor: PendingRequestsInterceptor, private spinnerVisibility: SpinnerVisibilityService) {
41-
const [showSpinner$, hideSpinner$] = partition((h: boolean) => h)(this.pendingRequestsInterceptor.pendingRequestsStatus$);
42-
43-
this.subscriptions = merge(
44-
this.pendingRequestsInterceptor.pendingRequestsStatus$.pipe(
45-
switchMap(() => showSpinner$.pipe(debounce(() => timer(this.debounceDelay))))
46-
),
47-
showSpinner$.pipe(
48-
switchMap(() => hideSpinner$.pipe(debounce(() => this.getVisibilityTimer$())))
49-
),
41+
}
42+
43+
private initIsvisibleObservable(): void {
44+
const [showSpinner$, hideSpinner$] = partition(this.pendingRequestsInterceptor.pendingRequestsStatus$, h => h);
45+
46+
this.isVisible$ = merge(
47+
this.pendingRequestsInterceptor.pendingRequestsStatus$
48+
.pipe(switchMap(() => showSpinner$.pipe(debounce(() => timer(this.debounceDelay))))),
49+
showSpinner$
50+
.pipe(switchMap(() => hideSpinner$.pipe(debounce(() => this.getVisibilityTimer$())))),
5051
this.spinnerVisibility.visibility$,
51-
)
52-
.pipe(
53-
distinctUntilChanged(),
54-
tap(h => this.updateExpirationDelay(h))
55-
)
56-
.subscribe(h => this._isVisible$.next(h));
52+
).pipe(
53+
distinctUntilChanged(),
54+
tap(h => this.updateExpirationDelay(h))
55+
);
5756
}
5857

5958
public ngOnInit(): void {
59+
this.initIsvisibleObservable();
6060
this.nullifySpinnerIfEntryComponentIsDefined();
6161
this.initFilters();
6262
}
6363

64-
public ngOnDestroy(): void {
65-
this.subscriptions.unsubscribe();
66-
}
67-
68-
get isVisible$(): Observable<boolean> {
69-
return this._isVisible$.asObservable();
70-
}
71-
7264
private nullifySpinnerIfEntryComponentIsDefined(): void {
7365
if (this.entryComponent) {
7466
this.spinner = null;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ describe('NgHttpLoaderComponent OnPush', () => {
4040
component = fixture.componentInstance;
4141
http = TestBed.get(HttpClient);
4242
httpMock = TestBed.get(HttpTestingController);
43+
fixture.detectChanges();
4344
});
4445

4546
it('should work as expected when the host component has ChangeDetectionStrategy.OnPush', fakeAsync(() => {

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

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
1111
import { By } from '@angular/platform-browser';
1212
import { BrowserDynamicTestingModule } from '@angular/platform-browser-dynamic/testing';
13-
import { BehaviorSubject } from 'rxjs';
13+
import { of } from 'rxjs';
1414
import { NgHttpLoaderComponent } from '../../lib/components/ng-http-loader.component';
1515
import { SkThreeBounceComponent } from '../../lib/components/sk-three-bounce/sk-three-bounce.component';
1616
import { SPINKIT_COMPONENTS } from '../../lib/spinkits';
@@ -32,12 +32,11 @@ describe('NgHttpLoaderComponentOutlet', () => {
3232
beforeEach(() => {
3333
fixture = TestBed.createComponent(NgHttpLoaderComponent);
3434
component = fixture.componentInstance;
35+
fixture.detectChanges();
3536
});
3637

3738
it('should be possible to specify an entryComponent', () => {
38-
spyOnProperty(component, 'isVisible$')
39-
.and.returnValue(new BehaviorSubject(true).asObservable());
40-
39+
component.isVisible$ = of(true);
4140
component.entryComponent = SkThreeBounceComponent;
4241
fixture.detectChanges();
4342

@@ -50,48 +49,36 @@ describe('NgHttpLoaderComponentOutlet', () => {
5049
});
5150

5251
it('should force [spinner] to null if [entryComponent] is defined', () => {
53-
spyOnProperty(component, 'isVisible$')
54-
.and.returnValue(new BehaviorSubject(true).asObservable());
55-
5652
component.spinner = 'spinner-name';
5753
component.entryComponent = SkThreeBounceComponent;
58-
fixture.detectChanges();
54+
component.ngOnInit();
5955

6056
expect(component.spinner).toBeNull();
6157
});
6258

6359
it('should correctly check [entryComponent] with empty string', () => {
64-
spyOnProperty(component, 'isVisible$')
65-
.and.returnValue(new BehaviorSubject(true).asObservable());
66-
6760
const spinnerName = 'spinner-name';
6861
component.spinner = spinnerName;
6962
component.entryComponent = '';
70-
fixture.detectChanges();
63+
component.ngOnInit();
7164

7265
expect(component.spinner).toBe(spinnerName);
7366
});
7467

7568
it('should correctly check [entryComponent] with null', () => {
76-
spyOnProperty(component, 'isVisible$')
77-
.and.returnValue(new BehaviorSubject(true).asObservable());
78-
7969
const spinnerName = 'spinner-name';
8070
component.spinner = spinnerName;
8171
component.entryComponent = null;
82-
fixture.detectChanges();
72+
component.ngOnInit();
8373

8474
expect(component.spinner).toBe(spinnerName);
8575
});
8676

8777
it('should correctly check [entryComponent] with undefined', () => {
88-
spyOnProperty(component, 'isVisible$')
89-
.and.returnValue(new BehaviorSubject(true).asObservable());
90-
9178
const spinnerName = 'spinner-name';
9279
component.spinner = spinnerName;
9380
component.entryComponent = undefined;
94-
fixture.detectChanges();
81+
component.ngOnInit();
9582

9683
expect(component.spinner).toBe(spinnerName);
9784
});

0 commit comments

Comments
 (0)