Skip to content
New issue

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

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

Already on GitHub? # to your account

Proposal: Add throttle #396

Closed
adrianrudnik opened this issue Nov 21, 2023 · 1 comment
Closed

Proposal: Add throttle #396

adrianrudnik opened this issue Nov 21, 2023 · 1 comment

Comments

@adrianrudnik
Copy link

Based on lodash _.throttle.

Use case: I want to publish an update to a websocket at most any given time.Duration. So basically like the already existing debounce, but limiting with delay, not delaying until end.

Working with onboard functions, I came up with something like this:

delay := 500 * time.Millisecond

internalInvoke, cancel = lo.NewDebounceBy(delay, func(key int64, count int) {
    // .... do stuff
})

throttle = func() {
    internalInvoke(time.Now().Round(delay).UnixNano())
}

So in the end, calling throttle() will put the rounded nano time in a bucket for debounce and that bucket will be triggered after delay is no longer inside the same rounding range.

Not sure what to do with cancel though.

@Lee-Minjea
Copy link

Lee-Minjea commented Feb 13, 2024

I was looking for 'throttle' too.

Hi @samber, if there is no PR working on this currently.
I would like to make one.

and Please pardon my absence of acknowledge on twitter, I don't have twitter account.

Lee-Minjea pushed a commit to Lee-Minjea/lo that referenced this issue Aug 27, 2024
Lee-Minjea pushed a commit to Lee-Minjea/lo that referenced this issue Aug 28, 2024
Lee-Minjea pushed a commit to Lee-Minjea/lo that referenced this issue Aug 28, 2024
samber pushed a commit to Lee-Minjea/lo that referenced this issue Jan 26, 2025
@samber samber closed this as completed Jan 26, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants