简体   繁体   中英

switch Observable stream and grab subscription each next

I have an Observable listening to the URL and I am switch ing it to a getRows() which returns an Observable pulling data back from the API with the URL parameters. I want to be able get a Subscription reference for every emit that getRows() does. This is to show a loading indicator on the UI.

The current code:

this.tableSource = this.urlParamService.getParameterGroup(this.parameterPrefix)
    .distinctUntilChanged()
    .map(p => this.getRows(p))
    .switch()
    .share();

And then explicitly when I have changed the parameters I have been calling:

this.tableLoad = this.tableSource.take(1).subscribe((r) => this.rows = this.parseRows(r));

But I want to enable the component to update when external entities manipulate the URL and so I should be subscribing instead of sharing tableSource , so how can I get a Subscription everytime I call getRows() , is it possible?

I managed to solve it this way:

this.urlParamService.getParameterGroup(this.parameterPrefix)
    .distinctUntilChanged()
    .do(p => {
        this.tableLoad = this.getRows(p).subscribe((r) => this.rows = this.parseRows(r));
    })
    .subscribe();

So I stopped trying to use both Observable sequences as one (even though one depends on the other), and just subscribed to the second as a side effect of the first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM