_handlers.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. const debugBuild = require('../debug-build.js');
  3. const logger = require('../logger.js');
  4. const stacktrace = require('../stacktrace.js');
  5. // We keep the handlers globally
  6. const handlers = {};
  7. const instrumented = {};
  8. /** Add a handler function. */
  9. function addHandler(type, handler) {
  10. handlers[type] = handlers[type] || [];
  11. (handlers[type] ).push(handler);
  12. }
  13. /**
  14. * Reset all instrumentation handlers.
  15. * This can be used by tests to ensure we have a clean slate of instrumentation handlers.
  16. */
  17. function resetInstrumentationHandlers() {
  18. Object.keys(handlers).forEach(key => {
  19. handlers[key ] = undefined;
  20. });
  21. }
  22. /** Maybe run an instrumentation function, unless it was already called. */
  23. function maybeInstrument(type, instrumentFn) {
  24. if (!instrumented[type]) {
  25. instrumentFn();
  26. instrumented[type] = true;
  27. }
  28. }
  29. /** Trigger handlers for a given instrumentation type. */
  30. function triggerHandlers(type, data) {
  31. const typeHandlers = type && handlers[type];
  32. if (!typeHandlers) {
  33. return;
  34. }
  35. for (const handler of typeHandlers) {
  36. try {
  37. handler(data);
  38. } catch (e) {
  39. debugBuild.DEBUG_BUILD &&
  40. logger.logger.error(
  41. `Error while triggering instrumentation handler.\nType: ${type}\nName: ${stacktrace.getFunctionName(handler)}\nError:`,
  42. e,
  43. );
  44. }
  45. }
  46. }
  47. exports.addHandler = addHandler;
  48. exports.maybeInstrument = maybeInstrument;
  49. exports.resetInstrumentationHandlers = resetInstrumentationHandlers;
  50. exports.triggerHandlers = triggerHandlers;
  51. //# sourceMappingURL=_handlers.js.map