mirror of
https://git.vectorsigma.ru/public/photoview.git
synced 2026-08-03 20:59:03 +00:00
Add replace debounce with throttle
Update dependencies
This commit is contained in:
22
api/utils/Throttle.go
Normal file
22
api/utils/Throttle.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package utils
|
||||
|
||||
import "time"
|
||||
|
||||
type Throttle struct {
|
||||
interval time.Duration
|
||||
lastAction time.Time
|
||||
}
|
||||
|
||||
func NewThrottle(interval time.Duration) Throttle {
|
||||
return Throttle{
|
||||
interval: interval,
|
||||
lastAction: time.Unix(0, 0),
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Throttle) Trigger(action func()) {
|
||||
if time.Now().After(t.lastAction.Add(t.interval)) {
|
||||
t.lastAction = time.Now()
|
||||
action()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user