-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathperformanceObserver.ts
50 lines (44 loc) · 1.66 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
39
40
41
42
43
44
45
46
47
48
49
50
import {
addClsInstrumentationHandler,
addFidInstrumentationHandler,
addInpInstrumentationHandler,
addLcpInstrumentationHandler,
addPerformanceInstrumentationHandler,
} from '@sentry-internal/browser-utils';
import type { ReplayContainer } from '../types';
import {
getCumulativeLayoutShift,
getFirstInputDelay,
getInteractionToNextPaint,
getLargestContentfulPaint,
webVitalHandler,
} 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(webVitalHandler(getLargestContentfulPaint, replay)),
addClsInstrumentationHandler(webVitalHandler(getCumulativeLayoutShift, replay)),
addFidInstrumentationHandler(webVitalHandler(getFirstInputDelay, replay)),
addInpInstrumentationHandler(webVitalHandler(getInteractionToNextPaint, replay)),
);
// A callback to cleanup all handlers
return () => {
clearCallbacks.forEach(clearCallback => clearCallback());
};
}