Skip to content
This repository was archived by the owner on Dec 18, 2024. It is now read-only.

Commit 51f17c0

Browse files
committed
Refactor rxjs bits
1 parent 7f19c8c commit 51f17c0

File tree

2 files changed

+30
-29
lines changed

2 files changed

+30
-29
lines changed

src/app/pages/component-viewer/component-viewer.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import {Component, OnInit, NgModule, ElementRef, ViewEncapsulation, ViewChild} from '@angular/core';
2-
import {ActivatedRoute, Router, RouterModule} from '@angular/router';
2+
import {ActivatedRoute, Params, Router, RouterModule} from '@angular/router';
33
import {DocumentationItems, DocItem} from '../../shared/documentation-items/documentation-items';
44
import {ComponentPageTitle} from '../page-title/page-title';
55
import {MatTabsModule} from '@angular/material';
66
import {DocViewerModule} from '../../shared/doc-viewer/doc-viewer-module';
77
import {CommonModule} from '@angular/common';
88
import {TableOfContentsModule} from '../../shared/table-of-contents/table-of-contents.module';
9+
import {Observable} from 'rxjs/Observable';
10+
911

1012
@Component({
1113
selector: 'app-component-viewer',
@@ -22,21 +24,23 @@ export class ComponentViewer {
2224
private router: Router,
2325
public _componentPageTitle: ComponentPageTitle,
2426
public docItems: DocumentationItems) {
25-
this._route.params.subscribe(params => {
26-
this.componentDocItem = docItems.getItemById(
27-
params['id'],
28-
this._route.parent.snapshot.params['section']);
29-
30-
if (this.componentDocItem) {
31-
this._componentPageTitle.title = `${this.componentDocItem.name}`;
32-
this.componentDocItem.examples.length ?
33-
this.sections.add('examples') :
34-
this.sections.delete('examples');
27+
// Listen to changes on the current route for the doc id (e.g. button/checkbox) and the
28+
// parent route for the section (material/cdk).
29+
Observable.combineLatest(_route.params, _route.parent.params)
30+
.map((p: [Params, Params]) => ({id: p[0]['id'], section: p[1]['section']}))
31+
.map(p => docItems.getItemById(p.id, p.section))
32+
.subscribe(d => {
33+
this.componentDocItem = d;
34+
if (this.componentDocItem) {
35+
this._componentPageTitle.title = `${this.componentDocItem.name}`;
36+
this.componentDocItem.examples.length ?
37+
this.sections.add('examples') :
38+
this.sections.delete('examples');
3539

36-
} else {
37-
this.router.navigate(['/components']);
38-
}
39-
});
40+
} else {
41+
this.router.navigate(['/components']);
42+
}
43+
});
4044
}
4145
}
4246

src/app/shared/table-of-contents/table-of-contents.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import {Component, Input, Inject, ElementRef, OnInit} from '@angular/core';
1+
import {Component, ElementRef, Inject, Input, OnInit} from '@angular/core';
22
import {DOCUMENT} from '@angular/platform-browser';
3-
import {Router, ActivatedRoute, NavigationEnd} from '@angular/router';
4-
import {Observable} from 'rxjs/Observable';
5-
import {Subscription} from 'rxjs/Subscription';
3+
import {ActivatedRoute, NavigationEnd, Router} from '@angular/router';
64
import 'rxjs/add/observable/fromEvent';
75
import 'rxjs/add/operator/debounceTime';
6+
import 'rxjs/add/operator/takeUntil';
7+
import {Observable} from 'rxjs/Observable';
8+
import {Subject} from 'rxjs/Subject';
89

910
interface Link {
1011
/* id of the section*/
@@ -36,17 +37,15 @@ export class TableOfContents implements OnInit {
3637

3738
_rootUrl: string;
3839
private _scrollContainer: any;
39-
private _scrollSubscription: Subscription;
40-
private _routeSubscription: Subscription;
41-
private _fragmentSubscription: Subscription;
40+
private _destroyed = new Subject();
4241
private _urlFragment = '';
4342

4443
constructor(private _router: Router,
4544
private _route: ActivatedRoute,
4645
private _element: ElementRef,
4746
@Inject(DOCUMENT) private _document: Document) {
4847

49-
this._routeSubscription = this._router.events.subscribe((event) => {
48+
this._router.events.takeUntil(this._destroyed).subscribe((event) => {
5049
if (event instanceof NavigationEnd) {
5150
const rootUrl = _router.url.split('#')[0];
5251
if (rootUrl !== this._rootUrl) {
@@ -56,7 +55,7 @@ export class TableOfContents implements OnInit {
5655
}
5756
});
5857

59-
this._fragmentSubscription = this._route.fragment.subscribe(fragment => {
58+
this._route.fragment.takeUntil(this._destroyed).subscribe(fragment => {
6059
this._urlFragment = fragment;
6160

6261
const target = document.getElementById(this._urlFragment);
@@ -73,17 +72,15 @@ export class TableOfContents implements OnInit {
7372
this._scrollContainer = this.container ?
7473
this._document.querySelectorAll(this.container)[0] : window;
7574

76-
this._scrollSubscription = Observable
77-
.fromEvent(this._scrollContainer, 'scroll')
75+
Observable.fromEvent(this._scrollContainer, 'scroll')
76+
.takeUntil(this._destroyed)
7877
.debounceTime(10)
7978
.subscribe(() => this.onScroll());
8079
});
8180
}
8281

8382
ngOnDestroy(): void {
84-
this._routeSubscription && this._routeSubscription.unsubscribe();
85-
this._scrollSubscription && this._scrollSubscription.unsubscribe();
86-
this._fragmentSubscription && this._fragmentSubscription.unsubscribe();
83+
this._destroyed.next();
8784
}
8885

8986
updateScrollPosition(): void {

0 commit comments

Comments
 (0)