All files / kernel-utils/src wake-detector.ts

100% Statements 10/10
100% Branches 7/7
100% Functions 3/3
100% Lines 10/10

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                                                                                            12x 12x   12x   12x 31x 31x   9x   31x       12x 14x      
/**
 * Options for configuring the wake detector.
 */
export type WakeDetectorOptions = {
  /**
   * How often to check for clock jumps (in milliseconds).
   *
   * @default 15000 (15 seconds)
   */
  intervalMs?: number | undefined;
 
  /**
   * Minimum clock jump to consider a wake event (in milliseconds).
   *
   * @default 30000 (30 seconds)
   */
  jumpThreshold?: number | undefined;
};
 
/**
 * Install a cross-environment sleep/wake detector that uses clock jump detection.
 * Useful for detecting when a machine wakes from sleep in both browser and Node.js.
 *
 * The detector works by checking if the system clock has jumped forward significantly
 * between interval checks. If the jump exceeds the threshold, it indicates the process
 * was suspended (e.g., machine sleep) and has now resumed.
 *
 * @param onWake - Callback to invoke when a wake event is detected.
 * @param options - Configuration options for the detector.
 * @returns A cleanup function to stop the detector.
 *
 * @example
 * ```typescript
 * const cleanup = installWakeDetector(() => {
 *   console.log('System woke from sleep, resetting connections...');
 *   resetBackoffCounters();
 * });
 *
 * // Later, when shutting down:
 * cleanup();
 * ```
 */
export function installWakeDetector(
  onWake: () => void,
  options: WakeDetectorOptions = {},
): () => void {
  const intervalMs = options.intervalMs ?? 15_000; // 15 seconds
  const jumpThreshold = options.jumpThreshold ?? 30_000; // 30 seconds
 
  let last = Date.now();
 
  const intervalId = setInterval(() => {
    const now = Date.now();
    if (now - last > intervalMs + jumpThreshold) {
      // Clock jumped forward significantly - probable wake from sleep
      onWake();
    }
    last = now;
  }, intervalMs);
 
  // Return cleanup function
  return () => {
    clearInterval(intervalId);
  };
}