Last active
February 14, 2021 01:38
-
-
Save nwillc/6d5e7a359750397b43d26734e8562888 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package util | |
import ( | |
"time" | |
) | |
type StopChannel chan bool | |
// RepeatUntilStopped creates a goroutine that loops, waiting the delay provided and | |
// then calling the function provided. The loop can be stopped by sending a value to | |
// returned StopChannel. | |
func RepeatUntilStopped(delay time.Duration, f func()) StopChannel { | |
control := make(chan bool, 1) | |
go func() { | |
for { | |
select { | |
case <-control: | |
return | |
case <-time.After(delay): | |
} | |
f() | |
} | |
}() | |
return control | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment