gosuki/watcher.go

71 lines
2.0 KiB
Go
Raw Normal View History

2017-10-20 10:51:56 +00:00
package main
import (
"github.com/fsnotify/fsnotify"
)
// Used as input to WatcherThread
// It does not have to be a browser as long is the interface is implemented
2017-11-20 15:10:11 +00:00
type IWatchable interface {
SetupFileWatcher() // Starts watching bookmarks and runs Load on change
Watch() bool // starts watching linked watcher
Run() // Callback fired on event
GetFileWatcher() *fsnotify.Watcher // returns linked watcher
ResetWatcher() // resets a new watcher
GetPath() string // returns watched path
GetDir() string // returns watched dir
EventsChan() chan fsnotify.Event
2017-11-20 15:10:11 +00:00
}
// Main thread for watching file changes
2017-11-20 15:05:44 +00:00
func WatcherThread(w IWatchable) {
2017-11-20 15:05:44 +00:00
bookmarkPath := w.GetPath()
2017-11-19 16:00:37 +00:00
log.Infof("watching %s", bookmarkPath)
for {
// Keep watcher here as it is reset from within
// the select block
watcher := w.GetFileWatcher()
select {
2017-11-20 15:05:44 +00:00
case event := <-watcher.Events:
// On Chrome like browsers the bookmarks file is created
// at every change.
/*
* When a file inside a watched directory is renamed/created,
* fsnotify does not seem to resume watching the newly created file, we
* need to destroy and create a new watcher. The ResetWatcher() and
* `break` statement ensure we get out of the `select` block and catch
* the newly created watcher to catch events even after rename/create
*/
if event.Op&fsnotify.Create == fsnotify.Create &&
event.Name == bookmarkPath {
2017-10-20 10:51:56 +00:00
2017-11-20 18:07:15 +00:00
w.Run()
log.Debugf("event: %v | eventName: %v", event.Op, event.Name)
log.Debugf("resetting watchers")
w.ResetWatcher()
break
2017-10-20 10:51:56 +00:00
}
// Firefox keeps the file open and makes changes on it
// It needs a debouncer
if event.Name == bookmarkPath {
log.Debugf("event: %v | eventName: %v", event.Op, event.Name)
//go debounce(1000*time.Millisecond, spammyEventsChannel, w)
ch := w.EventsChan()
ch <- event
2018-06-08 16:27:33 +00:00
//w.Run()
}
2017-11-20 15:05:44 +00:00
case err := <-watcher.Errors:
2018-10-28 19:19:12 +00:00
log.Error(err)
2017-10-20 10:51:56 +00:00
}
}
}