From bd3034819e54550d152967d2ac32eedb773b6280 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Tue, 5 Nov 2013 23:48:07 -0800 Subject: [PATCH] Ensure recursive `_.throttle` calls still work. --- lodash.js | 13 ++++++++++--- test/test.js | 20 +++++++++++++------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/lodash.js b/lodash.js index ce7307955..3c69b34f4 100644 --- a/lodash.js +++ b/lodash.js @@ -5697,7 +5697,9 @@ if (isCalled) { lastCalled = now(); result = func.apply(thisArg, args); - args = thisArg = null; + if (!timeoutId && !maxTimeoutId) { + args = thisArg = null; + } } } else { timeoutId = setTimeout(delayed, remaining); @@ -5712,7 +5714,9 @@ if (trailing || (maxWait !== wait)) { lastCalled = now(); result = func.apply(thisArg, args); - args = thisArg = null; + if (!timeoutId && !maxTimeoutId) { + args = thisArg = null; + } } }; @@ -5737,7 +5741,6 @@ } lastCalled = stamp; result = func.apply(thisArg, args); - args = thisArg = null; } else if (!maxTimeoutId) { maxTimeoutId = setTimeout(maxDelayed, remaining); @@ -5750,8 +5753,12 @@ timeoutId = setTimeout(delayed, wait); } if (leadingCall) { + isCalled = true; result = func.apply(thisArg, args); } + if (isCalled && !timeoutId && !maxTimeoutId) { + args = thisArg = null; + } return result; }; } diff --git a/test/test.js b/test/test.js index 200659d59..95d36e583 100644 --- a/test/test.js +++ b/test/test.js @@ -6889,26 +6889,32 @@ } }); - asyncTest('supports recursive calls', 2, function() { + asyncTest('supports recursive calls', 3, function() { if (!(isRhino && isModularize)) { - var count = 0; - var throttled = _.throttle(function() { + var args, + count = 0, + object = {}; + + var throttled = _.throttle(function(value) { count++; + args = [this]; + push.apply(args, arguments); if (count < 10) { - throttled(); + throttled.call(this, value); } }, 32); - throttled(); + throttled.call(object, 'a'); equal(count, 1); setTimeout(function() { - ok(count < 3) + ok(count < 3); + deepEqual(args, [object, 'a']); QUnit.start(); }, 32); } else { - skipTest(2); + skipTest(3); QUnit.start(); } });