bindReporter.js 1.3 KB

123456789101112131415161718192021222324252627282930313233
  1. /*
  2. * Copyright 2020 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * https://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. export const bindReporter = (callback, metric, reportAllChanges) => {
  17. let prevValue;
  18. return (forceReport) => {
  19. if (metric.value >= 0) {
  20. if (forceReport || reportAllChanges) {
  21. metric.delta = metric.value - (prevValue || 0);
  22. // Report the metric if there's a non-zero delta or if no previous
  23. // value exists (which can happen in the case of the document becoming
  24. // hidden when the metric value is 0).
  25. // See: https://github.com/GoogleChrome/web-vitals/issues/14
  26. if (metric.delta || prevValue === undefined) {
  27. prevValue = metric.value;
  28. callback(metric);
  29. }
  30. }
  31. }
  32. };
  33. };