简体   繁体   English

如何在动态创建的组件上以RxJS间隔使用异步管道?

[英]How to use async pipe with RxJS interval on dynamically created component?

I was wondering if it's somehow possible to use async pipe with RxJS interval() on dynamically created components like in this example . 我想知道是否可以在本示例中的动态创建的组件上使用带有RxJS interval() async管道。 Because if I apply async pipe inside of it, then when the components will change the interval will be mostly ignored. 因为如果我在其中应用async管道,那么当组件更改时,间隔将被忽略。

Let's say I want to get data every 15 minutes but the components change every 15 seconds. 假设我想每15分钟获取一次数据,但组件每15秒更改一次。

The only way I found is to take data from one Observable and save it to another one but I don't think that this is the best practice. 我发现的唯一方法是从一个Observable获取数据并将其保存到另一个Observable ,但是我认为这不是最佳实践。 Is there some other way on how to achieve this or this practice is ok? 还有其他方法可以实现此目标吗?

The Observable looks like this: Observable看起来像这样:

interval(1000 * 60 * 15).pipe(startWith(0), switchMap(() => this.http.get('someUrl')));

Solution I found working: 我发现有效的解决方案:

interval(1000 * 60 * 15).pipe(startWith(0), switchMap(() => this.http.get('someUrl'))).subscribe((data) => {
  this.someUrlData = scheduled([data], asapScheduler);
});

The solution you have looks like it will work and is an acceptable way of doing it. 您所拥有的解决方案似乎可以正常工作,并且是可以接受的解决方案。 If I were a new developer looking it over, though, I'd probably get confused about it and might attempt an ill-advised re-factor (unless you heavily commented it). 但是,如果我是一位新开发人员来寻找它,我可能会对此感到困惑,并且可能会尝试进行不明智的重构(除非您对此发表过多评论)。

For that reason, I'd try a more readable approach (and forgive me if I'm misunderstanding your use case): 因此,我会尝试一种更具可读性的方法(如果我误解了您的用例,请原谅我):

getData = new Subject<void>();

// UI Observable (referenced via the async pipe)
someData$ = this.getData.pipe(
    switchMap(() => this.http.get('someUrl')),
    shareReplay(1)
)

// Subscriptions (inside ngOnInit)
timer(0, 1000 * 60 * 15).subscribe(() => this.getData.next())

The advantage here is you also maintain control over when you get data. 这样做的好处是您还可以控制何时获取数据。 You could easily add a "refresh" button to the screen and have it just next on the getData subject. 您可以轻松地在屏幕上添加一个“刷新”按钮,并将其放在getData主题的下一个位置。

Just be sure to clean up your subscription on the timer, otherwise it will go forever. 只要确保在计时器上清理您的订阅,否则它将永远存在。


Update: 更新:

To answer your original question, it's 100% best practice to take a value from one Observable and save it to another one. 要回答您的原始问题,从一个可观察值中获取一个值并将其保存到另一个值是100%的最佳实践。 To get around the component being destroyed, though, the best practice is to put this in a service, that way it stays intact. 但是,为了避开被破坏的组件,最佳实践是将其放入服务中,以使其保持完整。

i think the answer you found is the best way to reach what you want to do. 我认为您找到的答案是达到您想要做的最好的方法。 you can make a shared state with the time of interval and when you create the dynamic component you check the interval and based on it make the http call I think in all ways you will need an outer state that you can listen to because you will destroy the interval every time you create the component. 您可以使用时间间隔创建一个共享状态,并在创建动态组件时检查时间间隔并基于它进行http调用,我认为在所有方面都需要一个可以听见的外部状态,因为您将破坏每次创建组件的时间间隔。

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

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