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. // Every triggers f every `t` time until the end of days.
func Every(t string, f func()) { func Every(t time.Duration, f func()) {
c := time.Tick(parseDuration(t)) c := time.Tick(t)
// start a go routine to not bloc the function // start a go routine to not bloc the function
go func() { go func() {
for { for {
@ -23,8 +23,8 @@ func Every(t string, f func()) {
} }
// After triggers the passed function after `t` duration. // After triggers the passed function after `t` duration.
func After(t string, f func()) { func After(t time.Duration, f func()) {
time.AfterFunc(parseDuration(t), f) time.AfterFunc(t, f)
} }
func Publish(c chan interface{}, val interface{}) { func Publish(c chan interface{}, val interface{}) {
@ -76,11 +76,3 @@ func ToScale(input, min, max float64) float64 {
return i 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() { Context("when valid", func() {
It("should execute function at every interval", func() { It("should execute function at every interval", func() {
var i = 0 var i = 0
Every("2ms", func() { Every(2*time.Millisecond, func() {
i++ i++
}) })
time.Sleep(5 * time.Millisecond) time.Sleep(5 * time.Millisecond)
@ -23,7 +23,7 @@ var _ = Describe("Utils", func() {
}) })
It("should execute function after specific interval", func() { It("should execute function after specific interval", func() {
var i = 0 var i = 0
After("1ms", func() { After(1*time.Millisecond, func() {
i = i + 1 i = i + 1
}) })
time.Sleep(2 * time.Millisecond) time.Sleep(2 * time.Millisecond)