this repo has no description
at main 63 lines 2.1 kB view raw
1import { bindReporter } from './lib/bindReporter.js'; 2import { getVisibilityWatcher } from './lib/getVisibilityWatcher.js'; 3import { initMetric } from './lib/initMetric.js'; 4import { observe } from './lib/observe.js'; 5import { onHidden } from './lib/onHidden.js'; 6 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/** 24 * Calculates the [FID](https://web.dev/fid/) value for the current page and 25 * calls the `callback` function once the value is ready, along with the 26 * relevant `first-input` performance entry used to determine the value. The 27 * reported value is a `DOMHighResTimeStamp`. 28 * 29 * _**Important:** since FID is only reported after the user interacts with the 30 * page, it's possible that it will not be reported for some page loads._ 31 */ 32const onFID = (onReport) => { 33 const visibilityWatcher = getVisibilityWatcher(); 34 const metric = initMetric('FID'); 35 // eslint-disable-next-line prefer-const 36 let report; 37 38 const handleEntry = (entry) => { 39 // Only report if the page wasn't hidden prior to the first input. 40 if (entry.startTime < visibilityWatcher.firstHiddenTime) { 41 metric.value = entry.processingStart - entry.startTime; 42 metric.entries.push(entry); 43 report(true); 44 } 45 }; 46 47 const handleEntries = (entries) => { 48 (entries ).forEach(handleEntry); 49 }; 50 51 const po = observe('first-input', handleEntries); 52 report = bindReporter(onReport, metric); 53 54 if (po) { 55 onHidden(() => { 56 handleEntries(po.takeRecords() ); 57 po.disconnect(); 58 }, true); 59 } 60}; 61 62export { onFID }; 63//# sourceMappingURL=getFID.js.map