{"version":3,"sources":["../../src/use-browser-memory/index.ts"],"sourcesContent":["import { useIntervalFn } from '../use-interval-fn'\nimport { useLatest } from '../use-latest'\nimport { useSafeState } from '../use-safe-state'\nimport { useStableFn } from '../use-stable-fn'\nimport { useSupported } from '../use-supported'\nimport { timestamp } from '../utils/basic'\n\nimport type { UseIntervalFnOptions } from '../use-interval-fn'\n\n/**\n * Performance.memory\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/API/Performance/memory\n */\ninterface MemoryInfo {\n  /**\n   * The maximum size of the heap, in bytes, that is available to the context.\n   */\n  readonly jsHeapSizeLimit: number\n  /**\n   *  The total allocated heap size, in bytes.\n   */\n  readonly totalJSHeapSize: number\n  /**\n   * The currently active segment of JS heap, in bytes.\n   */\n  readonly usedJSHeapSize: number\n\n  [Symbol.toStringTag]: 'MemoryInfo'\n}\n\ninterface PerformanceWithMemory extends Performance {\n  /**\n   * Performance.memory\n   */\n  memory: MemoryInfo\n}\n\nexport interface UseBrowserMemoryOptions extends UseIntervalFnOptions {\n  /**\n   * The interval to update the memory info\n   */\n  interval?: number\n}\n\nexport interface UseBrowserMemoryReturns {\n  /**\n   * The timestamp when the memory info was last updated.\n   */\n  timestamp: number\n  /**\n   * The memory info.\n   */\n  memory: MemoryInfo | undefined\n  /**\n   * The maximum size of the heap, in bytes, that is available to the context.\n   */\n  jsHeapSizeLimit: number\n  /**\n   * The total allocated heap size, in bytes.\n   */\n  totalJSHeapSize: number\n  /**\n   * The used heap size, in bytes.\n   */\n  usedJSHeapSize: number\n  /**\n   *  Whether the browser supports the `Performance.memory` API.\n   */\n  isSupported: boolean\n  /**\n   * Update the memory info.\n   */\n  update(): void\n}\n\n/**\n * A React Hook that helps to get the memory info of the browser.\n *\n * @param {UseBrowserMemoryOptions} [options={}] - `UseBrowserMemoryOptions`, options to configure the hook, see {@link UseBrowserMemoryOptions}\n * @returns {UseBrowserMemoryReturns} `UseBrowserMemoryReturns`, see {@link UseBrowserMemoryReturns}\n * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Performance/memory#browser_compatibility | Performance.memory's browser compatibility - MDN}\n */\nexport function useBrowserMemory(options: UseBrowserMemoryOptions = {}): UseBrowserMemoryReturns {\n  const { interval = 1000, ...useIntervalFnOptions } = options\n\n  const isSupported = useSupported(() => 'memory' in performance)\n  const latest = useLatest({ isSupported })\n\n  const [state, setState] = useSafeState({\n    timestamp: Date.now(),\n    memory: undefined as MemoryInfo | undefined,\n  })\n\n  const update = useStableFn(() => {\n    if (!latest.current.isSupported) return\n\n    setState({\n      memory: (performance as PerformanceWithMemory).memory,\n      timestamp: timestamp(),\n    })\n  })\n\n  useIntervalFn(update, interval, { immediateCallback: true, ...useIntervalFnOptions })\n\n  const memoryDetail = {\n    jsHeapSizeLimit: state.memory?.jsHeapSizeLimit || 0,\n    totalJSHeapSize: state.memory?.totalJSHeapSize || 0,\n    usedJSHeapSize: state.memory?.usedJSHeapSize || 0,\n  } as const\n\n  return {\n    ...state,\n    ...memoryDetail,\n    isSupported,\n    update,\n  }\n}\n"],"names":["useBrowserMemory","options","state","interval","useIntervalFnOptions","isSupported","useSupported","performance","latest","useLatest","useSafeState","timestamp","Date","now","memory","undefined","setState","update","useStableFn","current","useIntervalFn","immediateCallback","memoryDetail","jsHeapSizeLimit","totalJSHeapSize","usedJSHeapSize"],"mappings":";;;;+BAmFgBA;;;eAAAA;;;6BAnFc;yBACJ;4BACG;2BACD;4BACC;qBACH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8EnB,SAASA;QAAiBC,UAAAA,iEAAmC,CAAC;QAuBhDC,eACAA,gBACDA;IAxBlB,wBAAqDD,QAA7CE,UAAAA,0CAAW,0BAASC,kDAAyBH;;;IAErD,IAAMI,cAAcC,IAAAA,0BAAY,EAAC;eAAM,YAAYC;;I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