All files / app/services timer.service.ts

83.33% Statements 15/18
50% Branches 2/4
83.33% Functions 5/6
82.35% Lines 14/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50                1x       5x   5x   5x     5x 5x 12x 5x 5x 5x         5x         5x 7x 7x                          
import {Injectable} from '@angular/core';
import {interval, Observable, Subject, Subscription, takeUntil} from 'rxjs';
import {BaseComponent} from "../helpers/base/base.component";
import {DataService} from "./data.service";
 
@Injectable({
  providedIn: 'root'
})
export class TimerService extends BaseComponent  {
  musicProgress: number | undefined;
  musicIsPaused: boolean | undefined;
 
  private unsubscribe: Subject<void> = new Subject();
 
  updateRate = 100; // in ms
 
  sourceInterval$ : Observable<number> = interval(this.updateRate).pipe(takeUntil(this.unsubscribe));
  private sourceInterval$Subscription: Subscription | undefined;
 
  constructor(private data: DataService) {
    super()
    this.data.currentMusicProgress.subscribe(d => this.musicProgress = d)
    this.data.currentMusicIsPaused.subscribe(d => {
      this.musicIsPaused = d
      Iif (this.musicIsPaused) {
        this.unsubscribe.next();
        this.unsubscribe.complete();
      }
    })
    this.startTimer()
  }
 
  startTimer(){
    // takeWhile(() => (this.musicIsPaused ?? false)
    this.sourceInterval$Subscription = this.sourceInterval$.subscribe(n => {
      if (this.musicProgress !== undefined){
        this.data.changeMusicProgress(this.musicProgress > 99 ? 0 : (this.musicProgress + this.updateRate / 2000))
      }
    });
  }
 
 
 
  pauseTimer() {
    this.sourceInterval$Subscription?.unsubscribe();
  }
 
}