diff options
author | Tim Culverhouse <tim@timculverhouse.com> | 2023-03-02 16:46:04 -0600 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2023-03-07 16:37:02 +0100 |
commit | 3b405b3a70fea7bdb71126e93a554b774f1288c0 (patch) | |
tree | 317d0a6944a7b94d67116bdf1d872ce1da60f9d1 /worker/notmuch/worker.go | |
parent | ec6b552268bcdb8eca7a007df5ca15544342a6f3 (diff) | |
download | aerc-3b405b3a70fea7bdb71126e93a554b774f1288c0.tar.gz |
notmuch: use fswatcher to trigger directory count update
Use fswatcher to watch the underlying notmuch db to trigger directory
updates. Remove unused argument in the handleUpdateDirCounts function.
Call this function when listing directories to initialize counts when
starting aerc.
Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>
Tested-by: Ben Lee-Cohen <ben@lee-cohen.com>
Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'worker/notmuch/worker.go')
-rw-r--r-- | worker/notmuch/worker.go | 39 |
1 files changed, 29 insertions, 10 deletions
diff --git a/worker/notmuch/worker.go b/worker/notmuch/worker.go index 3a5dc141..49e598bf 100644 --- a/worker/notmuch/worker.go +++ b/worker/notmuch/worker.go @@ -13,6 +13,7 @@ import ( "net/url" "os" "os/exec" + "path" "path/filepath" "strconv" "strings" @@ -34,8 +35,6 @@ func init() { var errUnsupported = fmt.Errorf("unsupported command") -const backgroundRefreshDelay = 1 * time.Minute - type worker struct { w *types.Worker nmEvents chan eventType @@ -47,14 +46,21 @@ type worker struct { db *notmuch.DB setupErr error currentSortCriteria []*types.SortCriterion + watcher types.FSWatcher + watcherDebounce *time.Timer } // NewWorker creates a new notmuch worker with the provided worker. func NewWorker(w *types.Worker) (types.Backend, error) { events := make(chan eventType, 20) + watcher, err := handlers.NewWatcher() + if err != nil { + return nil, fmt.Errorf("(%s) could not create file system watcher: %w", w.Name, err) + } return &worker{ w: w, nmEvents: events, + watcher: watcher, }, nil } @@ -81,6 +87,15 @@ func (w *worker) Run() { if err != nil { log.Errorf("notmuch event failure: %v", err) } + case <-w.watcher.Events(): + if w.watcherDebounce != nil { + w.watcherDebounce.Stop() + } + // Debounce FS changes + w.watcherDebounce = time.AfterFunc(50*time.Millisecond, func() { + defer log.PanicHandler() + w.nmEvents <- &updateDirCounts{} + }) } } } @@ -203,14 +218,13 @@ func (w *worker) handleConnect(msg *types.Connect) error { } w.done(msg) w.emitLabelList() - go func() { - defer log.PanicHandler() - - for { - w.nmEvents <- &updateDirCounts{} - time.Sleep(backgroundRefreshDelay) - } - }() + // Watch all the files in the xapian folder for changes. We'll debounce + // changes, so catching multiple is ok + dbPath := path.Join(w.db.Path(), ".notmuch", "xapian") + err = w.watcher.Configure(dbPath) + if err != nil { + return err + } return nil } @@ -241,6 +255,11 @@ func (w *worker) handleListDirectories(msg *types.ListDirectories) error { }, }, nil) } + // Update dir counts when listing directories + err := w.handleUpdateDirCounts() + if err != nil { + return err + } w.done(msg) return nil } |