wretch
Version:
A tiny wrapper built around fetch with an intuitive syntax.
86 lines • 2.91 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Adds the ability to measure requests using the Performance Timings API.
*
* Uses the Performance API
* ([browsers](https://developer.mozilla.org/en-US/docs/Web/API/Performance_API) &
* [node.js](https://nodejs.org/api/perf_hooks.html)) to expose timings related to
* the underlying request.
*
* Browser timings are very accurate, node.js only contains raw measures.
*
* ```js
* import PerfsAddon from "wretch/addons/perfs"
*
* // Use perfs() before the response types (text, json, ...)
* wretch("...")
* .addon(PerfsAddon())
* .get()
* .perfs((timings) => {
* // Will be called when the timings are ready.
* console.log(timings.startTime);
* })
* .res();
*
* ```
*/
const perfs = () => {
const callbacks = new Map();
let observer /*: PerformanceObserver | null*/ = null;
const onMatch = (entries /*: PerformanceObserverEntryList */, name, callback, performance) => {
if (!entries.getEntriesByName)
return false;
const matches = entries.getEntriesByName(name);
if (matches && matches.length > 0) {
callback(matches.reverse()[0]);
if (performance.clearMeasures)
performance.clearMeasures(name);
callbacks.delete(name);
if (callbacks.size < 1) {
observer.disconnect();
if (performance.clearResourceTimings) {
performance.clearResourceTimings();
}
}
return true;
}
return false;
};
const initObserver = (performance, performanceObserver /*: (typeof PerformanceObserver) | null | undefined */) => {
if (!observer && performance && performanceObserver) {
observer = new performanceObserver(entries => {
callbacks.forEach((callback, name) => {
onMatch(entries, name, callback, performance);
});
});
if (performance.clearResourceTimings) {
performance.clearResourceTimings();
}
}
return observer;
};
const monitor = (name, callback) => {
if (!name || !callback)
return;
if (!initObserver(performance, PerformanceObserver))
return;
if (!onMatch(performance, name, callback, performance)) {
if (callbacks.size < 1)
observer.observe({ entryTypes: ["resource", "measure"] });
callbacks.set(name, callback);
}
};
return {
resolver: {
perfs(cb) {
this._fetchReq
.then(() => monitor(this._wretchReq._url, cb))
.catch(() => { });
return this;
},
}
};
};
exports.default = perfs;
//# sourceMappingURL=perfs.js.map