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

sync master /w main #2

Merged
merged 2 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions debounce_time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright © 2024 Jon Friesen <[email protected]>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

package debounce

import (
"sync"
"time"
)

var now = time.Now

// NewDebounceByDuration returns a debounced function that takes another function as its argument.
// This function will be called at the given interval, but no more than the max duration
// from the first call.
func NewDebounceByDuration(interval, maxDuration time.Duration) func(f func()) {
d := &durationDebouncer{
interval: interval,
maxDuration: maxDuration,
}

return func(f func()) {
d.add(f)
}
}

type durationDebouncer struct {
mu sync.Mutex
interval time.Duration
maxDuration time.Duration
timer *time.Timer
firstCall bool
startTime time.Time
}

func (d *durationDebouncer) add(f func()) {
d.mu.Lock()
defer d.mu.Unlock()

now := now()
if !d.firstCall {
d.firstCall = true
d.startTime = now
}

if d.timer != nil {
d.timer.Stop()
}

remainingDuration := d.maxDuration - time.Since(d.startTime)
if remainingDuration <= 0 {
d.reset()
f()
return
}

d.timer = time.AfterFunc(d.interval, func() {
d.mu.Lock()
defer d.mu.Unlock()

f()
d.reset()
})
}

func (d *durationDebouncer) reset() {
d.firstCall = false
if d.timer != nil {
d.timer.Stop()
d.timer = nil
}
d.startTime = time.Time{}
}
92 changes: 92 additions & 0 deletions debounce_time_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package debounce

import (
"sync"
"testing"
"time"
)

var (
mockNowFunc func() time.Time
mockNowMutex sync.Mutex
)

func mockNow() time.Time {
mockNowMutex.Lock()
defer mockNowMutex.Unlock()
return mockNowFunc()
}

func setMockNow(t time.Time) {
mockNowMutex.Lock()
defer mockNowMutex.Unlock()
mockNowFunc = func() time.Time {
return t
}
}

func init() {
now = mockNow
}

func TestTimeDebounce(t *testing.T) {
tests := []struct {
name string
interval time.Duration
maxDuration time.Duration
timeSteps []time.Duration
}{
{
name: "Single call within interval",
interval: 100 * time.Millisecond,
maxDuration: 500 * time.Millisecond,
timeSteps: []time.Duration{100 * time.Millisecond, 50 * time.Millisecond},
},
{
name: "Multiple calls within interval",
interval: 100 * time.Millisecond,
maxDuration: 500 * time.Millisecond,
timeSteps: []time.Duration{50 * time.Millisecond, 50 * time.Millisecond, 200 * time.Millisecond},
},
{
name: "Single call at max duration",
interval: 100 * time.Millisecond,
maxDuration: 200 * time.Millisecond,
timeSteps: []time.Duration{100 * time.Millisecond, 100 * time.Millisecond},
},
{
name: "Multiple calls at max duration",
interval: 150 * time.Millisecond,
maxDuration: 300 * time.Millisecond,
timeSteps: []time.Duration{50 * time.Millisecond, 50 * time.Millisecond, 200 * time.Millisecond},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
callCount := 0
mu := sync.Mutex{}
f := func() {
mu.Lock()
defer mu.Unlock()
callCount++
}

d := NewDebounceByDuration(tt.interval, tt.maxDuration)
start := time.Now()
setMockNow(start)

for _, step := range tt.timeSteps {
setMockNow(start.Add(step))
d(f)
time.Sleep(step)
}

mu.Lock()
if callCount != 1 {
t.Errorf("expected 1 calls, got %d", callCount)
}
mu.Unlock()
})
}
}
Loading