Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
!(function() {
const nativePO = window.PerformanceObserver
const polyfills = {
longtask: LongTaskPolyFill
// etc.
}
window.PerformanceObserver = (callback) => {
let po, registeredPolyFills = []
const observe = function(options) {
po = new nativePO((entries, observer) => {
callback(entries, this)
})
const filteredEntryTypes = po.observe(options)
(options.entryTypes || []).forEach((entryType) => {
if ((filteredEntryTypes || []).indexOf(entryType) === -1) {
const polyfill = polyfills[entryType]
polyfill && registeredPolyFills.push(polyfill.start(callback))
}
})
if (!filteredEntryTypes.length) {
po.disconnect()
po = undefined
}
return filteredEntryTypes
}
const disconnect = () => {
if (po) {
po.disconnect()
po = undefined
}
registeredPolyFills.forEach(p => p.stop())
registeredPolyFills = []
}
return {
observe,
disconnect
}
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment