-
Notifications
You must be signed in to change notification settings - Fork 9
Description
Environment
perfect-debounce version 2.0.0
node version v20.19.4
Reproduction
import { debounce } from "perfect-debounce";
let debounced = debounce(async (value) => {
console.log(value);
}, 100, {trailing: true});
debounced(1);
debounced(2);
debounced(3);
debounced(4);
// 4 as expected
debounced = debounce(async (value) => {
console.log(value);
}, 100, {leading: true, trailing: true});
debounced(1);
debounced(2);
debounced(3);
debounced(4);
// 1 and 4 expected
// 1 producedDescribe the bug
When trailing is true I would expect that there would be a trailing call whether leading is also true or not. However this is not the case. Maybe this issue is PEBKAC and it's just standard behaviour for a debounce function for setting "leading" to cancel out a "trailing" call. But if so it might helpful to state that in the doc for silly people like me.
Although even if that is expected behaviour it would be nice to have the option for a trailing call to be produced even if there's a leading one. That's the behaviour I'm looking for in my situation anyway where I have an api call which I want to be hit immediately so it doesn't feel slow to the user if they only request it once. But if they spam it I don't want to also flood the api with requests but I do need it to eventually be called with the final call once the user stops spamming it.
Actually in my ideal situation if the user is calling the function repeatedly every 10 ms and the wait time for the debounce function is 100 then I'd ideally like it to be called the first time then on the 10th time (which would be the wait time of 100ms after the first) then on the 20th time (another 100ms later) and so on until the user stops calling after which it would be called a final time once the wait time has been reached. I guess that's more of a throttling function though than a debounce function.
Additional context
No response