diff --git a/utils.go b/utils.go index e41f1405..c07581c9 100644 --- a/utils.go +++ b/utils.go @@ -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 -} diff --git a/utils_test.go b/utils_test.go index 743dde4c..95d9b5fb 100644 --- a/utils_test.go +++ b/utils_test.go @@ -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)