From 1fcaa481be5d839e482a25587527863f8733f335 Mon Sep 17 00:00:00 2001 From: Graeme Yeates Date: Tue, 23 Jun 2015 13:04:31 -0400 Subject: [PATCH] Reduce debounce redundancy for executing bound function --- lodash.src.js | 41 +++++++++++++++++------------------------ 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/lodash.src.js b/lodash.src.js index 762435464..0cd5c13d2 100644 --- a/lodash.src.js +++ b/lodash.src.js @@ -7821,32 +7821,12 @@ maxTimeoutId = timeoutId = trailingCall = undefined; } - function delayed() { - var remaining = wait - (now() - stamp); - if (remaining <= 0 || remaining > wait) { - if (maxTimeoutId) { - clearTimeout(maxTimeoutId); - } - var isCalled = trailingCall; - maxTimeoutId = timeoutId = trailingCall = undefined; - if (isCalled) { - lastCalled = now(); - result = func.apply(thisArg, args); - if (!timeoutId && !maxTimeoutId) { - args = thisArg = undefined; - } - } - } else { - timeoutId = setTimeout(delayed, remaining); - } - } - - function maxDelayed() { - if (timeoutId) { - clearTimeout(timeoutId); + function executeBoundFunction(shouldExecute, clearTimeoutId) { + if (clearTimeoutId) { + clearTimeout(clearTimeoutId); } maxTimeoutId = timeoutId = trailingCall = undefined; - if (trailing || (maxWait !== wait)) { + if (shouldExecute) { lastCalled = now(); result = func.apply(thisArg, args); if (!timeoutId && !maxTimeoutId) { @@ -7855,6 +7835,19 @@ } } + function delayed() { + var remaining = wait - (now() - stamp); + if (remaining <= 0 || remaining > wait) { + executeBoundFunction(trailingCall, maxTimeoutId); + } else { + timeoutId = setTimeout(delayed, remaining); + } + } + + function maxDelayed() { + executeBoundFunction(trailing || (maxWait !== wait), timeoutId); + } + function debounced() { args = arguments; stamp = now();