Skip to content

Commit

Permalink
After and Every now require a time.Duration instead of a string
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed May 3, 2014
1 parent eca3a1c commit e500296
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 14 deletions.
16 changes: 4 additions & 12 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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{}) {
Expand Down Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ 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)
Expect(2).To(Equal(i))
})
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)
Expand Down

0 comments on commit e500296

Please sign in to comment.