如何使可观察重复 [英] How to make observable to repeat

查看:34
本文介绍了如何使可观察重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个发出新值的代码:

I have a code that emits a new values :

function newValues(): Observable<any> {
    const data$ = new BehaviorSubject<any>({});

    setInterval((x) => {
       data$.next(this.getNewData());
    }, 1000);

   return data$.asObservable();
}

这样通过订阅 newValues().subscribe() 我可以一直接收新值.

so this way by subscribing to newValues().subscribe() I can receive a new values all the time.

但我认为这不是最好的方法,所以我查看了this,所以我的代码变得像这个:

But I think it is not the best way of doing this so I looked at this and so my code became like this:

function newValues(): Observable<any> {
    const data$ = new BehaviorSubject<any>({});

    defer(() => data$.next(this.getNewData()))
      .pipe(
       repeatWhen(notifications => notifications.pipe(delay(1000)))
     );

   return data$.asObservable();
}

但是现在似乎代码不起作用,在收到第一个值后,没有发生任何排放.

However it is seems like the code not working now, after receiving the first value no emissions is taking place.

任何想法如何解决?

更新:
@evantha 建议对该用例使用间隔,我只是想知道这是否意味着 defer....repeatWhen....delay 不适用于该用例?

推荐答案

我认为您的问题包含一些彼此不同的要点.

I think your question contains some points which are different one from each other.

第一点:获取值序列(即创建流)

在第一个代码片段中,您定义了一个函数 newValues(),它返回一个 Observable,它将发出一系列值,每个值都由函数 getNewData() 每 1000 毫秒调用一次.为此,您可以创建一个 BehaviorSubject,然后以一个间隔调用 next.

In the first code snippet you define a function newValues() which returns an Observable which will emit a sequence of values, each value being calculated by the function getNewData() called every 1000 ms. To do so you create a BehaviorSubject and then call next on it with an interval.

如果这是您想要的,那么使用 rxjs interval 函数可能是@evantha 建议的正确方法.至少在我看来,它更简单、更清晰.

If this is what you want, probably the use of rxjs interval function is the right way to go as suggested by @evantha. It is simpler and clearer, at least in my opinion.

第二点:为什么第二个片段不起作用

原因很简单.你调用函数 defer 但你没有对它返回的值做任何事情,而是简单地返回你刚刚创建的 BehaviorSubject,它本质上返回起始值仅此而已,除非有人对其调用 next,这不会在第二个代码段中发生.

The reason is simple. You call the function defer but you do not do anything with the value it returns, rather you simply return the BehaviorSubject you just created which, by its nature, returns the starting value and nothing more, unless somebody calls next on it, which does not happen in the second snippet.

第三点:使用repeatWhen来生成重复值

使用 repeatWhen 生成重复的情况要微妙得多,我建议您使用 阅读文档.我只想强调一件事.repeatWhen 在它的源 Observable complete s 时进入动作.当它 complete s 时,repeatWhen 确保只要函数返回的 Observable 传递给 repeatWhen 就重复相同的源 Observable参数发出.您无法轻易想象将 BehaviorSubject 作为 repeatWhen 的源,因为您通常不会complete Subject.delay 的加入使情况变得更加复杂.

The use of repeatWhen to generate a repetition is a much more nuanced case, and I suggest you to read the documentation. I wish to highlight only one thing. repeatWhen enters in action when its source Observable completes. And when it completes, repeatWhen makes sure that the same source Observable is repeated as soon as the Observable returned by the function passed to repeatWhen as parameter emits. You can not easily imagine to have a BehaviorSubject as source for repeatWhen since you normally do not complete a Subject. The addition of delay makes the case even more complicated.

同时使用 delayrepeatWhen 的示例是指特定用例:使用异步函数轮询资源,例如使用 hppt 轮询远程系统称呼.在这种情况下,您可能希望结合使用 delayrepeatWhen 来避免重叠调用.但这是一个非常具体的要求.

The example where both delay and repeatWhen are used refers to a specific use case: polling a resource using an async function, for instance polling a remote system using an hppt call. In this case you may want to use a combination of delay and repeatWhen to avoid having overlapping calls. But this is a very specific requirement.

这篇关于如何使可观察重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆