Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

feat: add functions that will wrap observables w/ skywait #39

Merged
merged 2 commits into from
Jul 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions src/app/public/modules/wait/wait.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import {
SkyWindowRefService
} from '@skyux/core';

import {
ReplaySubject
} from 'rxjs';

import {
SkyWaitFixturesModule
} from './fixtures/wait-fixtures.module';
Expand All @@ -21,6 +25,9 @@ import {
SkyWaitService
} from './wait.service';

const NO_OP_FUNC: () => void = () => {
};

describe('Wait service', () => {
let waitService: SkyWaitService;
let applicationRef: ApplicationRef;
Expand Down Expand Up @@ -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);
}));
});
25 changes: 23 additions & 2 deletions src/app/public/modules/wait/wait.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,20 @@ import {
} from '@skyux/core';

import {
SkyWaitPageComponent
} from './wait-page.component';
Observable
} from 'rxjs/Observable';

import {
SkyWaitPageAdapterService
} from './wait-page-adapter.service';

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
Expand Down Expand Up @@ -63,6 +70,20 @@ export class SkyWaitService {
}
}

public blockingWrap<T>(observable: Observable<T>): Observable<T> {
return Observable.defer(() => {
this.beginBlockingPageWait();
return observable.finally(() => this.endBlockingPageWait());
});
}

public nonBlockingWrap<T>(observable: Observable<T>): Observable<T> {
return Observable.defer(() => {
this.beginNonBlockingPageWait();
return observable.finally(() => this.endNonBlockingPageWait());
});
}

private setWaitComponentProperties(isBlocking: boolean): void {
if (isBlocking) {
SkyWaitService.waitComponent.hasBlockingWait = true;
Expand Down