Skip to content

Instantly share code, notes, and snippets.

@cvazac
Created September 14, 2017 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cvazac/ad97b8fcac33bb02b3367c8180f71c3b to your computer and use it in GitHub Desktop.
Save cvazac/ad97b8fcac33bb02b3367c8180f71c3b to your computer and use it in GitHub Desktop.
!(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