Add flush to debounced methods.

This commit is contained in:
John-David Dalton
2015-10-20 21:49:01 -07:00
parent 602e5d2766
commit 35c6db1289
2 changed files with 40 additions and 4 deletions

View File

@@ -7904,10 +7904,10 @@
* Creates a debounced function that delays invoking `func` until after `wait`
* milliseconds have elapsed since the last time the debounced function was
* invoked. The debounced function comes with a `cancel` method to cancel
* delayed invocations. Provide an options object to indicate that `func`
* should be invoked on the leading and/or trailing edge of the `wait` timeout.
* Subsequent calls to the debounced function return the result of the last
* `func` invocation.
* delayed `func` invocations and a `flush` method to immediately invoke them.
* Provide an options object to indicate that `func` should be invoked on the
* leading and/or trailing edge of the `wait` timeout. Subsequent calls to the
* debounced function return the result of the last `func` invocation.
*
* **Note:** If `leading` and `trailing` options are `true`, `func` is invoked
* on the trailing edge of the timeout only if the the debounced function is
@@ -8020,6 +8020,14 @@
}
}
function flush() {
if ((timeoutId && trailingCall) || (maxTimeoutId && trailing)) {
result = func.apply(thisArg, args);
}
cancel();
return result;
}
function maxDelayed() {
complete(trailing, timeoutId);
}
@@ -8066,6 +8074,7 @@
return result;
}
debounced.cancel = cancel;
debounced.flush = flush;
return debounced;
}

View File

@@ -18732,6 +18732,33 @@
done();
}
});
QUnit.test('_.' + methodName + ' should support flushing delayed calls', function(assert) {
assert.expect(2);
var done = assert.async();
if (!(isRhino && isModularize)) {
var callCount = 0;
var funced = func(function() {
return ++callCount;
}, 32, { 'leading': false });
funced();
var actual = funced.flush();
setTimeout(function() {
assert.strictEqual(actual, 1);
assert.strictEqual(callCount, 1);
done();
}, 64);
}
else {
skipTest(assert, 2);
done();
}
});
});
/*--------------------------------------------------------------------------*/