1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-12-12 12:57:21 +01:00
gojekyll/server/watcher.go

95 lines
2.0 KiB
Go
Raw Normal View History

2017-06-22 23:37:46 +02:00
package server
import (
"log"
"time"
"github.com/fsnotify/fsnotify"
2017-07-09 22:17:20 +02:00
"github.com/osteele/gojekyll/utils"
2017-06-22 23:37:46 +02:00
)
2017-07-09 15:37:23 +02:00
func (s *Server) watchAndReload() error {
site := s.Site
return s.watchFiles(func(filenames []string) {
2017-07-14 20:52:31 +02:00
// This resolves filenames to URLS *before* reloading the site, in case the latter
2017-07-09 15:37:23 +02:00
// remaps permalinks.
urls := map[string]bool{}
for _, relpath := range filenames {
url, ok := site.FilenameURLPath(relpath)
if ok {
urls[url] = true
}
}
2017-07-14 20:52:31 +02:00
s.reloadSite(len(filenames))
2017-07-09 15:37:23 +02:00
for url := range urls {
s.lr.Reload(url)
}
})
}
// calls fn repeatedly in a goroutine
func (s *Server) watchFiles(fn func([]string)) error {
2017-06-22 23:37:46 +02:00
var (
site = s.Site
2017-07-09 15:37:23 +02:00
sourceDir = site.SourceDir()
2017-06-22 23:37:46 +02:00
events = make(chan string)
debounced = debounce(time.Second, events)
)
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
go func() {
for {
select {
case event := <-watcher.Events:
events <- event.Name
case err := <-watcher.Errors:
log.Println("error:", err)
}
}
}()
go func() {
for {
filenames := <-debounced
2017-07-09 15:37:23 +02:00
relpaths := make([]string, 0, len(filenames))
2017-07-14 20:52:31 +02:00
seen := map[string]bool{}
for _, filename := range filenames {
2017-07-09 22:17:20 +02:00
relpath := utils.MustRel(sourceDir, filename)
2017-07-09 15:37:23 +02:00
if relpath == "_config.yml" || !site.Exclude(relpath) {
2017-07-14 20:52:31 +02:00
if !seen[relpath] {
seen[relpath] = true
relpaths = append(relpaths, relpath)
}
2017-06-22 23:37:46 +02:00
}
}
2017-07-09 15:37:23 +02:00
fn(relpaths)
2017-06-22 23:37:46 +02:00
}
}()
2017-07-09 15:37:23 +02:00
return watcher.Add(sourceDir)
2017-06-22 23:37:46 +02:00
}
// debounce relays values from input to output, merging successive values within interval
// TODO consider https://github.com/ReactiveX/RxGo
2017-06-29 20:21:27 +02:00
func debounce(interval time.Duration, input <-chan string) <-chan []string {
output := make(chan []string)
2017-06-22 23:37:46 +02:00
var (
pending = []string{}
2017-06-29 04:46:19 +02:00
ticker = time.Tick(interval) // nolint: staticcheck, megacheck
2017-06-22 23:37:46 +02:00
)
go func() {
for {
select {
case value := <-input:
pending = append(pending, value)
case <-ticker:
if len(pending) > 0 {
output <- pending
pending = []string{}
}
}
}
}()
2017-06-29 20:21:27 +02:00
return output
2017-06-22 23:37:46 +02:00
}