Skip to content

Commit

Permalink
Normalize .cancel API
Browse files Browse the repository at this point in the history
  • Loading branch information
stamoern authored and niksy committed Apr 29, 2022
1 parent a062d17 commit 699a94d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,17 @@ debounceFunc.cancel();

The logic that is being throttled or debounced will no longer be called.

To cancel only one upcoming debounced call, you can pass `true` to
`cancel` function:
To cancel only one upcoming debounced call, you can pass `upcomingOnly: true`
option to `cancel` function:

```js
const debounceFunc = debounce(300, () => {
// Debounced function
});

debounceFunc(); // will not be invoked

debounceFunc.cancel(true);
debounceFunc.cancel({ upcomingOnly: true });

debounceFunc(); // will be invoked
```
Expand Down Expand Up @@ -231,7 +232,7 @@ support).

<!-- prettier-ignore-start -->

**Original module license:** Copyright (c) 2010 "Cowboy" Ben Alman (Dual licensed under the MIT and GPL licenses. http://benalman.com/about/license/)
**Original module license:** Copyright (c) 2010 "Cowboy" Ben Alman (Dual licensed under the MIT and GPL licenses. http://benalman.com/about/license/)
**This module license:** MIT © [Ivan Nikolić](http://ivannikolic.com)

[ci]: https://github.com/niksy/throttle-debounce/actions?query=workflow%3ACI
Expand Down
4 changes: 2 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -585,14 +585,14 @@ test('cancel (running only)', function () {

let array = [];
let function_ = function () {
array.push(Number(new Date()));
array.push(Date.now());
};
let debounced = debounce(delay, function_);

debounced.call();

setTimeout(function () {
debounced.cancel(true);
debounced.cancel({ upcomingOnly: true });
}, delay / 2);

setTimeout(function () {
Expand Down
4 changes: 2 additions & 2 deletions throttle.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ export default function (delay, callback, options) {
}

// Function to cancel next exec
function cancel(upcomingOnly) {
function cancel(options) {
clearExistingTimeout();
cancelled = !upcomingOnly;
cancelled = !(options && options.upcomingOnly);
}

/*
Expand Down

0 comments on commit 699a94d

Please sign in to comment.