From 49f8aa428942890f1804024888995ce66ed11985 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Wed, 13 Apr 2016 18:17:07 -0700 Subject: [PATCH] Fix failing debounce test. --- lodash.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/lodash.js b/lodash.js index 703224baa..7a1fffbe5 100644 --- a/lodash.js +++ b/lodash.js @@ -9372,12 +9372,13 @@ function debounce(func, wait, options) { var lastArgs, lastThis, + maxWait, result, timerId, lastCallTime = 0, lastInvokeTime = 0, leading = false, - maxWait = false, + maxing = false, trailing = true; if (typeof func != 'function') { @@ -9386,7 +9387,8 @@ wait = toNumber(wait) || 0; if (isObject(options)) { leading = !!options.leading; - maxWait = 'maxWait' in options && nativeMax(toNumber(options.maxWait) || 0, wait); + maxing = 'maxWait' in options; + maxWait = maxing ? nativeMax(toNumber(options.maxWait) || 0, wait) : maxWait; trailing = 'trailing' in options ? !!options.trailing : trailing; } @@ -9414,7 +9416,7 @@ timeSinceLastInvoke = time - lastInvokeTime, result = wait - timeSinceLastCall; - return maxWait === false ? result : nativeMin(result, maxWait - timeSinceLastInvoke); + return maxing ? nativeMin(result, maxWait - timeSinceLastInvoke) : result; } function shouldInvoke(time) { @@ -9425,7 +9427,7 @@ // trailing edge, the system time has gone backwards and we're treating // it as the trailing edge, or we've hit the `maxWait` limit. return (!lastCallTime || (timeSinceLastCall >= wait) || - (timeSinceLastCall < 0) || (maxWait !== false && timeSinceLastInvoke >= maxWait)); + (timeSinceLastCall < 0) || (maxing && timeSinceLastInvoke >= maxWait)); } function timerExpired() { @@ -9474,10 +9476,12 @@ if (timerId === undefined) { return leadingEdge(lastCallTime); } - // Handle invocations in a tight loop. - clearTimeout(timerId); - timerId = setTimeout(timerExpired, wait); - return invokeFunc(lastCallTime); + if (maxing) { + // Handle invocations in a tight loop. + clearTimeout(timerId); + timerId = setTimeout(timerExpired, wait); + return invokeFunc(lastCallTime); + } } if (timerId === undefined) { timerId = setTimeout(timerExpired, wait); @@ -14698,7 +14702,7 @@ object = this; methodNames = baseFunctions(source, keys(source)); } - var chain = (isObject(options) && 'chain' in options) ? options.chain : true, + var chain = !(isObject(options) && 'chain' in options) || !!options.chain, isFunc = isFunction(object); arrayEach(methodNames, function(methodName) {