pulse more boilerplate refactoring

This commit is contained in:
Bel LaPointe
2025-11-19 12:31:17 -07:00
parent 5b11ae1648
commit 39123044b2
4 changed files with 85 additions and 191 deletions

34
src/with/every.go Normal file
View File

@@ -0,0 +1,34 @@
package with
import (
"context"
"time"
)
func GoEvery(ctx context.Context, d time.Duration, foo func()) {
every(ctx, d, foo, true)
}
func Every(ctx context.Context, d time.Duration, foo func()) {
every(ctx, d, foo, false)
}
func every(ctx context.Context, d time.Duration, foo func(), async bool) {
ticker := time.NewTicker(d)
defer ticker.Stop()
for ctx.Err() == nil {
everyTry(ctx, foo, async)
select {
case <-ctx.Done():
case <-ticker.C:
}
}
}
func everyTry(ctx context.Context, foo func(), async bool) {
if async {
go foo()
} else {
foo()
}
}