Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use monotonic clock for elapsed time #5870

Merged
merged 22 commits into from
Apr 24, 2019
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/js/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -1128,7 +1128,7 @@ class Component {
pageY: event.touches[0].pageY
};
// Record start time so we can detect a tap vs. "touch and hold"
touchStart = new Date().getTime();
touchStart = window.performance.now();
// Reset couldBeTap tracking
couldBeTap = true;
}
Expand Down Expand Up @@ -1166,7 +1166,7 @@ class Component {
// Proceed only if the touchmove/leave/cancel event didn't happen
if (couldBeTap === true) {
// Measure how long the touch lasted
const touchTime = new Date().getTime() - touchStart;
const touchTime = window.performance.now() - touchStart;

// Make sure the touch was less than the threshold to be considered a tap
if (touchTime < touchTimeThreshold) {
Expand Down
3 changes: 2 additions & 1 deletion src/js/utils/dom-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @module dom-data
*/
import * as Guid from './guid.js';
import window from 'global/window';

/**
* Element Data Store.
Expand All @@ -23,7 +24,7 @@ const elData = {};
* @constant
* @private
*/
const elIdAttr = 'vdata' + (new Date()).getTime();
const elIdAttr = 'vdata' + Math.floor(window.performance && window.performance.now() || Date.now());

/**
* Returns the cache object where data for an element is stored
Expand Down
4 changes: 2 additions & 2 deletions src/js/utils/fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ export const bind = function(context, fn, uid) {
* @return {Function}
*/
export const throttle = function(fn, wait) {
let last = Date.now();
let last = window.performance.now();

const throttled = function(...args) {
const now = Date.now();
const now = window.performance.now();

if (now - last >= wait) {
fn(...args);
Expand Down
5 changes: 5 additions & 0 deletions test/unit/utils/fn.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-env qunit */
import sinon from 'sinon';
import window from 'global/window';
import * as Fn from '../../../src/js/utils/fn.js';

QUnit.module('fn', {
Expand All @@ -22,6 +23,10 @@ QUnit.test('should add context to a function', function(assert) {
});

QUnit.test('should throttle functions properly', function(assert) {
// stub performance.now for sinon, see:
// https://github.com/sinonjs/sinon/issues/803
sinon.stub(window.performance, 'now', Date.now);

const tester = sinon.spy();
const throttled = Fn.throttle(tester, 100);

Expand Down