[Time Conductor] Fix throttle bug

Fix a timing/ordering issue in throttle which
allowed some throttled invocations to be ignored.
WTD-1515
This commit is contained in:
Victor Woeltjen 2015-09-16 11:04:07 -07:00
parent 7a97588aa5
commit 071368c3b9

View File

@ -61,18 +61,14 @@ define(
* @memberof platform/core.Throttle# * @memberof platform/core.Throttle#
*/ */
return function (fn, delay, apply) { return function (fn, delay, apply) {
var activeTimeout, var promise, // Promise for the result of throttled function
args = []; 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() { 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 // Defaults
@ -83,13 +79,10 @@ define(
// Store arguments from this invocation // Store arguments from this invocation
args = Array.prototype.slice.apply(arguments, [0]); args = Array.prototype.slice.apply(arguments, [0]);
// Start a timeout if needed // Start a timeout if needed
if (!activeTimeout) { promise = promise || $timeout(invoke, delay, apply);
activeTimeout = $timeout(invoke, delay, apply);
activeTimeout.then(clearActiveTimeout);
}
// Return whichever timeout is active (to get // Return whichever timeout is active (to get
// a promise for the results of fn) // a promise for the results of fn)
return activeTimeout; return promise;
}; };
}; };
} }