I have a function that takes a ID as parameter and makes HTTP calls, depending on the ID. It returns a Observable. I expected the calls with different parameters to be completely independent, but they influence each other.
I use
RxJS 5.0.0-beta.2
Angular 2.0.0-beta.7 for the http calls
Typescript 1.8.2
Code:
getCharacterDetails (id : number) : Observable<CharacterDetails> {
let keys : SpreadsheetKeys = this.characters[id];
if (!keys) {
return null;
}
let frontUrl : string = `${BASE_URL}/${CELLS}/${keys.spreadsheetKey}/${keys.frontWorksheetKey}/${OPTIONS}`;
let backUrl : string = `${BASE_URL}/${CELLS}/${keys.spreadsheetKey}/${keys.backWorksheetKey}/${OPTIONS}`;
return Observable.forkJoin(
this.http.get(frontUrl).map(response => response.json()),
this.http.get(backUrl).map(response => response.json()))
.map((response : any[]) => {
let character : CharacterDetails = EMPTY_MODEL;
console.log(JSON.stringify(character)); // The 1st console.log
applyFrontSheetToCharacter(response[0], character);
applyBackSheetToCharacter(response[1], character);
console.log(JSON.stringify(character)); // The 2nd console.log
return character;
});
}
Expected behavior:
I call
getCharacterDetails(1).subscribe((details) => { this.details = details }));
The 1st console.log prints my EMPTY_MODEL.
The 2nd console.log prints the character model for ID 1.
then I call
getCharacterDetails(2).subscribe((details) => { this.details = details });
The 1st console.log prints my EMPTY_MODEL.
The 2nd console.log prints the character model for ID 2.
Actual behavior:
I call
getCharacterDetails(1).subscribe((details) => { this.details = details }));
The 1st console.log prints my EMPTY_MODEL.
The 2nd console.log prints the character model for ID 1.
then I call
getCharacterDetails(2).subscribe((details) => { this.details = details });
The 1st console.log prints the character model for ID 1. <- Problem
The 2nd console.log prints the character model for ID 2.
Why are the 2 calls not completely independent? How does the 2nd call even know about the data from the first call?