debounce tracklist row rendering when fast scrolling
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type EventCounter struct {
|
||||
buf []time.Time
|
||||
ptr int
|
||||
}
|
||||
|
||||
func NewEventCounter(maxN int) *EventCounter {
|
||||
buffer := make([]time.Time, maxN)
|
||||
return &EventCounter{
|
||||
buf: buffer,
|
||||
}
|
||||
}
|
||||
|
||||
func (e *EventCounter) Add() {
|
||||
e.buf[e.ptr] = time.Now()
|
||||
e.ptr = (e.ptr + 1) % len(e.buf)
|
||||
}
|
||||
|
||||
func (e *EventCounter) NumEventsSince(t time.Time) int {
|
||||
i := e.ptr - 1
|
||||
|
||||
count := 0
|
||||
for {
|
||||
if i < 0 {
|
||||
i = len(e.buf) - 1
|
||||
}
|
||||
if !e.buf[i].IsZero() && e.buf[i].After(t) {
|
||||
count++
|
||||
} else {
|
||||
break
|
||||
}
|
||||
if count == len(e.buf) {
|
||||
break
|
||||
}
|
||||
i--
|
||||
}
|
||||
return count
|
||||
}
|
||||
Reference in New Issue
Block a user