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> { private fetchNavigationInfo(): Observable<NavigationResponse> {
const navigationInfo = this.http.get(navigationPath) const navigationInfo = this.http.get(navigationPath)
.map(res => res.json() as NavigationResponse) .map(res => res.json() as NavigationResponse)
.publishLast(); .publishLast();
navigationInfo.connect(); navigationInfo.connect();
return navigationInfo; return navigationInfo;
} }
private getVersionInfo(navigationInfo: Observable<NavigationResponse>) { private getVersionInfo(navigationInfo: Observable<NavigationResponse>) {
const versionInfo = navigationInfo.map(response => response.__versionInfo).publishReplay(1); const versionInfo = navigationInfo
.map(response => response.__versionInfo)
.publishLast();
versionInfo.connect(); versionInfo.connect();
return versionInfo; return versionInfo;
} }
private getNavigationViews(navigationInfo: Observable<NavigationResponse>): Observable<NavigationViews> { private getNavigationViews(navigationInfo: Observable<NavigationResponse>): Observable<NavigationViews> {
const navigationViews = navigationInfo.map(response => { const navigationViews = navigationInfo
const views: NavigationViews = Object.assign({}, response); .map(response => {
Object.keys(views).forEach(key => { const views = Object.assign({}, response);
if (key[0] === '_') { delete views[key]; } Object.keys(views).forEach(key => {
}); if (key[0] === '_') { delete views[key]; }
return views; });
}).publishReplay(1); return views as NavigationViews;
})
.publishLast();
navigationViews.connect(); navigationViews.connect();
return navigationViews; return navigationViews;
} }
@ -93,6 +97,7 @@ export class NavigationService {
const currentNode = combineLatest( const currentNode = combineLatest(
navigationViews.map(views => this.computeUrlToNavNodesMap(views)), navigationViews.map(views => this.computeUrlToNavNodesMap(views)),
this.location.currentPath, this.location.currentPath,
(navMap, url) => { (navMap, url) => {
const urlKey = url.startsWith('api/') ? 'api' : url; const urlKey = url.startsWith('api/') ? 'api' : url;
return navMap[urlKey] || { view: '', url: urlKey, nodes: [] }; return navMap[urlKey] || { view: '', url: urlKey, nodes: [] };

View File

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

View File

@ -539,6 +539,8 @@ describe('LocationService', () => {
beforeEach(() => { beforeEach(() => {
const gaService = injector.get(GaService); const gaService = injector.get(GaService);
gaLocationChanged = gaService.locationChanged; gaLocationChanged = gaService.locationChanged;
// execute currentPath observable so that gaLocationChanged is called
service.currentPath.subscribe();
}); });
it('should call locationChanged with initial URL', () => { 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 { Location, PlatformLocation } from '@angular/common';
import { Observable } from 'rxjs/Observable'; import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject'; import { ReplaySubject } from 'rxjs/ReplaySubject';
import 'rxjs/add/operator/do'; import 'rxjs/add/operator/do';
import 'rxjs/add/operator/publishReplay';
import { GaService } from 'app/shared/ga.service'; import { GaService } from 'app/shared/ga.service';
@ -12,23 +11,19 @@ import { GaService } from 'app/shared/ga.service';
export class LocationService { export class LocationService {
private readonly urlParser = document.createElement('a'); private readonly urlParser = document.createElement('a');
private urlSubject = new Subject<string>(); private urlSubject = new ReplaySubject<string>(1);
currentUrl = this.urlSubject currentUrl = this.urlSubject
.map(url => this.stripSlashes(url)) .map(url => this.stripSlashes(url));
.publishReplay(1);
currentPath = this.currentUrl currentPath = this.currentUrl
.map(url => url.match(/[^?#]*/)[0]) // strip query and hash .map(url => url.match(/[^?#]*/)[0]) // strip query and hash
.do(url => this.gaService.locationChanged(url)) .do(url => this.gaService.locationChanged(url));
.publishReplay(1);
constructor( constructor(
private gaService: GaService, private gaService: GaService,
private location: Location, private location: Location,
private platformLocation: PlatformLocation) { private platformLocation: PlatformLocation) {
this.currentUrl.connect();
this.currentPath.connect();
this.urlSubject.next(location.path(true)); this.urlSubject.next(location.path(true));
this.location.subscribe(state => { this.location.subscribe(state => {