Cleanup _.debounce.

This commit is contained in:
John-David Dalton
2013-09-07 02:17:06 -05:00
parent f86322545a
commit e078f584eb

View File

@@ -5433,37 +5433,36 @@
*/ */
function debounce(func, wait, options) { function debounce(func, wait, options) {
var args, var args,
maxTimeoutId,
result, result,
stamp, stamp,
thisArg, thisArg,
callCount = 0, timeoutId,
trailingCall,
lastCalled = 0, lastCalled = 0,
maxWait = false, maxWait = false,
maxTimeoutId = null,
timeoutId = null,
trailing = true; trailing = true;
if (!isFunction(func)) { if (!isFunction(func)) {
throw new TypeError; throw new TypeError;
} }
wait = nativeMax(0, wait || 0); wait = nativeMax(0, wait) || 0;
if (options === true) { if (options === true) {
var leading = true; var leading = true;
trailing = false; trailing = false;
} else if (isObject(options)) { } else if (isObject(options)) {
leading = options.leading; leading = options.leading;
maxWait = 'maxWait' in options && nativeMax(wait, options.maxWait || 0); maxWait = 'maxWait' in options && (nativeMax(wait, options.maxWait) || 0);
trailing = 'trailing' in options ? options.trailing : trailing; trailing = 'trailing' in options ? options.trailing : trailing;
} }
var delayed = function() { var delayed = function() {
var remaining = wait - (new Date - stamp); var remaining = wait - (new Date - stamp);
if (remaining <= 0) { if (remaining <= 0) {
var isCalled = trailing && (!leading || callCount > 1);
if (maxTimeoutId) { if (maxTimeoutId) {
clearTimeout(maxTimeoutId); clearTimeout(maxTimeoutId);
} }
callCount = 0; var isCalled = trailingCall;
maxTimeoutId = timeoutId = null; maxTimeoutId = timeoutId = trailingCall = undefined;
if (isCalled) { if (isCalled) {
lastCalled = +new Date; lastCalled = +new Date;
result = func.apply(thisArg, args); result = func.apply(thisArg, args);
@@ -5477,8 +5476,7 @@
if (timeoutId) { if (timeoutId) {
clearTimeout(timeoutId); clearTimeout(timeoutId);
} }
callCount = 0; maxTimeoutId = timeoutId = trailingCall = undefined;
maxTimeoutId = timeoutId = null;
if (trailing || (maxWait !== wait)) { if (trailing || (maxWait !== wait)) {
lastCalled = +new Date; lastCalled = +new Date;
result = func.apply(thisArg, args); result = func.apply(thisArg, args);
@@ -5489,21 +5487,16 @@
args = arguments; args = arguments;
stamp = +new Date; stamp = +new Date;
thisArg = this; thisArg = this;
callCount++; trailingCall = trailing && (timeoutId || !leading);
if (maxWait === false) { if (maxWait !== false) {
if (leading && callCount < 2) {
result = func.apply(thisArg, args);
}
} else {
if (!maxTimeoutId && !leading) { if (!maxTimeoutId && !leading) {
lastCalled = stamp; lastCalled = stamp;
} }
var remaining = maxWait - (stamp - lastCalled); var remaining = maxWait - (stamp - lastCalled);
if (remaining <= 0) { if (remaining <= 0) {
if (maxTimeoutId) { if (maxTimeoutId) {
clearTimeout(maxTimeoutId); maxTimeoutId = clearTimeout(maxTimeoutId);
maxTimeoutId = null;
} }
lastCalled = stamp; lastCalled = stamp;
result = func.apply(thisArg, args); result = func.apply(thisArg, args);
@@ -5512,6 +5505,9 @@
maxTimeoutId = setTimeout(maxDelayed, remaining); maxTimeoutId = setTimeout(maxDelayed, remaining);
} }
} }
else if (leading && !timeoutId) {
result = func.apply(thisArg, args);
}
if (!timeoutId && wait !== maxWait) { if (!timeoutId && wait !== maxWait) {
timeoutId = setTimeout(delayed, wait); timeoutId = setTimeout(delayed, wait);
} }