I'm subscribing to a BehaviorSubject routes$ that emits an array of directions
e.g. ['left', 'top', 'left']
Then I want to log each element of this array with 200ms delay, now after all element have been logged, I want to log route finished.
I tried the complete event ()=>{} and finally() operator but they both don't fire since routes$ is still alive and might emits new directions.
/** 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]);
}
);
Currently I'm using this workaround
.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;
}
}
);
Is there a native way to achieve this in observable?