Skip to content

Commit 8cd4c25

Browse files
willshowelljelbourn
authored andcommittedAug 29, 2017
fix(doc-viewer): cancel previous pending requests when changing url (#235)
-This fixes an issue where, on slow connections, requests would unnecessarily stack up if the user navigates components & tabs faster than requests can complete
1 parent 69c5224 commit 8cd4c25

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed
 

‎material.angular.io/src/app/shared/doc-viewer/doc-viewer.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
} from '@angular/core';
1313
import {Http} from '@angular/http';
1414
import {ComponentPortal, DomPortalHost} from '@angular/material';
15+
import {Subscription} from 'rxjs/Subscription';
1516
import {ExampleViewer} from '../example-viewer/example-viewer';
1617
import {HeaderLink} from './header-link';
1718

@@ -21,6 +22,7 @@ import {HeaderLink} from './header-link';
2122
})
2223
export class DocViewer implements OnDestroy {
2324
private _portalHosts: DomPortalHost[] = [];
25+
private _documentFetchSubscription: Subscription;
2426

2527
/** The URL of the document to display. */
2628
@Input()
@@ -39,7 +41,12 @@ export class DocViewer implements OnDestroy {
3941

4042
/** Fetch a document by URL. */
4143
private _fetchDocument(url: string) {
42-
this._http.get(url).subscribe(
44+
// Cancel previous pending request
45+
if (this._documentFetchSubscription) {
46+
this._documentFetchSubscription.unsubscribe();
47+
}
48+
49+
this._documentFetchSubscription = this._http.get(url).subscribe(
4350
response => {
4451
// TODO(mmalerba): Trust HTML.
4552
if (response.ok) {
@@ -95,5 +102,9 @@ export class DocViewer implements OnDestroy {
95102

96103
ngOnDestroy() {
97104
this._clearLiveExamples();
105+
106+
if (this._documentFetchSubscription) {
107+
this._documentFetchSubscription.unsubscribe();
108+
}
98109
}
99110
}

0 commit comments

Comments
 (0)
Please sign in to comment.