{"version":3,"file":"perfs.min.cjs","names":[],"sources":["../../../src/addons/perfs.ts"],"sourcesContent":["import type { WretchResponseChain, WretchAddon } from \"../types.js\"\n\nexport type PerfCallback = (timing: any) => void\n\nexport interface PerfsAddon {\n  /**\n   * Performs a callback on the API performance timings of the request.\n   *\n   * Warning: Still experimental on browsers and node.js\n   */\n  perfs: <T, C extends PerfsAddon, R>(this: C & WretchResponseChain<T, C, R>, cb?: PerfCallback) => this,\n}\n\n/**\n * Adds the ability to measure requests using the Performance Timings API.\n *\n * Uses the Performance API\n * ([browsers](https://developer.mozilla.org/en-US/docs/Web/API/Performance_API) &\n * [node.js](https://nodejs.org/api/perf_hooks.html)) to expose timings related to\n * the underlying request.\n *\n * Browser timings are very accurate, node.js only contains raw measures.\n *\n * ```js\n * import PerfsAddon from \"wretch/addons/perfs\"\n *\n * // Use perfs() before the response types (text, json, ...)\n * wretch(\"...\")\n *   .addon(PerfsAddon())\n *   .get()\n *   .perfs((timings) => {\n *     // Will be called when the timings are ready.\n * console.log(timings.startTime);\n *   })\n *   .res();\n *\n * ```\n */\nconst perfs: () => WretchAddon<unknown, PerfsAddon> = () => {\n  const callbacks = new Map<string, PerfCallback>()\n  let observer /*: PerformanceObserver | null*/ = null\n\n  const onMatch = (\n    entries /*: PerformanceObserverEntryList */,\n    name: string,\n    callback: PerfCallback,\n    performance: typeof globalThis.performance\n  ) => {\n    if (!entries.getEntriesByName)\n      return false\n    const matches = entries.getEntriesByName(name)\n    if (matches && matches.length > 0) {\n      callback(matches.reverse()[0])\n      if (performance.clearMeasures)\n        performance.clearMeasures(name)\n      callbacks.delete(name)\n\n      if (callbacks.size < 1) {\n        observer.disconnect()\n        if (performance.clearResourceTimings) {\n          performance.clearResourceTimings()\n        }\n      }\n      return true\n    }\n    return false\n  }\n\n  const initObserver = (\n    performance: (typeof globalThis.performance) | null | undefined,\n    performanceObserver /*: (typeof PerformanceObserver) | null | undefined */\n  ) => {\n    if (!observer && performance && performanceObserver) {\n      observer = new performanceObserver(entries => {\n        callbacks.forEach((callback, name) => {\n          onMatch(entries, name, callback, performance)\n        })\n      })\n      if (performance.clearResourceTimings) {\n        performance.clearResourceTimings()\n      }\n    }\n\n    return observer\n  }\n\n  const monitor = (\n    name: string | null | undefined,\n    callback: PerfCallback | null | undefined,\n  ) => {\n    if (!name || !callback)\n      return\n\n    if (!initObserver(performance, PerformanceObserver))\n      return\n\n    if (!onMatch(performance, name, callback, performance)) {\n      if (callbacks.size < 1)\n        observer.observe({ entryTypes: [\"resource\", \"measure\"] })\n      callbacks.set(name, callback)\n    }\n  }\n\n  return {\n    resolver: {\n      perfs(cb) {\n        this._fetchReq\n          .then(() =>\n            monitor(this._wretchReq._url, cb)\n          )\n          .catch(() => {/* swallow */ })\n        return this\n      },\n    }\n  }\n}\n\nexport default perfs\n"],"mappings":"AAsCA,MAAM,MAAsD,CAC1D,IAAM,EAAY,IAAI,IAClB,EAA4C,KAE1C,GACJ,EACA,EACA,EACA,IACG,CACH,GAAI,CAAC,EAAQ,iBACX,MAAO,GACT,IAAM,EAAU,EAAQ,iBAAiB,EAAK,CAe9C,OAdI,GAAW,EAAQ,OAAS,GAC9B,EAAS,EAAQ,SAAS,CAAC,GAAG,CAC1B,EAAY,eACd,EAAY,cAAc,EAAK,CACjC,EAAU,OAAO,EAAK,CAElB,EAAU,KAAO,IACnB,EAAS,YAAY,CACjB,EAAY,sBACd,EAAY,sBAAsB,EAG/B,IAEF,IAGH,GACJ,EACA,KAEI,CAAC,GAAY,GAAe,IAC9B,EAAW,IAAI,EAAoB,GAAW,CAC5C,EAAU,SAAS,EAAU,IAAS,CACpC,EAAQ,EAAS,EAAM,EAAU,EAAY,EAC7C,EACF,CACE,EAAY,sBACd,EAAY,sBAAsB,EAI/B,GAGH,GACJ,EACA,IACG,CACC,CAAC,GAAQ,CAAC,GAGT,EAAa,YAAa,oBAAoB,GAG9C,EAAQ,YAAa,EAAM,EAAU,YAAY,GAChD,EAAU,KAAO,GACnB,EAAS,QAAQ,CAAE,WAAY,CAAC,WAAY,UAAU,CAAE,CAAC,CAC3D,EAAU,IAAI,EAAM,EAAS,IAIjC,MAAO,CACL,SAAU,CACR,MAAM,EAAI,CAMR,OALA,KAAK,UACF,SACC,EAAQ,KAAK,WAAW,KAAM,EAAG,CAClC,CACA,UAAY,GAAiB,CACzB,MAEV,CACF"}