After and Every now require a time.Duration instead of a string

This commit is contained in:
Adrian Zankich 2014-05-03 03:20:02 -07:00
parent eca3a1ca99
commit e500296b96
2 changed files with 6 additions and 14 deletions

View File

@ -8,8 +8,8 @@ import (
)
// Every triggers f every `t` time until the end of days.
func Every(t string, f func()) {
c := time.Tick(parseDuration(t))
func Every(t time.Duration, f func()) {
c := time.Tick(t)
// start a go routine to not bloc the function
go func() {
for {
@ -23,8 +23,8 @@ func Every(t string, f func()) {
}
// After triggers the passed function after `t` duration.
func After(t string, f func()) {
time.AfterFunc(parseDuration(t), f)
func After(t time.Duration, f func()) {
time.AfterFunc(t, f)
}
func Publish(c chan interface{}, val interface{}) {
@ -76,11 +76,3 @@ func ToScale(input, min, max float64) float64 {
return i
}
}
func parseDuration(t string) time.Duration {
dur, err := time.ParseDuration(t)
if err != nil {
panic(err)
}
return dur
}

View File

@ -15,7 +15,7 @@ var _ = Describe("Utils", func() {
Context("when valid", func() {
It("should execute function at every interval", func() {
var i = 0
Every("2ms", func() {
Every(2*time.Millisecond, func() {
i++
})
time.Sleep(5 * time.Millisecond)
@ -23,7 +23,7 @@ var _ = Describe("Utils", func() {
})
It("should execute function after specific interval", func() {
var i = 0
After("1ms", func() {
After(1*time.Millisecond, func() {
i = i + 1
})
time.Sleep(2 * time.Millisecond)