Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pending is set to true even when there are no "pendingInvokes" in debounce #5633

Closed
JoSuzuki opened this issue Apr 19, 2023 · 2 comments
Closed
Labels
issue bankruptcy Closing the issue/PR to start fresh

Comments

@JoSuzuki
Copy link

This is more of a question which may possibly be a bug.

I was using the debouce function present on master specifically for the feature of pending.

The question is: Considering that a function log is debounced with leading: true, generating logDebounced. If logDebounced is called only once, during the wait period, should pending be true or false?

Currently it outputs true, but I believe that if there are no "pending invokes" it should output false. This would be helpful in cases where we are watching this variable to block navigation/show a loading state, since no more calls would be made we shouldn't need to wait for the wait time to expire.

The example in code is

import debounce from "./debounce.js";

const WAIT = 1000;

const log = (text) => {
  console.log("log", text);
};

const logDebounced = debounce(log, WAIT, { leading: true });

logDebounced("1");

console.log(logDebounced.pending()); // outputs true

setTimeout(() => {
  console.log(logDebounced.pending());
}, WAIT);
@JoSuzuki
Copy link
Author

A possible patch could be updating the pending function to watch for lastArgs (meaning it was called once after being invoked) and if it will call on trailing.

function pending() {
  return timerId !== undefined && lastArgs && trailing;
}

I think that there is the argument that a pendingTimer concept also exists, where we need to see if any timer is currently running.

@anmol242
Copy link

The example in code is

import debounce from "./debounce.js";

const WAIT = 1000;

const log = (text) => {
  console.log("log", text);
};

const logDebounced = debounce(log, WAIT, { leading: true });

logDebounced("1");

console.log(logDebounced.pending()); // outputs true

setTimeout(() => {
  console.log(logDebounced.pending());
}, WAIT);

Hm, I agree the behavior you described, where logDebounced.pending() outputs true, is expected based on the implementation of the debounce function in Lodash. The pending function returns true if there are any pending invocations, regardless of whether they are yet to be executed or have already been executed during the wait period.

I can work on the fix if you want.

@jdalton jdalton added the issue bankruptcy Closing the issue/PR to start fresh label Sep 16, 2023
@jdalton jdalton closed this as completed Sep 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
issue bankruptcy Closing the issue/PR to start fresh
Development

No branches or pull requests

3 participants