2017-10-20 10:51:56 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/fsnotify/fsnotify"
|
|
|
|
)
|
|
|
|
|
2017-11-23 01:51:23 +00:00
|
|
|
// 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 {
|
2018-11-01 16:00:58 +00:00
|
|
|
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
|
2018-06-14 00:30:18 +00:00
|
|
|
EventsChan() chan fsnotify.Event
|
2017-11-20 15:10:11 +00:00
|
|
|
}
|
|
|
|
|
2018-06-08 15:38:57 +00:00
|
|
|
// Main thread for watching file changes
|
2017-11-20 15:05:44 +00:00
|
|
|
func WatcherThread(w IWatchable) {
|
2017-11-16 13:27:50 +00:00
|
|
|
|
2017-11-20 15:05:44 +00:00
|
|
|
bookmarkPath := w.GetPath()
|
2017-11-19 16:00:37 +00:00
|
|
|
log.Infof("watching %s", bookmarkPath)
|
2017-11-16 13:27:50 +00:00
|
|
|
|
2017-11-09 20:27:03 +00:00
|
|
|
for {
|
2018-10-26 16:25:07 +00:00
|
|
|
// Keep watcher here as it is reset from within
|
|
|
|
// the select block
|
2018-11-01 16:00:58 +00:00
|
|
|
watcher := w.GetFileWatcher()
|
2018-10-26 16:25:07 +00:00
|
|
|
|
2017-11-09 20:27:03 +00:00
|
|
|
select {
|
2017-11-20 15:05:44 +00:00
|
|
|
case event := <-watcher.Events:
|
2018-06-08 15:38:57 +00:00
|
|
|
|
|
|
|
// On Chrome like browsers the bookmarks file is created
|
|
|
|
// at every change.
|
2018-10-28 14:18:45 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2017-11-09 20:27:03 +00:00
|
|
|
if event.Op&fsnotify.Create == fsnotify.Create &&
|
2017-11-16 13:27:50 +00:00
|
|
|
event.Name == bookmarkPath {
|
2017-10-20 10:51:56 +00:00
|
|
|
|
2017-11-20 18:07:15 +00:00
|
|
|
w.Run()
|
2018-10-26 16:25:07 +00:00
|
|
|
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
|
|
|
}
|
2018-06-08 15:38:57 +00:00
|
|
|
|
|
|
|
// Firefox keeps the file open and makes changes on it
|
2018-06-14 00:30:18 +00:00
|
|
|
// It needs a debouncer
|
|
|
|
if event.Name == bookmarkPath {
|
2018-10-26 16:25:07 +00:00
|
|
|
log.Debugf("event: %v | eventName: %v", event.Op, event.Name)
|
2018-06-14 00:30:18 +00:00
|
|
|
//go debounce(1000*time.Millisecond, spammyEventsChannel, w)
|
|
|
|
ch := w.EventsChan()
|
|
|
|
ch <- event
|
2018-06-08 16:27:33 +00:00
|
|
|
//w.Run()
|
2018-06-08 15:38:57 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|