Go Debouncer is a Go language library. It makes sure that the pre-defined function is only triggered once per client's signals during a fixed duration.
It allows creating a debouncer that delays invoking a triggered function until after the duration has elapsed since the last time the SendSingal was invoked.
- Official page: https://godebouncer.vnteamopen.com
- A product from https://vnteamopen.com
Import library to your project
go get -u github.com/vnteamopen/godebouncer
From your code, you can try to create debouncer.
package main
import (
"fmt"
"time"
"github.com/vnteamopen/godebouncer"
)
func main() {
debouncer := godebouncer.New(5 * time.Second).WithTriggered(func() {
fmt.Println("Trigger") // Triggered func will be called after 5 seconds from last SendSignal().
})
fmt.Println("Action 1")
debouncer.SendSignal()
time.Sleep(1 * time.Second)
fmt.Println("Action 2")
debouncer.SendSignal()
// After 5 seconds, the trigger will be called.
// Previous `SendSignal()` will be ignored to trigger the triggered function.
<-debouncer.Done()
}
Allows defining actions before calling SendSignal(). They are synchronous.
debouncer := godebouncer.New(10 * time.Second).WithTriggered(func() {
fmt.Println("Trigger") // Triggered func will be called after 10 seconds from last SendSignal().
})
debouncer.Do(func() {
fmt.Println("Action 1")
})
// Debouncer run the argument function of Do() then SendSignal(). They run sequentially.
// After 10 seconds from finishing Do(), the triggered function will be called.
Allows cancelling the timer from the last function SendSignal(). The scheduled triggered function is cancelled and doesn't invoke.
debouncer := godebouncer.New(10 * time.Second).WithTriggered(func() {
fmt.Println("Trigger") // Triggered func will be called after 10 seconds from last SendSignal().
})
debouncer.SendSignal()
debouncer.Cancel() // No triggered function is called
Allows replacing triggered function.
debouncer := godebouncer.New(10 * time.Second).WithTriggered(func() {
fmt.Println("Trigger 1") // Triggered func will be called after 10 seconds from last SendSignal().
})
debouncer.SendSignal()
debouncer.UpdateTriggeredFunc(func() {
fmt.Println("Trigger 2")
})
// Output: "Trigger 2" after 10 seconds
Allows replacing the waiting time duration. You need to call a SendSignal() again to trigger a new timer with a new waiting time duration.
debouncer := godebouncer.New(10 * time.Second).WithTriggered(func() {
fmt.Println("Trigger") // Triggered func will be called after 10 seconds from last SendSignal().
})
debouncer.UpdateTimeDuration(20 * time.Millisecond)
debouncer.SendSignal()
// Output: "Trigger" after 20 seconds
Allows the caller of godebouncer knows when the triggered function is done invoking to synchronize execution across goroutines.
debouncer := godebouncer.New(1 * time.Second).WithTriggered(func() {
fmt.Println("Fetching...")
time.Sleep(2 * time.Second)
fmt.Println("Done")
})
debouncer.SendSignal()
<-debouncer.Done() // The current goroutine will wait until the triggered func finish its execution.
fmt.Println("After done")
MIT
All your contributions to project and make it better, they are welcome. Feel free to start an issue.
Core contributors:
- Viet Nam We Build group https://webuild.community for discussion.