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

fix(doc-viewer): cancel previous pending requests when changing url #235

Merged
merged 1 commit into from
Aug 29, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/app/shared/doc-viewer/doc-viewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from '@angular/core';
import {Http} from '@angular/http';
import {ComponentPortal, DomPortalHost} from '@angular/material';
import {Subscription} from 'rxjs/Subscription';
import {ExampleViewer} from '../example-viewer/example-viewer';


Expand All @@ -19,6 +20,7 @@ import {ExampleViewer} from '../example-viewer/example-viewer';
})
export class DocViewer implements OnDestroy {
private _portalHosts: DomPortalHost[] = [];
private _documentFetchSubscription: Subscription;

/** The URL of the document to display. */
@Input()
Expand All @@ -35,7 +37,12 @@ export class DocViewer implements OnDestroy {

/** Fetch a document by URL. */
private _fetchDocument(url: string) {
this._http.get(url).subscribe(
// Cancel previous pending request
if (this._documentFetchSubscription) {
this._documentFetchSubscription.unsubscribe();
}

this._documentFetchSubscription = this._http.get(url).subscribe(
response => {
// TODO(mmalerba): Trust HTML.
if (response.ok) {
Expand Down Expand Up @@ -88,5 +95,9 @@ export class DocViewer implements OnDestroy {

ngOnDestroy() {
this._clearLiveExamples();

if (this._documentFetchSubscription) {
this._documentFetchSubscription.unsubscribe();
}
}
}