refactor: also check for event timestamp

This commit is contained in:
Evan You 2019-01-23 19:08:51 -05:00
parent 16270edda5
commit daf166553b

View File

@ -7,22 +7,31 @@ type EventValue = (Function | Function[]) & {
invoker?: Invoker | null
}
// async edge case fix requires storing an event listener's attach timestamp
// to avoid the overhead of repeatedly calling performance.now(), we cache
// Async edge case fix requires storing an event listener's attach timestamp.
let _getNow: () => number = Date.now
// Determine what event timestamp the browser is using. Annoyingly, the
// timestamp can either be hi-res ( relative to poge load) or low-res
// (relative to UNIX epoch), so in order to compare time we have to use the
// same timestamp type when saving the flush timestamp.
if (
typeof document !== 'undefined' &&
_getNow() > document.createEvent('Event').timeStamp
) {
// if the low-res timestamp which is bigger than the event timestamp
// (which is evaluated AFTER) it means the event is using a hi-res timestamp,
// and we need to use the hi-res version for event listeners as well.
_getNow = () => performance.now()
}
// To avoid the overhead of repeatedly calling performance.now(), we cache
// and use the same timestamp for all event listners attached in the same tick.
let cachedNow: number = 0
const p = Promise.resolve()
function getNow() {
if (cachedNow) {
return cachedNow
} else {
p.then(() => {
const reset = () => {
cachedNow = 0
})
return (cachedNow = performance.now())
}
}
const getNow = () => cachedNow || (p.then(reset), (cachedNow = _getNow()))
export function patchEvent(
el: Element,