]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
refactor: also check for event timestamp
authorEvan You <yyx990803@gmail.com>
Thu, 24 Jan 2019 00:08:51 +0000 (19:08 -0500)
committerEvan You <yyx990803@gmail.com>
Thu, 24 Jan 2019 00:08:51 +0000 (19:08 -0500)
packages/runtime-dom/src/modules/events.ts

index 11b374545b8760e1c2097c57b3dcda389198428b..92b5914e7c1a91b97b1ba9e6ffe4ff9d030124aa 100644 (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(() => {
-      cachedNow = 0
-    })
-    return (cachedNow = performance.now())
-  }
+const reset = () => {
+  cachedNow = 0
 }
+const getNow = () => cachedNow || (p.then(reset), (cachedNow = _getNow()))
 
 export function patchEvent(
   el: Element,