All files / repo/src/hooks useTimer.ts

100% Statements 30/30
83.33% Branches 5/6
100% Functions 4/4
100% Lines 30/30

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 371x   1x   1x 3x 3x 3x   3x 3x 3x 3x 3x 3x 3x   3x 2x 2x 2x 2x 2x   3x 1x 1x 1x   3x 3x 3x 3x 3x 3x 3x  
import { ref } from 'vue';
 
import { useGameStore } from '@/stores/useGameStore';
 
export function useTimer(onBackup?: () => void) {
  const elapsed = ref(0);
  let interval: number | null = null;
  const gameStore = useGameStore();
 
  function start() {
    if (interval) return;
    interval = window.setInterval(() => {
      gameStore.incrementTime();
      onBackup?.();
    }, 1000);
  }
 
  function stop() {
    if (interval) {
      clearInterval(interval);
      interval = null;
    }
  }
 
  function reset() {
    stop();
    gameStore.elapsed = 0;
  }
 
  return {
    elapsed,
    start,
    stop,
    reset,
  };
}