I'm facing a random behavior with the flatMap operator and I can't find the reason why. Sometimes it triggers, sometimes it doesn't...
Here is the situation: The user can change the language in my app, so I have a BehaviorSubject on the language (which is triggered by a select list) returned as an observable by its provider. When there's a change I call (via flatMap) a http request to fetch the data in the language selected.
It looks like this:
this.languageProvider.getLang$().flatMap(langCode => {
return this.http.get(`https://SERVER_URL.net/datas?lang=${langCode}`)
.map(data => data.json())
})
.subscribe(
data => {
// do smth
},
err => {
// do smth
}
);
The thing is, when I change the language the http call is most often not triggered.
If I add a simple subscribe it always work...
this.languageProvider.getLang$().subscribe(langCode => {
console.log(langCode);
});
Any idea why I have this issue ?
Here is the languageProvider:
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class LanguageProvider {
private lang$: BehaviorSubject<string>;
constructor() {
this.lang$ = new BehaviorSubject('en');
}
setLang(langCode: string) {
this.lang$.next(langCode);
}
getLang$(): Observable<string> {
return this.lang$.asObservable();
}
}
Thanks a lot