Created
September 14, 2017 15:09
-
-
Save cvazac/ad97b8fcac33bb02b3367c8180f71c3b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!(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