我有一个通道可以接收突发写入。我想等到通道上的突发发送完成后再触发操作。
我已经看过这个gist,但是,interval如果缓冲区中有数据,它就会发送输出:
func debounceChannel(interval time.Duration, output chan int) chan int {
input := make(chan int)
go func() {
var buffer int
var ok bool
// We do not start waiting for interval until called at least once
buffer, ok = <-input
// If channel closed exit, we could also close output
if !ok {
return
}
// We start waiting for an interval
for {
select {
case buffer, ok = <-input:
// If channel closed exit, we could also close output
if !ok {
return
}
case <-time.After(interval):
// Interval has passed and we have data, so send it
output <- buffer
// Wait for data again before starting waiting for an interval
buffer, ok = <-input
if !ok {
return
}
// If channel is not closed we have more data and start waiting for interval
}
}
}()
return input
}
在我的情况下,我想等到在触发或发送输出之前不再在输入通道上为此突发发送任何数据。
我如何实现这一目标?
炎炎设计
慕工程0101907
相关分类