{"version":3,"file":"perfs.min.cjs","sources":["../../../src/addons/perfs.ts"],"sourcesContent":["import type { WretchResponseChain, WretchAddon } from \"../types.js\"\n\nconst onMatch = (entries, name, callback, _performance) => {\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    utils.callbacks.delete(name)\n\n    if (utils.callbacks.size < 1) {\n      utils.observer.disconnect()\n      if (_performance.clearResourceTimings) {\n        _performance.clearResourceTimings()\n      }\n    }\n    return true\n  }\n  return false\n}\n\nconst lazyObserver = (_performance, _observer) => {\n  if (!utils.observer && _performance && _observer) {\n    utils.observer = new _observer(entries => {\n      utils.callbacks.forEach((callback, name) => {\n        onMatch(entries, name, callback, _performance)\n      })\n    })\n    if (_performance.clearResourceTimings)\n      _performance.clearResourceTimings()\n  }\n  return utils.observer\n}\n\nconst utils = {\n  callbacks: new Map(),\n  observer: null,\n  observe: (name, callback, config) => {\n    if (!name || !callback)\n      return\n\n    const _performance = config.polyfill(\"performance\", false)\n    const _observer = config.polyfill(\"PerformanceObserver\", false)\n\n    if (!lazyObserver(_performance, _observer))\n      return\n\n    if (!onMatch(_performance, name, callback, _performance)) {\n      if (utils.callbacks.size < 1)\n        utils.observer.observe({ entryTypes: [\"resource\", \"measure\"] })\n      utils.callbacks.set(name, callback)\n    }\n\n  }\n}\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?: (timing: any) => void) => 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 *\n * For node.js, there is a little extra work to do :\n *\n * ```js\n * // Node.js only\n * const { performance, PerformanceObserver } = require(\"perf_hooks\");\n *\n * wretch.polyfills({\n *   fetch: function (url, opts) {\n *     performance.mark(url + \" - begin\");\n *     return fetch(url, opts).then(res => {\n *       performance.mark(url + \" - end\");\n *       setTimeout(() => performance.measure(res.url, url + \" - begin\", url + \" - end\"), 0);\n *       return res;\n *     });\n *   },\n *   // other polyfills…\n *   performance: performance,\n *   PerformanceObserver: PerformanceObserver,\n * });\n * ```\n */\nconst perfs: () => WretchAddon<unknown, PerfsAddon> = () => ({\n  resolver: {\n    perfs(cb) {\n      this._fetchReq.then(res => utils.observe(res.url, cb, this._wretchReq._config)).catch(() => {/* swallow */ })\n      return this\n    },\n  }\n})\n\nexport default perfs\n"],"names":["matches","entries","getEntriesByName","name","length","callback","reverse","clearMeasures","_performance","utils","callbacks","delete","size","observer","clearResourceTimings","Map","observe","config","polyfill","_observer","forEach","onMatch","lazyObserver","entryTypes","resolver","perfs","cb","this","_fetchReq","then","res","url","_wretchReq","_config","catch"],"mappings":"2CAEM,OAAU,EACd,MAAYA,EAAiBC,EAAAC,iBAAAC,GAC3B,OAAAH,KAAYI,OAAA,GACdC,EAAaL,EAAGM,UAAwB,MACzBC,eACbC,EAAiBD,cAAaJ,GAC9BM,EAAgBC,UAAAC,OAAcR,GAC5BM,YAAaG,KAAA,IACfH,EAAeI,wBAESC,wBACPA,wBAGd,GAEH,GAeKL,EAAA,CACRC,UAAA,IAAAK,IAEDF,SAAc,KACZG,QAAS,CAAAb,EAAME,EAAKY,KACpB,QAAcZ,SAEZ,MAASG,EAAaS,EAAAC,SAAA,cAAA,GApBZ,EAAAV,EAAAW,MACbV,EAAAI,UAAAL,GAAAW,eAEiC,IAAAA,OACtBV,YAAaW,SAAA,CAAAf,EAAgBF,KACvBkB,EAAApB,EAAgBE,EAACE,EAAUG,EAAA,GACvC,IAEAA,EAAEM,sBACFN,EAAAM,0BAEYD,UAYdS,CAAkBd,EAFVS,EAAAC,SAAA,sBAAA,MAKHG,EAAAb,EAAyBL,IAAYK,KAClCC,EAAAC,UAAAE,KAAA,GAEIH,EAAaI,SAAAG,QAAM,CAAEO,WAAsB,CAAA,WAAG,aACxDd,EAASC,cAAWP,MAClB,WA6DLqB,SAAA,CACGC,MAAgDC,GAElD,OADMC,KAAEC,UAAAC,MAAAC,GAAArB,EAAAO,QAAAc,EAAAC,IAAAL,EAAAC,KAAAK,WAAAC,WAAAC,OAAA,SACAP,IACN,YAILF"}