Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export class TimerService {
  2.     K = 1000;
  3.     INTERVAL = this.K;
  4.     MINUTES = 25;
  5.     TIME = this.MINUTES * this.K * 60;
  6.  
  7.     current = 0;
  8.     time = this.TIME;
  9.  
  10.     toggle$ = new BehaviorSubject(true);
  11.     remainingSeconds$ = this.toggle$.pipe(
  12.         switchMap((running: boolean) => {
  13.             return running ? timer(0, this.INTERVAL) : NEVER;
  14.         }),
  15.         map(this.toRemainingSeconds),
  16.         takeWhile(t => t >= 0)
  17.     );
  18.  
  19.     constructor() {}
  20.  
  21.     toMinutes(ms: number): number {
  22.         return Math.floor(ms / this.K / 60);
  23.     }
  24.  
  25.     toSeconds(ms: number): number {
  26.         return Math.floor(ms / this.K) % 60;
  27.     }
  28.  
  29.     toSecondsString(ms: number): string {
  30.         const seconds = this.toSeconds(ms);
  31.         return seconds < 10 ? `0${seconds}` : seconds.toString();
  32.     }
  33.  
  34.     toMs(t: number): number {
  35.         return t * this.INTERVAL;
  36.     }
  37.  
  38.     currentInterval(): number {
  39.         return this.time / this.INTERVAL;
  40.     }
  41.  
  42.     toRemainingSeconds(t: number): number {
  43.         return this.currentInterval() - t;
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement