@sentry/core
Version:
Base implementation for all Sentry JavaScript SDKs
51 lines (50 loc) • 1.67 kB
JavaScript
import { DEBUG_BUILD } from "../debug-build.js";
import { debug } from "../utils/debug-logger.js";
import { getFunctionName } from "../utils/stacktrace.js";
// src/instrument/handlers.ts
const handlers = {};
const instrumented = {};
/** Add a handler function. Returns a function to remove the handler. */
function addHandler(type, handler) {
handlers[type] = handlers[type] || [];
handlers[type].push(handler);
return () => {
const typeHandlers = handlers[type];
if (typeHandlers) {
const index = typeHandlers.indexOf(handler);
if (index !== -1) typeHandlers.splice(index, 1);
}
};
}
/**
* Reset all instrumentation handlers.
* This can be used by tests to ensure we have a clean slate of instrumentation handlers.
*/
function resetInstrumentationHandlers() {
Object.keys(handlers).forEach((key) => {
handlers[key] = void 0;
});
}
/** Maybe run an instrumentation function, unless it was already called. */
function maybeInstrument(type, instrumentFn) {
if (!instrumented[type]) {
instrumented[type] = true;
try {
instrumentFn();
} catch (e) {
DEBUG_BUILD && debug.error(`Error while instrumenting ${type}`, e);
}
}
}
/** Trigger handlers for a given instrumentation type. */
function triggerHandlers(type, data) {
const typeHandlers = type && handlers[type];
if (!typeHandlers) return;
for (const handler of typeHandlers) try {
handler(data);
} catch (e) {
DEBUG_BUILD && debug.error(`Error while triggering instrumentation handler.\nType: ${type}\nName: ${getFunctionName(handler)}\nError:`, e);
}
}
export { addHandler, maybeInstrument, resetInstrumentationHandlers, triggerHandlers };
//# sourceMappingURL=handlers.js.map