60 lines
1.6 KiB
JavaScript
60 lines
1.6 KiB
JavaScript
Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
|
const debugBuild = require('../debug-build.js');
|
|
const logger = require('../logger.js');
|
|
const stacktrace = require('../stacktrace.js');
|
|
|
|
// We keep the handlers globally
|
|
const handlers = {};
|
|
const instrumented = {};
|
|
|
|
/** Add a handler function. */
|
|
function addHandler(type, handler) {
|
|
handlers[type] = handlers[type] || [];
|
|
(handlers[type] ).push(handler);
|
|
}
|
|
|
|
/**
|
|
* 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 ] = undefined;
|
|
});
|
|
}
|
|
|
|
/** Maybe run an instrumentation function, unless it was already called. */
|
|
function maybeInstrument(type, instrumentFn) {
|
|
if (!instrumented[type]) {
|
|
instrumentFn();
|
|
instrumented[type] = true;
|
|
}
|
|
}
|
|
|
|
/** 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) {
|
|
debugBuild.DEBUG_BUILD &&
|
|
logger.logger.error(
|
|
`Error while triggering instrumentation handler.\nType: ${type}\nName: ${stacktrace.getFunctionName(handler)}\nError:`,
|
|
e,
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
exports.addHandler = addHandler;
|
|
exports.maybeInstrument = maybeInstrument;
|
|
exports.resetInstrumentationHandlers = resetInstrumentationHandlers;
|
|
exports.triggerHandlers = triggerHandlers;
|
|
//# sourceMappingURL=_handlers.js.map
|