In my code, I have a method that sets up a timer:
create(options: any) {
return timer(0, 1000).pipe(
map(() => {
return options;
}),
takeWhile(() => new Date() < options.finishDate)
);
}
After creating the timer, it is added to an array called this.timers
of Observables:
this.timers.push(this.create({finishDate: new Date()}))
When I merge these observers together:
this.mergeObserver = Observable.merge(this.timers).subscribe(x => {
console.log(x);
});
Even after the timer has finished, I notice that console.log(x);
still continues to output values.
Why does this happen? And how can I stop all timers in the array?
I attempted to unsubscribe from the timers like this:
Observable.merge(this.timers).subscribe(x => {
x.unsubscribe();
});
However, this method did not successfully stop the Observable.merge
.