getFID.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. const bindReporter = require('./lib/bindReporter.js');
  3. const getVisibilityWatcher = require('./lib/getVisibilityWatcher.js');
  4. const initMetric = require('./lib/initMetric.js');
  5. const observe = require('./lib/observe.js');
  6. const onHidden = require('./lib/onHidden.js');
  7. /*
  8. * Copyright 2020 Google LLC
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * https://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. */
  22. /**
  23. * Calculates the [FID](https://web.dev/fid/) value for the current page and
  24. * calls the `callback` function once the value is ready, along with the
  25. * relevant `first-input` performance entry used to determine the value. The
  26. * reported value is a `DOMHighResTimeStamp`.
  27. *
  28. * _**Important:** since FID is only reported after the user interacts with the
  29. * page, it's possible that it will not be reported for some page loads._
  30. */
  31. const onFID = (onReport) => {
  32. const visibilityWatcher = getVisibilityWatcher.getVisibilityWatcher();
  33. const metric = initMetric.initMetric('FID');
  34. // eslint-disable-next-line prefer-const
  35. let report;
  36. const handleEntry = (entry) => {
  37. // Only report if the page wasn't hidden prior to the first input.
  38. if (entry.startTime < visibilityWatcher.firstHiddenTime) {
  39. metric.value = entry.processingStart - entry.startTime;
  40. metric.entries.push(entry);
  41. report(true);
  42. }
  43. };
  44. const handleEntries = (entries) => {
  45. (entries ).forEach(handleEntry);
  46. };
  47. const po = observe.observe('first-input', handleEntries);
  48. report = bindReporter.bindReporter(onReport, metric);
  49. if (po) {
  50. onHidden.onHidden(() => {
  51. handleEntries(po.takeRecords() );
  52. po.disconnect();
  53. }, true);
  54. }
  55. };
  56. exports.onFID = onFID;
  57. //# sourceMappingURL=getFID.js.map