简体   繁体   English

每次异步管道订阅时,由Http返回的Observable

[英]Observable returned by Http fired each time async pipe is subscribing

So I have this method that returns some observable. 所以我有这种方法返回一些可观察的。

fetch(publication: Publication): Observable<Array<ProcessingCenter>> {
  return this.http.get(`${BASE_URL}/api/publications/${publication.id}`, {
    headers: headers
  }).map(response => {
    return response.json() as Array<ProcessingCenter>;
  })
}

I store returned value ( getObservable always returns the same observable). 我存储返回值( getObservable总是返回相同的observable)。

And in one component I utilize this observable in component: 在一个组件中,我利用了这一可观察的组件:

<tr *ngFor="let pc of getObservable(p) | async">
  ...
</tr>

And now, even though I'm always using the same observable, when I toggle component, each time it is created and async pipe is subscribing, an AJAX request is issued. 现在,即使我始终使用相同的可观察对象,当我切换组件时,每次创建组件并订阅异步管道时,都会发出AJAX请求。 AFAIK AJAX request should be issued only once, when first subscription is done, right? 首次订阅完成后,AFAIK AJAX请求应该只发出一次,对吗?

EDIT 编辑

Thanks for answers, but they just don't focus on the real issue. 感谢您的回答,但他们只是不关注实际问题。 Let's me rephrase it. 让我改一下。

  1. Suppose I have a observable variable myObservable returned by Http service 假设我有一个由Http服务返回的可观察变量myObservable

  2. Suppose in template I'm using this variable with async pipe 假设在模板中我将此变量与async管道一起使用

  3. Suppose that I'm toggling a part of the template that is using async pipe with simple *ngIf 假设我要通过简单的*ngIf切换使用async管道的模板的一部分

Eg: 例如:

<button (click)="toggle = !toggle">Toggle</button>
<table class="table table-condensed" *ngIf="toggle">
  <!-- Somewhere in here is myObservable | async  -->>
</table>

Now, when I click the button, each time table is toggled, http request is sent (even though myObservable is still the same observable object (component's instance field). 现在,当我单击按钮时,将切换每个时间表,并发送http请求(即使myObservable仍然是相同的可观察对象(组件的实例字段)。

And the questions is: what is so special about async pipe or observable that is returned by Http service that makes them do Http request each time a subscription is made? 问题是: Http服务返回的async管道或可观察对象有什么特别之处,使得每次订阅时它们都执行Http请求?

That's the normal way observables work. 这是可观察的正常工作方式。 The logic they run in order to produce values (ie firing an http request and waiting for its result) is executed every time an observer subscribes. 每当观察者订阅时,它们就会执行以产生值(即触发http请求并等待其结果)的逻辑。 These are called cold observables. 这些被称为cold观测。 You can instead have hot observables that execute the logic once, by using publish() . 您可以使用publish()来使hot可观察对象执行一次逻辑。 If an observer subscribed after such observable emitted its values, it would receive nothing. 如果观察者在该可观察对象发出其值后订阅,它将什么也收不到。 But you can have other variants of hot observables that would know how to deal with already emitted values, at each new subscription. 但是您可以在其他新订阅中使用其他可观察到的hot变量,这些变量将知道如何处理已发出的值。 See publishLast() , publishBehavior() and publishReplay() . 请参见publishLast()publishBehavior()publishReplay()

You can use publishLast with refcount. 您可以将publishLast与refcount一起使用。 publishLast is multicast operator with AsyncSubject that will execute the observable once and remember. publishLast是具有AsyncSubject的多播运算符,它将执行一次可观察的事件并记住。

Something like that: 像这样:

rows$ = Observable.of([1, 2, 3]).do(x => 
                   console.log(x)).publishLast().refCount();

You do not need to worry about it because, browser understands that you are requesting same resource again, which is already present in cache, so browser add header in request stating, give me content only if its new, So although request is made each time, you get data from server only if its new, otherwise you just get empty response with 304 status. 您无需担心,因为浏览器了解您再次请求相同的资源,该资源已经存在于缓存中,因此浏览器在请求声明中添加标头,仅在新内容时才提供内容,因此尽管每次都发出请求,则仅在服务器新的情况下才从服务器获取数据,否则仅会收到304状态的空响应。

This repeated request also can be avoided with proper configurations on backend, which states for how much time browser can consider the response to be new by default. 也可以通过在后端进行适当的配置来避免重复请求,该配置指出浏览器默认情况下可以将响应视为新响应的时间。

This link explains it https://www.linkedin.com/pulse/worried-making-repeated-requests-same-resource-slowing-akshay-jain/ 此链接对此进行了说明https://www.linkedin.com/pulse/worried-making-repeated-requests-same-resource-slowing-akshay-jain/

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

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