UNPKG

@embrace-io/web-sdk

Version:
1 lines 2.62 kB
{"version":3,"file":"performanceObserver.cjs","names":[],"sources":["../../../src/utils/performanceObserver/performanceObserver.ts"],"sourcesContent":["import type { DiagLogger } from '@opentelemetry/api';\n\nexport function isEntryTypeSupported(type: string): boolean {\n return (\n typeof PerformanceObserver !== 'undefined' &&\n (PerformanceObserver.supportedEntryTypes?.includes(type) ?? false)\n );\n}\n\n/**\n * Creates a PerformanceObserver for the specified entry type if supported, and starts observing.\n * Returns the observer instance, or null if the entry type is not supported or if an error occurs.\n *\n * Each entry is passed individually to `processEntry`. Errors thrown by `processEntry` are caught\n * and logged via `diag` (if provided) so that one bad entry does not block the rest.\n *\n * Note: The observer is created with the \"buffered\" option set to true, so it will receive entries\n * that were recorded before the observer was created.\n * buffered: true is not supported when observing multiple entry types, so if you need to observe\n * multiple types, you should create separate observers for each type with buffered: true.\n * See https://developer.mozilla.org/en-US/docs/Web/API/PerformanceObserver/observe#entrytypes\n */\nexport function createPerformanceObserver<T extends PerformanceEntry>(\n type: string,\n processEntry: (entry: T) => void,\n options?: PerformanceObserverInit & { diag?: DiagLogger },\n): PerformanceObserver | null {\n const { diag, ...observeOptions } = options ?? {};\n\n if (!isEntryTypeSupported(type)) {\n diag?.debug(`${type} not supported, skipping observer`);\n return null;\n }\n\n try {\n const observer = new PerformanceObserver((list) => {\n for (const entry of list.getEntries() as T[]) {\n try {\n processEntry(entry);\n } catch (e) {\n diag?.error(`error processing ${type} entry`, e);\n }\n }\n });\n observer.observe({ type, buffered: true, ...observeOptions });\n return observer;\n } catch {\n return null;\n }\n}\n"],"mappings":";;AAEA,SAAgB,qBAAqB,MAAuB;CAC1D,OACE,OAAO,wBAAwB,gBAC9B,oBAAoB,qBAAqB,SAAS,IAAI,KAAK;AAEhE;;;;;;;;;;;;;;AAeA,SAAgB,0BACd,MACA,cACA,SAC4B;CAC5B,MAAM,EAAE,MAAM,GAAG,mBAAmB,WAAW,CAAC;CAEhD,IAAI,CAAC,qBAAqB,IAAI,GAAG;EAC/B,MAAM,MAAM,GAAG,KAAK,kCAAkC;EACtD,OAAO;CACT;CAEA,IAAI;EACF,MAAM,WAAW,IAAI,qBAAqB,SAAS;GACjD,KAAK,MAAM,SAAS,KAAK,WAAW,GAClC,IAAI;IACF,aAAa,KAAK;GACpB,SAAS,GAAG;IACV,MAAM,MAAM,oBAAoB,KAAK,SAAS,CAAC;GACjD;EAEJ,CAAC;EACD,SAAS,QAAQ;GAAE;GAAM,UAAU;GAAM,GAAG;EAAe,CAAC;EAC5D,OAAO;CACT,QAAQ;EACN,OAAO;CACT;AACF"}