因此,如果运行的函数运行足够长的时间以触发在计时器上运行的takenUntil函数,我将尝试使布尔值为true。
这是代码
start = this.http.get(environment.shochat_content_creator_set_valid_stream_start).pipe(
tap(() => console.log('Stream start'))
);
poll = this.http.get(environment.check_if_stream_is_active_on_mux).pipe(
tap(() => {
this.streamready = true;
return 0;
}
),
catchError(error => {
console.log(error);
return EMPTY;
})
);
startastream(){
const endtimer = timer(60000);
this.streampollsubscription = this.start.pipe(
switchMap(() => timer(0, 5000).pipe(
tap(() => console.log('Polling every 5s')),
mergeMap(() => this.poll)
)),
takeUntil(endtimer)
).subscribe();
}
本质上,如果takeUntil被触发,我希望将布尔值设置为true。
timeout = true;
我一直在看这个stackoverflow帖子
但是事情并没有我想的那么清楚。
takeUntil
完成可观察的操作,因此要在完成操作后执行操作,可以在几个地方进行:
completion
处理程序中:this.start.pipe(
switchMap(() => timer(0, 5000).pipe(
tap(() => console.log('Polling every 5s')),
mergeMap(() => this.poll))
),
takeUntil(endtimer)
)
.subscribe(
next => console.log('handling next value'),
error => console.log('handling error'),
() => this.timeout = true // <--- this fires after observable completes
);
finalize
运算符:this.start.pipe(
switchMap(() => timer(0, 5000).pipe(
tap(() => console.log('Polling every 5s')),
mergeMap(() => this.poll))
),
takeUntil(endtimer),
finalize(() => this.timeout = true)
)
.subscribe();
注意:这些解决方案并不完全符合您的要求。的确,它们会在takeUntil
触发时触发,但由于流完成的任何其他原因,它们也会触发。我认为区别对您而言并不重要,但想在整个问题中提及。
正如Sang Dang在评论中提到的那样,您还可以从“计时器关闭时(而不是到目前为止我提到的内容,当可观察的对象完成时”)的角度来进行处理,只需添加一个tap
给你的计时器。
const endtimer = timer(60000).pipe(
tap(() => this.timeout = true)
);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句