fixed potential race condition
This commit is contained in:
@@ -6,12 +6,20 @@ import "time"
|
|||||||
//
|
//
|
||||||
// Debounce emits the most recently emitted value from the source
|
// Debounce emits the most recently emitted value from the source
|
||||||
// withing the timespan set by the span time.Duration
|
// withing the timespan set by the span time.Duration
|
||||||
func Sample[T any](span time.Duration, source chan T, done chan struct{}, fn func(e T)) {
|
func Sample(span time.Duration, source chan []byte, done chan struct{}, fn func(e []byte)) {
|
||||||
ticker := time.NewTicker(span)
|
var (
|
||||||
|
item []byte
|
||||||
|
ticker = time.NewTicker(span)
|
||||||
|
)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
fn(<-source)
|
if item != nil {
|
||||||
|
fn(item)
|
||||||
|
}
|
||||||
|
case <-source:
|
||||||
|
item = <-source
|
||||||
case <-done:
|
case <-done:
|
||||||
ticker.Stop()
|
ticker.Stop()
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user