From 5d09d99ff5563fa88d6fbccec09abc6471ae83fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torjus=20H=C3=A5kestad?= Date: Thu, 10 Oct 2024 23:13:00 +0200 Subject: [PATCH] Let debouncer delay be configurable --- debouncer/debouncer.go | 10 +++++----- debouncer/debouncer_test.go | 8 ++++---- main.go | 5 +++-- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/debouncer/debouncer.go b/debouncer/debouncer.go index 9d6a1b6..c7ac44e 100644 --- a/debouncer/debouncer.go +++ b/debouncer/debouncer.go @@ -7,7 +7,7 @@ type Debouncer struct { C <-chan bool } -func New(ch <-chan bool) *Debouncer { +func New(ch <-chan bool, window time.Duration) *Debouncer { output := make(chan bool) var currentState bool var lastState bool @@ -21,11 +21,11 @@ func New(ch <-chan bool) *Debouncer { output <- false lastState = false } else { - falseTimer.Reset(200 * time.Millisecond) + falseTimer.Reset(window) } case v, ok := <-ch: if !ok { - falseTimer.Reset(200 * time.Millisecond) + falseTimer.Reset(window) <-falseTimer.C output <- false close(output) @@ -35,12 +35,12 @@ func New(ch <-chan bool) *Debouncer { if !lastState { output <- true lastState = true - falseTimer.Reset(200 * time.Millisecond) + falseTimer.Reset(window) } currentState = true } else { - falseTimer.Reset(200 * time.Millisecond) + falseTimer.Reset(window) currentState = false } diff --git a/debouncer/debouncer_test.go b/debouncer/debouncer_test.go index 550ece8..65b4411 100644 --- a/debouncer/debouncer_test.go +++ b/debouncer/debouncer_test.go @@ -10,7 +10,7 @@ import ( func TestDebouncer(t *testing.T) { t.Run("Simple", func(t *testing.T) { ch := make(chan bool) - d := debouncer.New(ch) + d := debouncer.New(ch, 200*time.Millisecond) go func() { ch <- true @@ -23,7 +23,7 @@ func TestDebouncer(t *testing.T) { }) t.Run("TrueTrueTrueFalse", func(t *testing.T) { ch := make(chan bool) - d := debouncer.New(ch) + d := debouncer.New(ch, 200*time.Millisecond) go func() { ch <- true ch <- true @@ -40,7 +40,7 @@ func TestDebouncer(t *testing.T) { }) t.Run("Debounce", func(t *testing.T) { ch := make(chan bool) - d := debouncer.New(ch) + d := debouncer.New(ch, 200*time.Millisecond) go func() { ch <- true time.Sleep(10 * time.Millisecond) @@ -65,7 +65,7 @@ func TestDebouncer(t *testing.T) { }) t.Run("DebounceDelay", func(t *testing.T) { ch := make(chan bool) - d := debouncer.New(ch) + d := debouncer.New(ch, 200*time.Millisecond) go func() { ch <- true ch <- false diff --git a/main.go b/main.go index aa69801..1ce1788 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "net/http" "os" "os/signal" + "time" "git.t-juice.club/torjus/ghettoptt/bus" "git.t-juice.club/torjus/ghettoptt/debouncer" @@ -15,7 +16,7 @@ import ( "github.com/prometheus/client_golang/prometheus/promhttp" ) -const Version = "v0.1.3" +const Version = "v0.1.4" func main() { // Setup logger @@ -73,7 +74,7 @@ func main() { doneCtx, doneCancel := context.WithCancel(srvCtx) defer doneCancel() - debouncer := debouncer.New(eventCh) + debouncer := debouncer.New(eventCh, 400*time.Millisecond) go func() { var done bool for !done {