declare global {
interface Window {
____lastLongTask: number
____lhPerformanceObserver: PerformanceObserver
}
}
* Used by _waitForCPUIdle and executed in the context of the page, updates the ____lastLongTask
* property on window to the end time of the last long task.
*/
export default function registerPerformanceObserverInPage () {
window.____lastLongTask = window.performance.now()
const observer = new window.PerformanceObserver(entryList => {
const entries = entryList.getEntries()
for (const entry of entries) {
if (entry.entryType === 'longtask') {
const taskEnd = entry.startTime + entry.duration
window.____lastLongTask = Math.max(window.____lastLongTask, taskEnd)
}
}
})
observer.observe({ entryTypes: ['longtask'] })
window.____lhPerformanceObserver = observer
}