Skip to content

Commit

Permalink
removed sleeps from every and after funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
mattetti committed Apr 26, 2014
1 parent eb887f7 commit a6def38
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,24 @@ import (
"time"
)

// Every triggers f every `t` time until the end of days.
func Every(t string, f func()) {
dur := parseDuration(t)
c := time.Tick(parseDuration(t))
// start a go routine to not bloc the function
go func() {
for {
time.Sleep(dur)
// wait for the ticker to tell us to run
<-c
// run the passed function in another go routine
// so we don't slow down the loop.
go f()
}
}()
}

// After triggers the passed function after `t` duration.
func After(t string, f func()) {
dur := parseDuration(t)
go func() {
time.Sleep(dur)
f()
}()
time.AfterFunc(parseDuration(t), f)
}

func Publish(c chan interface{}, val interface{}) {
Expand Down

0 comments on commit a6def38

Please sign in to comment.