简体   繁体   English

切换可观察流并在下一次获取订阅

[英]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. 我有一个Observable侦听URL,并将其switchgetRows() ,该方法返回一个带有URL参数的从API返回的Observable提取数据。 I want to be able get a Subscription reference for every emit that getRows() does. 我希望能够获得getRows()所做的每个发射的订阅引用。 This is to show a loading indicator on the UI. 这是为了在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? 但是我想让组件在外部实体操纵URL时更新,因此我应该订阅而不是共享tableSource ,所以每次调用getRows()如何获得订阅,有可能吗?

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. 因此,我停止尝试将两个Observable序列都用作一个(即使一个依赖于另一个),而只是订阅了第二个作为第一个的副作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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