{"version":3,"file":"memory.mjs","sources":["../../../../src/lib/performance/utils/memory.ts"],"sourcesContent":["/**\n * @file Memory Utility Functions\n * @description Shared memory API utilities for performance monitoring.\n *\n * This module provides cross-browser compatible helpers for accessing\n * the Performance Memory API (Chrome/Chromium only) with graceful degradation.\n *\n * @example\n * ```typescript\n * import { isMemoryApiSupported, getPerformanceMemory } from '@/lib/performance/utils/memory';\n *\n * if (isMemoryApiSupported()) {\n *   const memory = getPerformanceMemory();\n *   console.log(`Heap usage: ${memory?.usedJSHeapSize}`);\n * }\n * ```\n */\n\n// ============================================================================\n// Types\n// ============================================================================\n\n/**\n * Performance memory information\n * @see https://developer.mozilla.org/en-US/docs/Web/API/Performance/memory\n */\nexport interface PerformanceMemoryInfo {\n  /** The total allocated heap size, in bytes */\n  readonly usedJSHeapSize: number;\n  /** The current size of the JS heap including free space not occupied by any JS objects */\n  readonly totalJSHeapSize: number;\n  /** The maximum size of the heap, in bytes, that is available to the context */\n  readonly jsHeapSizeLimit: number;\n}\n\n/**\n * Extended Performance interface with memory property (Chrome only)\n */\ninterface PerformanceWithMemory extends Performance {\n  memory?: PerformanceMemoryInfo;\n}\n\n// ============================================================================\n// Memory API Detection\n// ============================================================================\n\n/**\n * Check if the Memory API is supported in the current browser.\n *\n * The Memory API (performance.memory) is a non-standard API only available\n * in Chrome/Chromium-based browsers.\n *\n * @returns true if performance.memory is available\n *\n * @example\n * ```typescript\n * if (isMemoryApiSupported()) {\n *   // Safe to use memory monitoring features\n *   startMemoryMonitoring();\n * } else {\n *   console.log('Memory API not supported in this browser');\n * }\n * ```\n */\nexport function isMemoryApiSupported(): boolean {\n  return typeof performance !== 'undefined' && 'memory' in performance;\n}\n\n// ============================================================================\n// Memory Data Access\n// ============================================================================\n\n/**\n * Get the current performance memory information.\n *\n * Returns null if the Memory API is not supported.\n *\n * @returns PerformanceMemoryInfo object or null if unavailable\n *\n * @example\n * ```typescript\n * const memory = getPerformanceMemory();\n * if (memory) {\n *   const usagePercent = (memory.usedJSHeapSize / memory.jsHeapSizeLimit) * 100;\n *   console.log(`Memory usage: ${usagePercent.toFixed(1)}%`);\n * }\n * ```\n */\nexport function getPerformanceMemory(): PerformanceMemoryInfo | null {\n  if (!isMemoryApiSupported()) {\n    return null;\n  }\n\n  const perfWithMemory = performance as PerformanceWithMemory;\n  return perfWithMemory.memory ?? null;\n}\n\n/**\n * Calculate memory usage percentage from performance memory info.\n *\n * @param memory - Performance memory info object\n * @returns Usage percentage (0-100) or null if memory is null\n *\n * @example\n * ```typescript\n * const memory = getPerformanceMemory();\n * const usage = calculateMemoryUsagePercent(memory);\n * if (usage !== null && usage > 80) {\n *   console.warn('High memory usage detected');\n * }\n * ```\n */\nexport function calculateMemoryUsagePercent(memory: PerformanceMemoryInfo | null): number | null {\n  if (!memory) {\n    return null;\n  }\n  return (memory.usedJSHeapSize / memory.jsHeapSizeLimit) * 100;\n}\n\n/**\n * Determine memory pressure level based on usage percentage.\n *\n * @param usagePercent - Memory usage as percentage (0-100)\n * @param warningThreshold - Threshold for warning level (0-1, default 0.7)\n * @param criticalThreshold - Threshold for critical level (0-1, default 0.9)\n * @returns Memory pressure level\n *\n * @example\n * ```typescript\n * const memory = getPerformanceMemory();\n * const usage = calculateMemoryUsagePercent(memory);\n * if (usage !== null) {\n *   const pressure = getMemoryPressureLevel(usage);\n *   if (pressure === 'critical') {\n *     triggerEmergencyCleanup();\n *   }\n * }\n * ```\n */\nexport function getMemoryPressureLevel(\n  usagePercent: number,\n  warningThreshold: number = 0.7,\n  criticalThreshold: number = 0.9\n): 'normal' | 'warning' | 'critical' {\n  const usage = usagePercent / 100;\n  if (usage >= criticalThreshold) return 'critical';\n  if (usage >= warningThreshold) return 'warning';\n  return 'normal';\n}\n\n// ============================================================================\n// Exports\n// ============================================================================\n\nexport type { PerformanceWithMemory };\n"],"names":["isMemoryApiSupported","getPerformanceMemory","calculateMemoryUsagePercent","memory","getMemoryPressureLevel","usagePercent","warningThreshold","criticalThreshold","usage"],"mappings":"AAgEO,SAASA,IAAgC;AAC9C,SAAO,OAAO,cAAgB,OAAe,YAAY;AAC3D;AAsBO,SAASC,IAAqD;AACnE,SAAKD,MAIkB,YACD,UAAU,OAJvB;AAKX;AAiBO,SAASE,EAA4BC,GAAqD;AAC/F,SAAKA,IAGGA,EAAO,iBAAiBA,EAAO,kBAAmB,MAFjD;AAGX;AAsBO,SAASC,EACdC,GACAC,IAA2B,KAC3BC,IAA4B,KACO;AACnC,QAAMC,IAAQH,IAAe;AAC7B,SAAIG,KAASD,IAA0B,aACnCC,KAASF,IAAyB,YAC/B;AACT;"}