【发布时间】:2016-12-10 09:36:03
【问题描述】:
我正在订阅一个 BehaviorSubject routes$,它会发出一系列方向
例如['left', 'top', 'left']
然后我想以 200 毫秒的延迟记录这个数组的每个元素,现在在记录所有元素之后,我想记录 route finished。
我尝试了完整的事件 ()=>{} 和 finally() 运算符,但它们都没有触发,因为 routes$ 仍然存在并且可能会发出新的方向。
/** Stream recent directions */
this.route$.switchMap(() => {
return Observable
.interval(200)
.timeInterval()
.take(this.route$.getValue().length)
}).subscribe(
(v) => {
if (v.value === this.route$.getValue().length) return;
let i = v.value;
this.moveStep(this.route$.getValue()[i]);
}
);
目前我正在使用这种解决方法
.subscribe(
(v) => {
if (v.value === this.route$.getValue().length) return;
let i = v.value;
this.moveStep(this.route$.getValue()[i]);
/** Check if it is the last iteration */
if(i + 1 === this.route$.getValue().length){
console.log('route finished');
this.state = PlayerState.Idle;
}
}
);
在 observable 中是否有本地方法来实现这一点?
【问题讨论】:
-
为了清楚起见,你能重写一下吗?
标签: javascript typescript rxjs observable rxjs5