Одним из решений является сохранение подписки в вашем компоненте.
export class Component {
interval$: Subscription;
start() {
this.max = this.duration;
this.interval$ = Observable.interval(this.selectedSprint.duration)
.takeWhile(_ => !this.isFinished)
.do(i => this.current += 1)
.subscribe();
}
stop() {
// here I need to unsybscribe to the observable
this.dialogService.openConfirmDialog('do you want to stop ?')
.afterClosed().subscribe((result: boolean) => {
if (result) {
this.interval$.unsubscribe();
}
});
}
}
РЕДАКТИРОВАТЬ, чтобы ответить на комментарий ОП
export class Component {
intervalSub$: Subscription;
intervalObs: Observable<number>;
start() {
this.max = this.duration;
this.intervalObs$ = Observable.interval(this.selectedSprint.duration)
.takeWhile(_ => !this.isFinished)
.do(i => this.current += 1);
this.intervalSub$ = intervalObs.subscribe();
}
stop() {
// here I need to unsybscribe to the observable
this.dialogService.openConfirmDialog('do you want to stop ?')
.afterClosed().subscribe((result: boolean) => {
if (result) {
this.intervalSub$.unsubscribe();
}
});
}
/**
* If you unsubscribed from the interval and you want to resume
* (If this.isFinished is true, it won't do a thing)
*/
resume() {
this.intervalObs.subscribe();
}
}