2015-08-04 17:35:09 -05:00
|
|
|
package markdown
|
|
|
|
|
|
|
|
import "time"
|
|
|
|
|
2015-08-04 19:00:53 -05:00
|
|
|
const DefaultInterval = time.Second * 60
|
2015-08-04 17:35:09 -05:00
|
|
|
|
|
|
|
// Watch monitors the configured markdown directory for changes. It calls GenerateLinks
|
|
|
|
// when there are changes.
|
|
|
|
func Watch(md Markdown, c *Config, interval time.Duration) (stopChan chan struct{}) {
|
|
|
|
return TickerFunc(interval, func() {
|
|
|
|
GenerateLinks(md, c)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// TickerFunc runs f at interval. If interval is <= 0, it loops f. A message to the
|
|
|
|
// returned channel will stop the executing goroutine.
|
|
|
|
func TickerFunc(interval time.Duration, f func()) chan struct{} {
|
|
|
|
stopChan := make(chan struct{})
|
|
|
|
|
2015-08-04 18:41:04 -05:00
|
|
|
ticker := time.NewTicker(interval)
|
|
|
|
go func() {
|
|
|
|
loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
f()
|
|
|
|
case <-stopChan:
|
|
|
|
ticker.Stop()
|
|
|
|
break loop
|
2015-08-04 17:35:09 -05:00
|
|
|
}
|
2015-08-04 18:41:04 -05:00
|
|
|
}
|
|
|
|
}()
|
2015-08-04 17:35:09 -05:00
|
|
|
|
|
|
|
return stopChan
|
|
|
|
}
|