mirror of
https://github.com/niusmallnan/steve.git
synced 2025-06-21 20:27:05 +00:00
41 lines
600 B
Go
41 lines
600 B
Go
package counts
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/rancher/apiserver/pkg/types"
|
|
)
|
|
|
|
func buffer(c chan types.APIEvent) chan types.APIEvent {
|
|
result := make(chan types.APIEvent)
|
|
go func() {
|
|
defer close(result)
|
|
debounce(result, c)
|
|
}()
|
|
return result
|
|
}
|
|
|
|
func debounce(result, input chan types.APIEvent) {
|
|
t := time.NewTicker(time.Second)
|
|
defer t.Stop()
|
|
|
|
var (
|
|
lastEvent *types.APIEvent
|
|
)
|
|
for {
|
|
select {
|
|
case event, ok := <-input:
|
|
if ok {
|
|
lastEvent = &event
|
|
} else {
|
|
return
|
|
}
|
|
case <-t.C:
|
|
if lastEvent != nil {
|
|
result <- *lastEvent
|
|
lastEvent = nil
|
|
}
|
|
}
|
|
}
|
|
}
|