aboutsummaryrefslogtreecommitdiffhomepage
path: root/watcher/batcher.go
diff options
context:
space:
mode:
Diffstat (limited to 'watcher/batcher.go')
-rw-r--r--watcher/batcher.go30
1 files changed, 22 insertions, 8 deletions
diff --git a/watcher/batcher.go b/watcher/batcher.go
index 12c51940d..718eea73f 100644
--- a/watcher/batcher.go
+++ b/watcher/batcher.go
@@ -17,11 +17,12 @@ import (
"time"
"github.com/fsnotify/fsnotify"
+ "github.com/gohugoio/hugo/watcher/filenotify"
)
// Batcher batches file watch events in a given interval.
type Batcher struct {
- *fsnotify.Watcher
+ filenotify.FileWatcher
interval time.Duration
done chan struct{}
@@ -29,12 +30,25 @@ type Batcher struct {
}
// New creates and starts a Batcher with the given time interval.
-func New(interval time.Duration) (*Batcher, error) {
- watcher, err := fsnotify.NewWatcher()
+// It will fall back to a poll based watcher if native isn's supported.
+// To always use polling, set poll to true.
+func New(intervalBatcher, intervalPoll time.Duration, poll bool) (*Batcher, error) {
+ var err error
+ var watcher filenotify.FileWatcher
+
+ if poll {
+ watcher = filenotify.NewPollingWatcher(intervalPoll)
+ } else {
+ watcher, err = filenotify.New(intervalPoll)
+ }
+
+ if err != nil {
+ return nil, err
+ }
batcher := &Batcher{}
- batcher.Watcher = watcher
- batcher.interval = interval
+ batcher.FileWatcher = watcher
+ batcher.interval = intervalBatcher
batcher.done = make(chan struct{}, 1)
batcher.Events = make(chan []fsnotify.Event, 1)
@@ -42,7 +56,7 @@ func New(interval time.Duration) (*Batcher, error) {
go batcher.run()
}
- return batcher, err
+ return batcher, nil
}
func (b *Batcher) run() {
@@ -51,7 +65,7 @@ func (b *Batcher) run() {
OuterLoop:
for {
select {
- case ev := <-b.Watcher.Events:
+ case ev := <-b.FileWatcher.Events():
evs = append(evs, ev)
case <-tick:
if len(evs) == 0 {
@@ -69,5 +83,5 @@ OuterLoop:
// Close stops the watching of the files.
func (b *Batcher) Close() {
b.done <- struct{}{}
- b.Watcher.Close()
+ b.FileWatcher.Close()
}