I use Subject, switchMapand .next()to cancel previous pending http calls when starting a new request http.get().
The problem is that I am processing an HTTP error (for example, timeout), the method _postMPCHC.next(...)called in html no longer works ... Do I need to recreate the error subscription? How?
import {Component} from '@angular/core';
import {NavController, NavParams, Toast} from 'ionic-angular';
import {Http, URLSearchParams} from '@angular/http';
import {AppSettings} from '../../appSettings';
import {Subject} from 'rxjs/Subject';
@Component({
templateUrl: 'build/pages/video/video.html'
})
export class VideoPage {
_postMPCHC: any= new Subject();
constructor(private http: Http, private nav: NavController) {
this.defineHttp();
}
defineHttp() {
var sub = this._postMPCHC.switchMap((x: string) => {
let params: URLSearchParams = new URLSearchParams();
params.set('token', AppSettings.API_TOKEN);
params.set('prog', 'mhz');
params.set('prog', 'mpchc');
params.set('action', x);
return this.http.get(AppSettings.API_ENDPOINT, { search: params })
}).timeout(5000, new Error('timeout exceeded')).subscribe(x => { },
error => {
let toast = Toast.create({
message: 'Server response: ' + <any>error,
duration: 3000,
position: 'middle'
});
this.nav.present(toast);
})
}
}
source
share