All files / use-timer/src types.ts

0% Statements 0/0
0% Branches 0/0
0% Functions 0/0
0% Lines 0/0

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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177                                                                                                                                                                                                                                                                                                                                                                 
/**
 * Supported time format types for display
 */
export type TimeFormat =
  | "HH:MM:SS"
  | "HH:MM:SS.SSS"
  | "MM:SS"
  | "mm:ss.SSS"
  | "SS";
 
/**
 * Custom time formatter function type
 */
export type TimeFormatter = (timeMs: number) => string;
 
/**
 * Timer status states
 */
export type TimerStatus = "idle" | "running" | "paused" | "finished";
 
/**
 * Options for useTimer hook
 */
export interface UseTimerOptions {
  /**
   * Update interval in milliseconds
   * @default 100
   */
  interval?: number;
 
  /**
   * Time format preset or custom formatter function
   * @default "MM:SS"
   */
  format?: TimeFormat | TimeFormatter;
 
  /**
   * Whether to start the timer automatically on mount
   * @default false
   */
  autoStart?: boolean;
 
  /**
   * Whether to loop the timer when it completes
   * @default false
   */
  loop?: boolean;
 
  /**
   * Whether to use requestAnimationFrame for smoother animations
   * When true, ignores interval option and syncs with display refresh rate
   * @default false
   */
  useRAF?: boolean;
 
  /**
   * Called when the timer completes (time reaches 0)
   */
  onComplete?: () => void;
 
  /**
   * Called on each tick with the remaining time in milliseconds
   */
  onTick?: (remainingMs: number) => void;
 
  /**
   * Called when the timer starts
   */
  onStart?: () => void;
 
  /**
   * Called when the timer pauses
   */
  onPause?: () => void;
 
  /**
   * Called when the timer resets
   */
  onReset?: () => void;
 
  /**
   * Called when the timer stops
   */
  onStop?: () => void;
}
 
/**
 * Return type for useTimer hook
 */
export interface UseTimerReturn {
  // ============ State ============
  /**
   * Formatted time string based on format option
   * @example "05:30", "01:30:00"
   */
  time: string;
 
  /**
   * Progress percentage (0-100)
   * 0 at start, 100 when complete
   */
  progress: number;
 
  /**
   * Current timer status
   */
  status: TimerStatus;
 
  // ============ Derived State ============
  /**
   * Whether the timer is currently running
   */
  isRunning: boolean;
 
  /**
   * Whether the timer is paused
   */
  isPaused: boolean;
 
  /**
   * Whether the timer has finished (time = 0)
   */
  isFinished: boolean;
 
  /**
   * Whether the timer is idle (never started or after reset)
   */
  isIdle: boolean;
 
  // ============ Controls ============
  /**
   * Start or resume the timer
   */
  start: () => void;
 
  /**
   * Pause the timer (preserves remaining time)
   */
  pause: () => void;
 
  /**
   * Stop the timer (preserves remaining time, resets status to idle)
   */
  stop: () => void;
 
  /**
   * Reset the timer to initial time
   */
  reset: () => void;
 
  /**
   * Reset and start immediately
   */
  restart: () => void;
 
  /**
   * Toggle between running and paused states
   */
  toggle: () => void;
 
  // ============ Time Manipulation ============
  /**
   * Add time in milliseconds
   */
  addTime: (ms: number) => void;
 
  /**
   * Subtract time in milliseconds (minimum 0)
   */
  subtractTime: (ms: number) => void;
 
  /**
   * Set time to a specific value in milliseconds
   */
  setTime: (ms: number) => void;
}