Ensure _.throttle & _.debounce work if the system time is set backwards.

This commit is contained in:
John-David Dalton
2014-02-13 22:26:26 -08:00
parent bc3b8e73fe
commit fb2bafb49e
2 changed files with 39 additions and 6 deletions

View File

@@ -4889,7 +4889,7 @@
}
var delayed = function() {
var remaining = wait - (now() - stamp);
if (remaining <= 0) {
if (remaining <= 0 || remaining > wait) {
if (maxTimeoutId) {
clearTimeout(maxTimeoutId);
}
@@ -4934,7 +4934,7 @@
lastCalled = stamp;
}
var remaining = maxWait - (stamp - lastCalled),
isCalled = remaining <= 0;
isCalled = remaining <= 0 || remaining > maxWait;
if (isCalled) {
if (maxTimeoutId) {

View File

@@ -8046,12 +8046,12 @@
var actual = [];
var object = {
'func': func(function() { actual.push(this); }, 32)
'funced': func(function() { actual.push(this); }, 32)
};
object.func();
object.funced();
if (methodName == 'throttle') {
object.func();
object.funced();
}
setTimeout(function() {
deepEqual(actual, methodName == 'throttle' ? [object, object] : [object]);
@@ -8059,7 +8059,40 @@
}, 64);
}
else {
skipTest(1);
skipTest();
QUnit.start();
}
});
asyncTest('_.' + methodName + ' should work if system time is set backwards', 1, function() {
if (!isModularize) {
var callCount = 0,
dateCount = 0;
var getTime = function() {
return ++dateCount < 2 ? +new Date : +new Date(2012, 3, 23, 23, 27, 18);
};
var lodash = _.runInContext(_.assign({}, root, {
'Date': function() {
return { 'getTime': getTime, 'valueOf': getTime };
}
}));
var funced = lodash[methodName](function() {
callCount++;
}, 32);
funced();
setTimeout(function() {
funced();
equal(callCount, methodName == 'throttle' ? 2 : 1);
QUnit.start();
}, 64);
}
else {
skipTest();
QUnit.start();
}
});