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

Cancel debouce #87

Closed
hlspablo opened this issue Sep 19, 2022 · 9 comments · Fixed by #118
Closed

Cancel debouce #87

hlspablo opened this issue Sep 19, 2022 · 9 comments · Fixed by #118

Comments

@hlspablo
Copy link

hlspablo commented Sep 19, 2022

there is some way to cancel debounced function's execution?, this is a very useful use case and since this lib wants to replace lodash, a think this is a must have

@sodiray
Copy link
Owner

sodiray commented Oct 13, 2022

Hey @hlspablo thanks for bringing this up. I don't think I understand the use case. Can you maybe provide some pseudo code for what you're looking for? Just to help me understand :)

@hlspablo
Copy link
Author

const req = debounce({ delay: 100 }, makeSearchRequest)

where req.cancel() cancel the makeSearchRequest execution if called before the delay. It can be very handy in a lot of scenarios.

@sodiray
Copy link
Owner

sodiray commented Oct 16, 2022

I see how this could be helpful, happy to build it (or merge a PR if someone else wants to build it) to support it.

One question, since I've never needed it personally, to make sure I understand the behavior: Should cancel cancel the next scheduled invocation or does it cancel all future invocations?

@hlspablo
Copy link
Author

hlspablo commented Oct 16, 2022

in a simpler implementation, should cancel all future invocations, maybe in the future it could evolve to cancel n invocations passed to cancel, but I think that cancelling all should help 99% of use cases.

@MarlonPassos-git
Copy link
Contributor

@hlspablo I'm developing a beta version that solves this problem and I managed to solve it for both future and past debounces, but with that I have a doubt.
Would it be interesting to create a method that allows debounce to work again? Or know what his status is?

I'm looking at lodash and it has flush and pending methods. If you find it interesting, I can add it too.

@hlspablo
Copy link
Author

@MarlonPassos-git I don't think that this complexity is necessary, make it work again is almost the same of calling the debounced func with new parameters. I like the ideia of implementing flush too, could be very useful to fire the debounced functions immediately. @rayepps what do you think?

@sodiray
Copy link
Owner

sodiray commented Oct 21, 2022

I like the idea of making it as simple as create and cancel. In a way, the debounce can be immutable. If you need to pause you can cancel then create a new function when you want to resume.

@hlspablo
Copy link
Author

A simple immutable debounce, with cancel and flush methods looks perfect to me.

@sodiray
Copy link
Owner

sodiray commented Dec 12, 2022

This is out and available in v10.1.0 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants