Skip to content

Commit

Permalink
refactor: simplify timestamp check
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 24, 2019
1 parent 60a277c commit 32072e8
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions src/core/observer/scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,18 @@ function resetSchedulerState () {
// attached during that flush.
export let currentFlushTimestamp = 0

let getNow
if (inBrowser) {
// 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.
const lowResNow = Date.now()
const eventTimestamp = document.createEvent('Event').timeStamp
// the event timestamp is created after Date.now(), if it's smaller
// it means it's using a hi-res timestamp.
getNow = eventTimestamp < lowResNow
? () => performance.now() // hi-res
: Date.now // low-res
} else {
getNow = Date.now
// 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 (inBrowser && 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()
}

/**
Expand Down

0 comments on commit 32072e8

Please sign in to comment.