observe.js 872 B

12345678910111213141516171819202122232425262728293031323334353637
  1. /**
  2. * Takes a performance entry type and a callback function, and creates a
  3. * `PerformanceObserver` instance that will observe the specified entry type
  4. * with buffering enabled and call the callback _for each entry_.
  5. *
  6. * This function also feature-detects entry support and wraps the logic in a
  7. * try/catch to avoid errors in unsupporting browsers.
  8. */
  9. const observe = (
  10. type,
  11. callback,
  12. opts,
  13. ) => {
  14. try {
  15. if (PerformanceObserver.supportedEntryTypes.includes(type)) {
  16. const po = new PerformanceObserver(list => {
  17. callback(list.getEntries() );
  18. });
  19. po.observe(
  20. Object.assign(
  21. {
  22. type,
  23. buffered: true,
  24. },
  25. opts || {},
  26. ) ,
  27. );
  28. return po;
  29. }
  30. } catch (e) {
  31. // Do nothing.
  32. }
  33. return;
  34. };
  35. export { observe };
  36. //# sourceMappingURL=observe.js.map