Reputation: 2883
I'm a starter with RxJS and only familiar with basics. The situation is: I have an observable that should load some data asynchronously on demand. And the other function should create that demand and ask the first observable to load next piece of data.
Like so:
dataPiece$:Observable<DataPiece> = http.load(this.url + this.pieceUrl)
loadNextPiece(pieceUrl)
{
this.pieceUrl = pieceUrl;
//make the observable to automatically load new piece of data
//once pieceUrl is updated
}
Upvotes: 0
Views: 158
Reputation: 826
You can create a stream that contains the pieceUrls using a Subject. For every piece the request will be made and using switchMap you can merge all responses into a new stream.
let urlPiece = new Subject();
let dataPiece$:Observable<DataPiece>;
loadNextPiece(pieceUrl)
{
urlPiece.next(pieceUrl);
}
dataPiece$ = urlPiece.asObservable()
.switchMap(urlPiece => http.load(this.url + pieceUrl))
.map((response) => {
// parse response or just return it
return response;
})
.catch(error => {
// handle error
});
let subscription = dataPiece$.subscribe( (response) => {
//do something with response
})
Upvotes: 1