From ef527df381fff1765b34b217fe2a1b9375d1e875 Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Wed, 16 Sep 2015 11:04:07 -0700 Subject: [PATCH] [Time Conductor] Fix throttle bug Fix a timing/ordering issue in throttle which allowed some throttled invocations to be ignored. WTD-1515 --- platform/core/src/services/Throttle.js | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/platform/core/src/services/Throttle.js b/platform/core/src/services/Throttle.js index eda6713dec..60444ad6c4 100644 --- a/platform/core/src/services/Throttle.js +++ b/platform/core/src/services/Throttle.js @@ -61,18 +61,14 @@ define( * @memberof platform/core.Throttle# */ return function (fn, delay, apply) { - var activeTimeout, + var promise, // Promise for the result of throttled function args = []; - // Clear active timeout, so that next invocation starts - // a new one. - function clearActiveTimeout() { - activeTimeout = undefined; - } - - // Invoke the function with the latest supplied arguments. function invoke() { - fn.apply(null, args); + // Clear the active timeout so a new one starts next time. + promise = undefined; + // Invoke the function with the latest supplied arguments. + return fn.apply(null, args); } // Defaults @@ -83,13 +79,10 @@ define( // Store arguments from this invocation args = Array.prototype.slice.apply(arguments, [0]); // Start a timeout if needed - if (!activeTimeout) { - activeTimeout = $timeout(invoke, delay, apply); - activeTimeout.then(clearActiveTimeout); - } + promise = promise || $timeout(invoke, delay, apply); // Return whichever timeout is active (to get // a promise for the results of fn) - return activeTimeout; + return promise; }; }; }