Typescript RxJS如何随着时间的推移进行组合测试?
我正在尝试创建一个轮询系统,在间隔时间内轮询后端 我有一个方法Typescript RxJS如何随着时间的推移进行组合测试?,typescript,rxjs,combinelatest,Typescript,Rxjs,Combinelatest,我正在尝试创建一个轮询系统,在间隔时间内轮询后端 我有一个方法add,它将包含命令的pollingItem作为字符串和间隔的刷新时间,并将从间隔创建的可观察项传递给主题 this.sideEffect是在init上传递给类构造函数的函数,该函数与后端通信 我要创建两种类型的间隔: 当调用IntervalMap中链接到命令的主题时,可以停止的默认主题 一个“开关”,可以用与上面相同的方式停止,但也可以在添加新的轮询项时停止 add(pollingItem, type = 'DEFAULT') {
add
,它将包含命令的pollingItem作为字符串和间隔的刷新时间,并将从间隔创建的可观察项传递给主题
this.sideEffect
是在init上传递给类构造函数的函数,该函数与后端通信
我要创建两种类型的间隔:
当调用IntervalMap中链接到命令的主题时,可以停止的默认主题
一个“开关”,可以用与上面相同的方式停止,但也可以在添加新的轮询项时停止
add(pollingItem, type = 'DEFAULT') {
this.intervalMap = { ...this.intervalMap, [pollingItem.command]: new Subject() };
switch (type) {
case 'SWITCH': {
const poller = interval(pollingItem.refreshTime).pipe(
takeUntil(this.intervalMap[pollingItem.command]),
takeUntil(this.onAdd$),
tap(() => {
this.sideEffect(pollingItem.command);
})
);
this.poll$.next(poller);
break;
}
default: {
const poller = interval(pollingItem.refreshTime).pipe(
takeUntil(this.intervalMap[pollingItem.command]),
tap(() => {
this.sideEffect(pollingItem.command);
})
);
this.poll$.next(poller);
break;
}
}}
我的问题是,我想找到一种方法,随着时间的推移,将传递到poll$
中的观察值组合起来
目前,我已尝试通过以下方式实现此目标:
initialize() {
this.poll$
.pipe(
takeUntil(this.clear$),
scan((agg, current) => {
return [...agg, current];
}, [])
)
.subscribe(
poll => {
combineLatest(poll)
.pipe(takeUntil(this.onComplete$))
.subscribe(() => {
this.onAdd$.next();
});
},
() => {},
() => {
this.onComplete$.next();
}
);}
然而,虽然这确实实现了随着时间的推移组合观察值的目标,但它是有限的,因为它会在添加时重新启动所有间隔,这不是预期的行为
我不知道接下来该怎么办
有什么建议吗?我还是RxJS的新手,所以如果有任何建议,无论是否与主题相关,我都将不胜感激
谢谢大家! 请详细介绍一下“this.poll$”属性好吗?你是怎么订阅的?嗨!谢谢你的回复!我设法找到了解决办法。这不是最好的<代码>this.poll$.pipe(switchMap(poll=>{this.pollArray=this.pollArray.concat(poll);返回combineTest(…this.pollArray.map(poller=>poller.obs)).pipe(takeUntil(this.clear$);})).subscribe()代码>