mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-31 23:37:49 +00:00
Add flush to debounced methods.
This commit is contained in:
17
lodash.js
17
lodash.js
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
27
test/test.js
27
test/test.js
@@ -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();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
Reference in New Issue
Block a user