2022-11-07 08:37:47 +00:00
|
|
|
package utils
|
|
|
|
|
2023-01-08 13:31:35 +00:00
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
2022-11-07 08:37:47 +00:00
|
|
|
|
2023-09-22 08:54:16 +00:00
|
|
|
var CNLoc = time.FixedZone("UTC", 8*60*60)
|
|
|
|
|
2022-11-07 08:37:47 +00:00
|
|
|
func MustParseCNTime(str string) time.Time {
|
2023-09-22 08:54:16 +00:00
|
|
|
lastOpTime, _ := time.ParseInLocation("2006-01-02 15:04:05 -07", str+" +08", CNLoc)
|
2022-11-07 08:37:47 +00:00
|
|
|
return lastOpTime
|
|
|
|
}
|
2023-01-08 13:31:35 +00:00
|
|
|
|
|
|
|
func NewDebounce(interval time.Duration) func(f func()) {
|
|
|
|
var timer *time.Timer
|
|
|
|
var lock sync.Mutex
|
|
|
|
return func(f func()) {
|
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
|
|
|
if timer != nil {
|
|
|
|
timer.Stop()
|
|
|
|
}
|
|
|
|
timer = time.AfterFunc(interval, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDebounce2(interval time.Duration, f func()) func() {
|
|
|
|
var timer *time.Timer
|
|
|
|
var lock sync.Mutex
|
|
|
|
return func() {
|
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
|
|
|
if timer == nil {
|
|
|
|
timer = time.AfterFunc(interval, f)
|
|
|
|
}
|
|
|
|
(*time.Timer)(timer).Reset(interval)
|
|
|
|
}
|
|
|
|
}
|
2024-04-02 08:51:02 +00:00
|
|
|
|
|
|
|
func NewThrottle(interval time.Duration) func(func()) {
|
|
|
|
var lastCall time.Time
|
|
|
|
|
|
|
|
return func(fn func()) {
|
|
|
|
now := time.Now()
|
|
|
|
if now.Sub(lastCall) < interval {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
time.AfterFunc(interval, fn)
|
|
|
|
lastCall = now
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewThrottle2(interval time.Duration, fn func()) func() {
|
|
|
|
var lastCall time.Time
|
|
|
|
return func() {
|
|
|
|
now := time.Now()
|
|
|
|
if now.Sub(lastCall) < interval {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
time.AfterFunc(interval, fn)
|
|
|
|
lastCall = now
|
|
|
|
}
|
|
|
|
}
|