Documentation ¶
Overview ¶
Package debouncer provides feature to make 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.*/
Example ¶
package main import ( "fmt" "time" "github.com/DanielRenne/GoCore/core/debouncer" ) func main() { wait := 5 * time.Second d := debouncer.New(wait).WithTriggered(func() { fmt.Println("Trigger") // Triggered func will be called after 5 seconds from last SendSignal(). }) fmt.Println("Action 1") d.SendSignal() time.Sleep(1 * time.Second) fmt.Println("Action 2") d.SendSignal() // After 5 seconds, the trigger will be called. //Previous `SendSignal()` will be ignore to trigger the triggered function. <-d.Done() }
Output:
Index ¶
- Constants
- type Debouncer
- func (d *Debouncer) Cancel()
- func (d *Debouncer) Do(signalFunc func())
- func (d *Debouncer) DoAny(signalFunc func(any), anyVar any)
- func (d *Debouncer) Done() <-chan struct{}
- func (d *Debouncer) SendSignal() (err error)
- func (d *Debouncer) SendSignalWithData(anyVar any) (err error)
- func (d *Debouncer) UpdateAnyFunc(newTriggeredFunc func(any))
- func (d *Debouncer) UpdateTimeDuration(newTimeDuration time.Duration)
- func (d *Debouncer) UpdateTriggeredFunc(newTriggeredFunc func())
- func (d *Debouncer) WithAny(triggeredFunc func(any)) *Debouncer
- func (d *Debouncer) WithTriggered(triggeredFunc func()) *Debouncer
Examples ¶
Constants ¶
const ( // ErrorTypeIncorrectSendSignal if you call SendSignal on something you configured WithAny ErrorTypeIncorrectSendSignal = "you are using sendsignal with something setup withany" // ErrorTypeIncorrectSendSignalWithAny if you call SendSignalWithAny on something you configured WithTriggered ErrorTypeIncorrectSendSignalWithAny = "you are using sendsignalwithany with something setup withtriggered" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Debouncer ¶
type Debouncer struct {
// contains filtered or unexported fields
}
Debouncer main struct for debouncer package
func New ¶
New creates a new instance of debouncer. Each instance of debouncer works independent, concurrency with different wait duration.
func (*Debouncer) Cancel ¶
func (d *Debouncer) Cancel()
Cancel the timer from the last function SendSignal(). The scheduled triggered function is cancelled and doesn't invoke.
func (*Debouncer) Do ¶
func (d *Debouncer) Do(signalFunc func())
Do run the signalFunc() and call SendSignal() after all. The signalFunc() and SendSignal() function run sequentially.
func (*Debouncer) DoAny ¶
DoAny run the signalFunc(any) and call SendSignalWithData(any) after all. The signalFunc() and SendSignal() function run sequentially.
func (*Debouncer) Done ¶
func (d *Debouncer) Done() <-chan struct{}
Done returns a receive-only channel to notify the caller when the triggered func has been executed.
func (*Debouncer) SendSignal ¶
SendSignal makes an action that notifies to invoke the triggered function after a wait duration.
func (*Debouncer) SendSignalWithData ¶
SendSignalWithData makes an action that notifies to invoke the triggered function after a wait duration.
func (*Debouncer) UpdateAnyFunc ¶
UpdateAnyFunc replaces triggered function.
func (*Debouncer) UpdateTimeDuration ¶
UpdateTimeDuration replaces the waiting time duration. You need to call a SendSignal() again to trigger a new timer with a new waiting time duration.
func (*Debouncer) UpdateTriggeredFunc ¶
func (d *Debouncer) UpdateTriggeredFunc(newTriggeredFunc func())
UpdateTriggeredFunc replaces triggered function.
func (*Debouncer) WithAny ¶
WithAny attached a triggered function to debouncer instance and return the same instance of debouncer to use.
func (*Debouncer) WithTriggered ¶
WithTriggered attached a triggered function to debouncer instance and return the same instance of debouncer to use.