From cf7689ea7b9a4002c416b95f832b930ed7c829fe Mon Sep 17 00:00:00 2001 From: Ward Bell Date: Mon, 8 May 2017 00:03:42 -0700 Subject: [PATCH] refactor(aio): simplify/clarify some observables --- aio/src/app/navigation/navigation.service.ts | 25 ++++++++++++-------- aio/src/app/search/search.service.ts | 2 +- aio/src/app/shared/location.service.spec.ts | 2 ++ aio/src/app/shared/location.service.ts | 13 ++++------ 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/aio/src/app/navigation/navigation.service.ts b/aio/src/app/navigation/navigation.service.ts index d036516e48..974e6e834a 100644 --- a/aio/src/app/navigation/navigation.service.ts +++ b/aio/src/app/navigation/navigation.service.ts @@ -59,26 +59,30 @@ export class NavigationService { */ private fetchNavigationInfo(): Observable { const navigationInfo = this.http.get(navigationPath) - .map(res => res.json() as NavigationResponse) - .publishLast(); + .map(res => res.json() as NavigationResponse) + .publishLast(); navigationInfo.connect(); return navigationInfo; } private getVersionInfo(navigationInfo: Observable) { - const versionInfo = navigationInfo.map(response => response.__versionInfo).publishReplay(1); + const versionInfo = navigationInfo + .map(response => response.__versionInfo) + .publishLast(); versionInfo.connect(); return versionInfo; } private getNavigationViews(navigationInfo: Observable): Observable { - const navigationViews = navigationInfo.map(response => { - const views: NavigationViews = Object.assign({}, response); - Object.keys(views).forEach(key => { - if (key[0] === '_') { delete views[key]; } - }); - return views; - }).publishReplay(1); + const navigationViews = navigationInfo + .map(response => { + const views = Object.assign({}, response); + Object.keys(views).forEach(key => { + if (key[0] === '_') { delete views[key]; } + }); + return views as NavigationViews; + }) + .publishLast(); navigationViews.connect(); return navigationViews; } @@ -93,6 +97,7 @@ export class NavigationService { const currentNode = combineLatest( navigationViews.map(views => this.computeUrlToNavNodesMap(views)), this.location.currentPath, + (navMap, url) => { const urlKey = url.startsWith('api/') ? 'api' : url; return navMap[urlKey] || { view: '', url: urlKey, nodes: [] }; diff --git a/aio/src/app/search/search.service.ts b/aio/src/app/search/search.service.ts index 7a34109c24..f3840435e0 100644 --- a/aio/src/app/search/search.service.ts +++ b/aio/src/app/search/search.service.ts @@ -30,7 +30,7 @@ export class SearchService { private worker: WebWorkerClient; private ready: Observable; private resultsSubject = new Subject(); - get searchResults() { return this.resultsSubject.asObservable(); } + readonly searchResults = this.resultsSubject.asObservable(); constructor(private zone: NgZone) {} diff --git a/aio/src/app/shared/location.service.spec.ts b/aio/src/app/shared/location.service.spec.ts index a17921250a..72862e61e3 100644 --- a/aio/src/app/shared/location.service.spec.ts +++ b/aio/src/app/shared/location.service.spec.ts @@ -539,6 +539,8 @@ describe('LocationService', () => { beforeEach(() => { const gaService = injector.get(GaService); gaLocationChanged = gaService.locationChanged; + // execute currentPath observable so that gaLocationChanged is called + service.currentPath.subscribe(); }); it('should call locationChanged with initial URL', () => { diff --git a/aio/src/app/shared/location.service.ts b/aio/src/app/shared/location.service.ts index e88bc2cc57..2d41a03684 100644 --- a/aio/src/app/shared/location.service.ts +++ b/aio/src/app/shared/location.service.ts @@ -2,9 +2,8 @@ import { Injectable } from '@angular/core'; import { Location, PlatformLocation } from '@angular/common'; import { Observable } from 'rxjs/Observable'; -import { Subject } from 'rxjs/Subject'; +import { ReplaySubject } from 'rxjs/ReplaySubject'; import 'rxjs/add/operator/do'; -import 'rxjs/add/operator/publishReplay'; import { GaService } from 'app/shared/ga.service'; @@ -12,23 +11,19 @@ import { GaService } from 'app/shared/ga.service'; export class LocationService { private readonly urlParser = document.createElement('a'); - private urlSubject = new Subject(); + private urlSubject = new ReplaySubject(1); currentUrl = this.urlSubject - .map(url => this.stripSlashes(url)) - .publishReplay(1); + .map(url => this.stripSlashes(url)); currentPath = this.currentUrl .map(url => url.match(/[^?#]*/)[0]) // strip query and hash - .do(url => this.gaService.locationChanged(url)) - .publishReplay(1); + .do(url => this.gaService.locationChanged(url)); constructor( private gaService: GaService, private location: Location, private platformLocation: PlatformLocation) { - this.currentUrl.connect(); - this.currentPath.connect(); this.urlSubject.next(location.path(true)); this.location.subscribe(state => {