mirror of
https://github.com/gohugoio/hugo.git
synced 2024-12-29 16:51:46 +00:00
d376314763
This contains a few fixes (v0.9.1 through v0.9.3) but the same API and few internal changes. https://github.com/go-fsnotify/fsnotify/blob/master/CHANGELOG.md#v093--2014-12-31 A good first step before switching to v1.
57 lines
984 B
Go
57 lines
984 B
Go
package watcher
|
|
|
|
import (
|
|
"time"
|
|
|
|
"gopkg.in/fsnotify.v0"
|
|
)
|
|
|
|
type Batcher struct {
|
|
*fsnotify.Watcher
|
|
interval time.Duration
|
|
done chan struct{}
|
|
|
|
Event chan []*fsnotify.FileEvent // Events are returned on this channel
|
|
}
|
|
|
|
func New(interval time.Duration) (*Batcher, error) {
|
|
watcher, err := fsnotify.NewWatcher()
|
|
|
|
batcher := &Batcher{}
|
|
batcher.Watcher = watcher
|
|
batcher.interval = interval
|
|
batcher.done = make(chan struct{}, 1)
|
|
batcher.Event = make(chan []*fsnotify.FileEvent, 1)
|
|
|
|
if err == nil {
|
|
go batcher.run()
|
|
}
|
|
|
|
return batcher, err
|
|
}
|
|
|
|
func (b *Batcher) run() {
|
|
tick := time.Tick(b.interval)
|
|
evs := make([]*fsnotify.FileEvent, 0)
|
|
OuterLoop:
|
|
for {
|
|
select {
|
|
case ev := <-b.Watcher.Event:
|
|
evs = append(evs, ev)
|
|
case <-tick:
|
|
if len(evs) == 0 {
|
|
continue
|
|
}
|
|
b.Event <- evs
|
|
evs = make([]*fsnotify.FileEvent, 0)
|
|
case <-b.done:
|
|
break OuterLoop
|
|
}
|
|
}
|
|
close(b.done)
|
|
}
|
|
|
|
func (b *Batcher) Close() {
|
|
b.done <- struct{}{}
|
|
b.Watcher.Close()
|
|
}
|