From 030eff453b23e510dc18eb1a25caae2f145c5fb4 Mon Sep 17 00:00:00 2001 From: "Colby M. White" Date: Tue, 23 Jul 2019 17:07:06 -0500 Subject: [PATCH 1/2] feat: add functions that will wrap observables w/ skywait --- .../public/modules/wait/wait.service.spec.ts | 85 +++++++++++++++++++ src/app/public/modules/wait/wait.service.ts | 27 +++++- 2 files changed, 110 insertions(+), 2 deletions(-) diff --git a/src/app/public/modules/wait/wait.service.spec.ts b/src/app/public/modules/wait/wait.service.spec.ts index 4b9cb6a7..c24b53be 100644 --- a/src/app/public/modules/wait/wait.service.spec.ts +++ b/src/app/public/modules/wait/wait.service.spec.ts @@ -13,6 +13,10 @@ import { SkyWindowRefService } from '@skyux/core'; +import { + ReplaySubject +} from 'rxjs'; + import { SkyWaitFixturesModule } from './fixtures/wait-fixtures.module'; @@ -21,6 +25,9 @@ import { SkyWaitService } from './wait.service'; +const NO_OP_FUNC: () => void = () => { +}; + describe('Wait service', () => { let waitService: SkyWaitService; let applicationRef: ApplicationRef; @@ -212,4 +219,82 @@ describe('Wait service', () => { verifyNonBlockingPageWaitExists(false); verifyBlockingPageWaitExists(false); })); + + it('should wrap with blocking wait when the given observable is hot', fakeAsync(() => { + const subject = new ReplaySubject(); + waitService.blockingWrap(subject.asObservable()).subscribe(NO_OP_FUNC); + subject.next('A'); + tick(); + applicationRef.tick(); + verifyBlockingPageWaitExists(true); + subject.complete(); + tick(); + applicationRef.tick(); + verifyBlockingPageWaitExists(false); + })); + + it('should not wrap with blocking wait when the given observable is cold', fakeAsync(() => { + const subject = new ReplaySubject(); + waitService.blockingWrap(subject.asObservable()); + subject.next('A'); + tick(); + applicationRef.tick(); + verifyBlockingPageWaitExists(false); + subject.complete(); + tick(); + applicationRef.tick(); + verifyBlockingPageWaitExists(false); + })); + + it('should wrap with blocking wait when the given observable throws error', fakeAsync(() => { + const subject = new ReplaySubject(); + waitService.blockingWrap(subject.asObservable()).subscribe(NO_OP_FUNC, NO_OP_FUNC); + subject.next('A'); + tick(); + applicationRef.tick(); + verifyBlockingPageWaitExists(true); + subject.error('error'); + tick(); + applicationRef.tick(); + verifyBlockingPageWaitExists(false); + })); + + it('should wrap with nonblocking wait when the given observable is hot', fakeAsync(() => { + const subject = new ReplaySubject(); + waitService.nonBlockingWrap(subject.asObservable()).subscribe(NO_OP_FUNC); + subject.next('A'); + tick(); + applicationRef.tick(); + verifyNonBlockingPageWaitExists(true); + subject.complete(); + tick(); + applicationRef.tick(); + verifyNonBlockingPageWaitExists(false); + })); + + it('should not wrap with nonblocking wait when the given observable is cold', fakeAsync(() => { + const subject = new ReplaySubject(); + waitService.nonBlockingWrap(subject.asObservable()); + subject.next('A'); + tick(); + applicationRef.tick(); + verifyNonBlockingPageWaitExists(false); + subject.complete(); + tick(); + applicationRef.tick(); + verifyNonBlockingPageWaitExists(false); + })); + + it('should wrap with nonblocking wait when the given observable throws error', fakeAsync(() => { + const subject = new ReplaySubject(); + waitService.nonBlockingWrap(subject.asObservable()).subscribe(NO_OP_FUNC, NO_OP_FUNC); + subject.next('A'); + tick(); + applicationRef.tick(); + verifyNonBlockingPageWaitExists(true); + subject.error('error'); + tick(); + applicationRef.tick(); + verifyNonBlockingPageWaitExists(false); + })); }); diff --git a/src/app/public/modules/wait/wait.service.ts b/src/app/public/modules/wait/wait.service.ts index f9d6d485..5e60ac41 100644 --- a/src/app/public/modules/wait/wait.service.ts +++ b/src/app/public/modules/wait/wait.service.ts @@ -9,13 +9,22 @@ import { } from '@skyux/core'; import { - SkyWaitPageComponent -} from './wait-page.component'; + defer, + Observable +} from 'rxjs'; + +import { + finalize +} from 'rxjs/operators'; import { SkyWaitPageAdapterService } from './wait-page-adapter.service'; +import { + SkyWaitPageComponent +} from './wait-page.component'; + // Need to add the following to classes which contain static methods. // See: https://github.com/ng-packagr/ng-packagr/issues/641 // @dynamic @@ -63,6 +72,20 @@ export class SkyWaitService { } } + public blockingWrap(observable: Observable): Observable { + return defer(() => { + this.beginBlockingPageWait(); + return observable.pipe(finalize(() => this.endBlockingPageWait())); + }); + } + + public nonBlockingWrap(observable: Observable): Observable { + return defer(() => { + this.beginNonBlockingPageWait(); + return observable.pipe(finalize(() => this.endNonBlockingPageWait())); + }); + } + private setWaitComponentProperties(isBlocking: boolean): void { if (isBlocking) { SkyWaitService.waitComponent.hasBlockingWait = true; From 79041901cd2f7faf4c35759fa5619f13650986ed Mon Sep 17 00:00:00 2001 From: "Colby M. White" Date: Thu, 25 Jul 2019 17:46:56 -0500 Subject: [PATCH 2/2] fix: add backwards compat w/ rxjs --- src/app/public/modules/wait/wait.service.ts | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/app/public/modules/wait/wait.service.ts b/src/app/public/modules/wait/wait.service.ts index 5e60ac41..3bd84bff 100644 --- a/src/app/public/modules/wait/wait.service.ts +++ b/src/app/public/modules/wait/wait.service.ts @@ -9,13 +9,8 @@ import { } from '@skyux/core'; import { - defer, Observable -} from 'rxjs'; - -import { - finalize -} from 'rxjs/operators'; +} from 'rxjs/Observable'; import { SkyWaitPageAdapterService @@ -25,6 +20,9 @@ import { SkyWaitPageComponent } from './wait-page.component'; +import 'rxjs/add/operator/finally'; +import 'rxjs/add/observable/defer'; + // Need to add the following to classes which contain static methods. // See: https://github.com/ng-packagr/ng-packagr/issues/641 // @dynamic @@ -73,16 +71,16 @@ export class SkyWaitService { } public blockingWrap(observable: Observable): Observable { - return defer(() => { + return Observable.defer(() => { this.beginBlockingPageWait(); - return observable.pipe(finalize(() => this.endBlockingPageWait())); + return observable.finally(() => this.endBlockingPageWait()); }); } public nonBlockingWrap(observable: Observable): Observable { - return defer(() => { + return Observable.defer(() => { this.beginNonBlockingPageWait(); - return observable.pipe(finalize(() => this.endNonBlockingPageWait())); + return observable.finally(() => this.endNonBlockingPageWait()); }); }