From daf166553b5a2ebe2b95035c1fe1bb20b0178f03 Mon Sep 17 00:00:00 2001 From: Evan You Date: Wed, 23 Jan 2019 19:08:51 -0500 Subject: [PATCH] refactor: also check for event timestamp --- packages/runtime-dom/src/modules/events.ts | 33 ++++++++++++++-------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/packages/runtime-dom/src/modules/events.ts b/packages/runtime-dom/src/modules/events.ts index 11b37454..92b5914e 100644 --- a/packages/runtime-dom/src/modules/events.ts +++ b/packages/runtime-dom/src/modules/events.ts @@ -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(() => { - cachedNow = 0 - }) - return (cachedNow = performance.now()) - } +const reset = () => { + cachedNow = 0 } +const getNow = () => cachedNow || (p.then(reset), (cachedNow = _getNow())) export function patchEvent( el: Element,