Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/gohugoio/hugo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEgon Elbre <egonelbre@gmail.com>2013-12-15 19:19:22 +0400
committerspf13 <steve.francia@gmail.com>2013-12-28 22:46:52 +0400
commit8d80f9b39e42dd5b61af052979a7c04dc8edf851 (patch)
tree89c1474782e3a3bca03a76c88081d21c5e6f3ce6 /watcher
parent1979f7d9c7d047340c9205f0d3e6d8393d498f9c (diff)
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()
+}