[Time Conductor] Allow arguments for throttled functions

WTD-1515. Ensures that bounds passed in from
the time controller get appropriately captured.
This commit is contained in:
Victor Woeltjen 2015-09-16 10:18:57 -07:00
parent 77c66053f3
commit c2868a4573

View File

@ -36,11 +36,16 @@ define(
* *
* Returns a function that, when invoked, will invoke `fn` after * Returns a function that, when invoked, will invoke `fn` after
* `delay` milliseconds, only if no other invocations are pending. * `delay` milliseconds, only if no other invocations are pending.
* The optional argument `apply` determines whether. * The optional argument `apply` determines whether or not a
* digest cycle should be triggered.
* *
* The returned function will itself return a `Promise` which will * The returned function will itself return a `Promise` which will
* resolve to the returned value of `fn` whenever that is invoked. * resolve to the returned value of `fn` whenever that is invoked.
* *
* In cases where arguments are provided, only the most recent
* set of arguments will be passed on to the throttled function
* at the time it is executed.
*
* @returns {Function} * @returns {Function}
* @memberof platform/core * @memberof platform/core
*/ */
@ -56,7 +61,8 @@ define(
* @memberof platform/core.Throttle# * @memberof platform/core.Throttle#
*/ */
return function (fn, delay, apply) { return function (fn, delay, apply) {
var activeTimeout; var activeTimeout,
args = [];
// Clear active timeout, so that next invocation starts // Clear active timeout, so that next invocation starts
// a new one. // a new one.
@ -64,14 +70,21 @@ define(
activeTimeout = undefined; activeTimeout = undefined;
} }
// Invoke the function with the latest supplied arguments.
function invoke() {
fn.apply(null, args);
}
// Defaults // Defaults
delay = delay || 0; delay = delay || 0;
apply = apply || false; apply = apply || false;
return function () { return function () {
// Store arguments from this invocation
args = Array.prototype.slice.apply(arguments, [0]);
// Start a timeout if needed // Start a timeout if needed
if (!activeTimeout) { if (!activeTimeout) {
activeTimeout = $timeout(fn, delay, apply); activeTimeout = $timeout(invoke, delay, apply);
activeTimeout.then(clearActiveTimeout); activeTimeout.then(clearActiveTimeout);
} }
// Return whichever timeout is active (to get // Return whichever timeout is active (to get