@embrace-io/web-sdk
Version:
45 lines (44 loc) • 1.74 kB
JavaScript
//#region src/utils/performanceObserver/performanceObserver.ts
function isEntryTypeSupported(type) {
return typeof PerformanceObserver !== "undefined" && (PerformanceObserver.supportedEntryTypes?.includes(type) ?? false);
}
/**
* Creates a PerformanceObserver for the specified entry type if supported, and starts observing.
* Returns the observer instance, or null if the entry type is not supported or if an error occurs.
*
* Each entry is passed individually to `processEntry`. Errors thrown by `processEntry` are caught
* and logged via `diag` (if provided) so that one bad entry does not block the rest.
*
* Note: The observer is created with the "buffered" option set to true, so it will receive entries
* that were recorded before the observer was created.
* buffered: true is not supported when observing multiple entry types, so if you need to observe
* multiple types, you should create separate observers for each type with buffered: true.
* See https://developer.mozilla.org/en-US/docs/Web/API/PerformanceObserver/observe#entrytypes
*/
function createPerformanceObserver(type, processEntry, options) {
const { diag, ...observeOptions } = options ?? {};
if (!isEntryTypeSupported(type)) {
diag?.debug(`${type} not supported, skipping observer`);
return null;
}
try {
const observer = new PerformanceObserver((list) => {
for (const entry of list.getEntries()) try {
processEntry(entry);
} catch (e) {
diag?.error(`error processing ${type} entry`, e);
}
});
observer.observe({
type,
buffered: true,
...observeOptions
});
return observer;
} catch {
return null;
}
}
//#endregion
export { createPerformanceObserver, isEntryTypeSupported };
//# sourceMappingURL=performanceObserver.js.map