mirror of
https://github.com/scroll-tech/scroll.git
synced 2026-04-23 03:00:50 -04:00
allow loop with 0 period
This commit is contained in:
@@ -34,14 +34,25 @@ func LoopWithContext(ctx context.Context, period time.Duration, f func(ctx conte
|
||||
|
||||
// Loop Run the f func periodically.
|
||||
func Loop(ctx context.Context, period time.Duration, f func()) {
|
||||
tick := time.NewTicker(period)
|
||||
defer tick.Stop()
|
||||
for ; ; <-tick.C {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
default:
|
||||
f()
|
||||
if period == 0*time.Second {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
default:
|
||||
f()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
tick := time.NewTicker(period)
|
||||
defer tick.Stop()
|
||||
for ; ; <-tick.C {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
default:
|
||||
f()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user