Skip to content

Commit

Permalink
BREAKING CHANGE: change definition of throttle options
Browse files Browse the repository at this point in the history
  • Loading branch information
seiyab authored and niksy committed Apr 6, 2022
1 parent 67aab0f commit 358ae92
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
4 changes: 2 additions & 2 deletions debounce.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ import throttle from './throttle';
*/
export default function (delay, atBegin, callback) {
return callback === undefined
? throttle(delay, atBegin, false)
: throttle(delay, callback, atBegin !== false);
? throttle(delay, atBegin, { debounceMode: false })
: throttle(delay, callback, { debounceMode: atBegin !== false });
}
20 changes: 12 additions & 8 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function execManyTimes(each, complete) {

module('throttle');

test('delay, callback', function () {
test('no option', function () {
expect(7);
stop();

Expand Down Expand Up @@ -97,7 +97,7 @@ test('delay, callback', function () {
);
});

test('delay, false, callback', function () {
test('{ noTrailing: false }', function () {
expect(7);
stop();

Expand All @@ -107,7 +107,7 @@ test('delay, false, callback', function () {
let function_ = function (now) {
array.push(now - this);
};
let throttled = throttle(delay, false, function_);
let throttled = throttle(delay, function_, { noTrailing: false });

equals(
throttled.guid,
Expand Down Expand Up @@ -148,7 +148,7 @@ test('delay, false, callback', function () {
);
});

test('delay, true, callback', function () {
test('{ noTrailing: true }', function () {
expect(7);
stop();

Expand All @@ -158,7 +158,7 @@ test('delay, true, callback', function () {
let function_ = function (now) {
array.push(now - this);
};
let throttled = throttle(delay, true, function_);
let throttled = throttle(delay, function_, { noTrailing: true });

equals(
throttled.guid,
Expand Down Expand Up @@ -204,9 +204,13 @@ test('cancel', function () {
stop();

let callCount = 0;
let throttled = throttle(delay * 100, false, function () {
callCount++;
});
let throttled = throttle(
delay * 100,
function () {
callCount++;
},
{ noTrailing: false }
);

equals(1, 1);

Expand Down
30 changes: 13 additions & 17 deletions throttle.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@
* Throttle execution of a function. Especially useful for rate limiting
* execution of handlers on events like resize and scroll.
*
* @param {number} delay - A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.
* @param {boolean} [noTrailing] - Optional, defaults to false. If noTrailing is true, callback will only execute every `delay` milliseconds while the
* throttled-function is being called. If noTrailing is false or unspecified, callback will be executed one final time
* after the last throttled-function call. (After the throttled-function has not been called for `delay` milliseconds,
* the internal counter is reset).
* @param {Function} callback - A function to be executed after delay milliseconds. The `this` context and all arguments are passed through, as-is,
* to `callback` when the throttled-function is executed.
* @param {boolean} [debounceMode] - If `debounceMode` is true (at begin), schedule `clear` to execute after `delay` ms. If `debounceMode` is false (at end),
* schedule `callback` to execute after `delay` ms.
* @param {number} delay - A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher)
* are most useful.
* @param {Function} callback - A function to be executed after delay milliseconds. The `this` context and all arguments are passed through,
* as-is, to `callback` when the throttled-function is executed.
* @param {object} [options] - An object to configure options.
* @param {boolean} [options.noTrailing] - Optional, defaults to false. If noTrailing is true, callback will only execute every `delay` milliseconds
* while the throttled-function is being called. If noTrailing is false or unspecified, callback will be executed
* one final time after the last throttled-function call. (After the throttled-function has not been called for
* `delay` milliseconds, the internal counter is reset).
* @param {boolean} [options.debounceMode] - If `debounceMode` is true (at begin), schedule `clear` to execute after `delay` ms. If `debounceMode` is
* false (at end), schedule `callback` to execute after `delay` ms.
*
* @returns {Function} A new, throttled, function.
*/
export default function (delay, noTrailing, callback, debounceMode) {
export default function (delay, callback, options) {
const { noTrailing = false, debounceMode = undefined } = options || {};
/*
* After wrapper has stopped being called, this timeout ensures that
* `callback` is executed at the proper times in `throttle` and `end`
Expand All @@ -41,13 +44,6 @@ export default function (delay, noTrailing, callback, debounceMode) {
cancelled = true;
}

// `noTrailing` defaults to falsy.
if (typeof noTrailing !== 'boolean') {
debounceMode = callback;
callback = noTrailing;
noTrailing = undefined;
}

/*
* The `wrapper` function encapsulates all of the throttling / debouncing
* functionality and when executed will limit the rate at which `callback`
Expand Down

0 comments on commit 358ae92

Please sign in to comment.