aboutsummaryrefslogtreecommitdiffhomepage
path: root/watcher
diff options
context:
space:
mode:
authorEgon Elbre <[email protected]>2013-12-15 17:19:22 +0200
committerspf13 <[email protected]>2013-12-28 13:46:52 -0500
commit8d80f9b39e42dd5b61af052979a7c04dc8edf851 (patch)
tree89c1474782e3a3bca03a76c88081d21c5e6f3ce6 /watcher
parent1979f7d9c7d047340c9205f0d3e6d8393d498f9c (diff)
downloadhugo-8d80f9b39e42dd5b61af052979a7c04dc8edf851.tar.gz
hugo-8d80f9b39e42dd5b61af052979a7c04dc8edf851.zip
Added batching behavior for page building.
Quite often file watcher gets many changes and each change triggered a build. One build per second should be sufficient. Also added tracking for new folders.
Diffstat (limited to 'watcher')
-rw-r--r--watcher/batcher.go56
1 files changed, 56 insertions, 0 deletions
diff --git a/watcher/batcher.go b/watcher/batcher.go
new file mode 100644
index 000000000..a22ad8b41
--- /dev/null
+++ b/watcher/batcher.go
@@ -0,0 +1,56 @@
+package watcher
+
+import (
+ "github.com/howeyc/fsnotify"
+ "time"
+)
+
+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()
+}