-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
performanceObserver.ts
38 lines (31 loc) · 1.29 KB
/
performanceObserver.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { addLcpInstrumentationHandler, addPerformanceInstrumentationHandler } from '@sentry-internal/browser-utils';
import type { ReplayContainer } from '../types';
import { getLargestContentfulPaint } from '../util/createPerformanceEntries';
/**
* Sets up a PerformanceObserver to listen to all performance entry types.
* Returns a callback to stop observing.
*/
export function setupPerformanceObserver(replay: ReplayContainer): () => void {
function addPerformanceEntry(entry: PerformanceEntry): void {
// It is possible for entries to come up multiple times
if (!replay.performanceEntries.includes(entry)) {
replay.performanceEntries.push(entry);
}
}
function onEntries({ entries }: { entries: PerformanceEntry[] }): void {
entries.forEach(addPerformanceEntry);
}
const clearCallbacks: (() => void)[] = [];
(['navigation', 'paint', 'resource'] as const).forEach(type => {
clearCallbacks.push(addPerformanceInstrumentationHandler(type, onEntries));
});
clearCallbacks.push(
addLcpInstrumentationHandler(({ metric }) => {
replay.replayPerformanceEntries.push(getLargestContentfulPaint(metric, replay.getDomMirror()));
}),
);
// A callback to cleanup all handlers
return () => {
clearCallbacks.forEach(clearCallback => clearCallback());
};
}