aboutsummaryrefslogtreecommitdiffstats
path: root/modules/util/timer.go
diff options
context:
space:
mode:
Diffstat (limited to 'modules/util/timer.go')
-rw-r--r--modules/util/timer.go28
1 files changed, 28 insertions, 0 deletions
diff --git a/modules/util/timer.go b/modules/util/timer.go
index daf96bda7e..d598fde73a 100644
--- a/modules/util/timer.go
+++ b/modules/util/timer.go
@@ -4,6 +4,7 @@
package util
import (
+ "sync"
"time"
)
@@ -18,3 +19,30 @@ func StopTimer(t *time.Timer) bool {
}
return stopped
}
+
+func Debounce(d time.Duration) func(f func()) {
+ type debouncer struct {
+ mu sync.Mutex
+ t *time.Timer
+ }
+ db := &debouncer{}
+
+ return func(f func()) {
+ db.mu.Lock()
+ defer db.mu.Unlock()
+
+ if db.t != nil {
+ db.t.Stop()
+ }
+ var trigger *time.Timer
+ trigger = time.AfterFunc(d, func() {
+ db.mu.Lock()
+ defer db.mu.Unlock()
+ if trigger == db.t {
+ f()
+ db.t = nil
+ }
+ })
+ db.t = trigger
+ }
+}