Edited the question with a concise example. I think it'll make it easier for people to understand. Take note this example is super simplified, normally I layer things between different components, but for the question it will suffice.
Take this component. It takes the name of a fetched object, and a button to fetch the next object. To get the value of the next REST request, I know of no other way than to subscribe to the answer, and what I would like is to have something like "combineLatest" but for "the future", so I can combineLatest of later streams.
import { Component, VERSION, OnInit } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
private readonly PEOPLE_API_ENDPOINT = `https://swapi.dev/api/people/`;
private characterSubject : BehaviorSubject<any> = new BehaviorSubject<any>({name: 'loading'});
private currentCharacter: number = 1;
character$ : Observable<any> = this.characterSubject.asObservable();
constructor(
private http: HttpClient
) {}
ngOnInit() {
this.updateCurrentCharacter();
}
nextCharacter() : void {
this.currentCharacter ++;
this.updateCurrentCharacter();
}
//I would want to avoid subscribing, instead
//I would like some sort of operation to send the stream
//emissions to the subject. As to not break the observable
//chain up until presentation, like the best practices say.
private updateCurrentCharacter() : void {
this.fetchCharacter(this.currentCharacter)
.subscribe(
character => this.characterSubject.next(character)
);
}
private fetchCharacter (id: number) : Observable<any> {
return this.http.get(this.PEOPLE_API_ENDPOINT + `${id}/`);
}
}
<span>{{ character$ | async }} </span>
<button (click)="nextCharacter()">Next character</button>
Is there any way to do that? Doing something like "emitIn(characterSubject)". I think there is nothing like this, like dynamically add source emissions to a source.