Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 在angular 5中使用RxJS轮询多个资源_Typescript_Rxjs_Operators_Angular5 - Fatal编程技术网

Typescript 在angular 5中使用RxJS轮询多个资源

Typescript 在angular 5中使用RxJS轮询多个资源,typescript,rxjs,operators,angular5,Typescript,Rxjs,Operators,Angular5,我需要随时间轮询多个资源。每次我开始轮询时,它都应该将它添加到某种轮询池中。如果任何请求符合某些标准,那么我必须将其从我的池中取出。我们如何使用rxjs cool operators实现这一点?我理解你的问题,你想调查一个资源列表 定时轮询的间隔(计时器)和takeUntil()/takeWhile()结束订阅可能是您的事情 let pollSubscriptions: Subscription = new Subscription() let requestUrl = 'http://bes

我需要随时间轮询多个资源。每次我开始轮询时,它都应该将它添加到某种轮询池中。如果任何请求符合某些标准,那么我必须将其从我的池中取出。我们如何使用rxjs cool operators实现这一点?

我理解你的问题,你想调查一个资源列表

定时轮询的间隔(计时器)和takeUntil()/takeWhile()结束订阅可能是您的事情

let pollSubscriptions: Subscription = new Subscription()

let requestUrl = 'http://bestResourceever.com'
let polltime = 5000; // Timer for polling

this.addNewPoll(requestUrl, polltime);


addNewPoll(url, timer){
    this.pollSubscriptions.add(
        interval(timer).pipe(
            switchMap(()=> this.http.get(url) ),
            tap(data => this.writeDateSomewhere(data) ),
            takeUntil( data => this.shouldPollingBeStopped(data) )
        ).subscribe()
    )
}
我添加了一个“pollSubscription.add()”,以防您想一次取消所有轮询订阅(此.pollSubscriptions.unsubscribe())


热情问候

您能为问题中的每个输入添加一些输入和所需输出的示例吗?一般来说,您不使用RxJs进行轮询,而是使用RxJs来避免轮询,并在有人推您时执行一些操作。不管怎么说,这只是一点废话。举一个你能取得的成就的例子会有所帮助。非常感谢@JanRecker。这看起来很有希望。。我一定会试试的。。。出于好奇,我只是在问“this.shouldlpollingbestopped(data)”方法是否会在资源池权利上停止,其条件不满足其他权利?TakeTill将在该方法得到“true”后从当前流中取消订阅。因此,它只会取消订阅一个轮询流。但在这里,它仍然是所有投票的一种方法,因此,即使每个投票都是为自己而停止的,其背后的逻辑也是一样的。为了使它更通用,您可以给“addNewPoll”一个函数作为参数,然后该函数可以用作“shouldPollingBeStopped”函数。然后,您可以根据轮询流拥有不同的takeUntil条件实现谢谢@JanRecker。但是我看到takeUntil接受了一个可观察的。如果my(data)=>data.isOk==true,我想停止轮询;我如何才能做到这一点?是的,在您的情况下,您希望根据流中的事件做出反应,那么takeWhile()就是一种方法。对不起,我错过了那个。你能把你的密码发出来,让我看看吗?我不想尝试。