refactor(aio): simplify/clarify some observables

This commit is contained in:
Ward Bell 2017-05-08 00:03:42 -07:00 committed by Pete Bacon Darwin
parent 2848f0499f
commit cf7689ea7b
4 changed files with 22 additions and 20 deletions

View File

@ -59,26 +59,30 @@ export class NavigationService {
*/
private fetchNavigationInfo(): Observable<NavigationResponse> {
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<NavigationResponse>) {
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<NavigationResponse>): Observable<NavigationViews> {
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: [] };

View File

@ -30,7 +30,7 @@ export class SearchService {
private worker: WebWorkerClient;
private ready: Observable<boolean>;
private resultsSubject = new Subject<SearchResults>();
get searchResults() { return this.resultsSubject.asObservable(); }
readonly searchResults = this.resultsSubject.asObservable();
constructor(private zone: NgZone) {}

View File

@ -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', () => {

View File

@ -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<string>();
private urlSubject = new ReplaySubject<string>(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 => {