-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathhandlers.ts
63 lines (56 loc) · 1.88 KB
/
handlers.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
51
52
53
54
55
56
57
58
59
60
61
62
63
import { DEBUG_BUILD } from '../debug-build';
import { logger } from '../logger';
import { getFunctionName } from '../stacktrace';
export type InstrumentHandlerType =
| 'console'
| 'dom'
| 'fetch'
| 'fetch-body-resolved'
| 'history'
| 'xhr'
| 'error'
| 'unhandledrejection';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type InstrumentHandlerCallback = (data: any) => void;
// We keep the handlers globally
const handlers: { [key in InstrumentHandlerType]?: InstrumentHandlerCallback[] } = {};
const instrumented: { [key in InstrumentHandlerType]?: boolean } = {};
/** Add a handler function. */
export function addHandler(type: InstrumentHandlerType, handler: InstrumentHandlerCallback): void {
handlers[type] = handlers[type] || [];
(handlers[type] as InstrumentHandlerCallback[]).push(handler);
}
/**
* Reset all instrumentation handlers.
* This can be used by tests to ensure we have a clean slate of instrumentation handlers.
*/
export function resetInstrumentationHandlers(): void {
Object.keys(handlers).forEach(key => {
handlers[key as InstrumentHandlerType] = undefined;
});
}
/** Maybe run an instrumentation function, unless it was already called. */
export function maybeInstrument(type: InstrumentHandlerType, instrumentFn: () => void): void {
if (!instrumented[type]) {
instrumentFn();
instrumented[type] = true;
}
}
/** Trigger handlers for a given instrumentation type. */
export function triggerHandlers(type: InstrumentHandlerType, data: unknown): void {
const typeHandlers = type && handlers[type];
if (!typeHandlers) {
return;
}
for (const handler of typeHandlers) {
try {
handler(data);
} catch (e) {
DEBUG_BUILD &&
logger.error(
`Error while triggering instrumentation handler.\nType: ${type}\nName: ${getFunctionName(handler)}\nError:`,
e,
);
}
}
}