Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions fetcher/idle.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func (w *IdleWatcher) Watch(account *config.Account, folder string) {
// Stop existing watcher for this account if any
if existing, ok := w.watchers[account.ID]; ok {
close(existing.stop)
<-existing.done
delete(w.watchers, account.ID)
// Let old connection tear down in the background
}

a := &accountIdle{
Expand All @@ -62,18 +62,45 @@ func (w *IdleWatcher) Watch(account *config.Account, folder string) {
go a.run()
}

// Stop stops the IDLE watcher for a specific account.
func (w *IdleWatcher) Stop(accountID string) {
w.mu.Lock()
defer w.mu.Unlock()

if a, ok := w.watchers[accountID]; ok {
close(a.stop)
delete(w.watchers, accountID)
// Let old connection tear down in the background
}
}

// StopAll stops all IDLE watchers.
func (w *IdleWatcher) StopAll() {
w.mu.Lock()
defer w.mu.Unlock()

for id, a := range w.watchers {
close(a.stop)
<-a.done
delete(w.watchers, id)
}
}

// StopAllAndWait stops all IDLE watchers and waits for them to finish.
func (w *IdleWatcher) StopAllAndWait() {
w.mu.Lock()
var pending []chan struct{}
for id, a := range w.watchers {
close(a.stop)
pending = append(pending, a.done)
delete(w.watchers, id)
}
w.mu.Unlock()

for _, done := range pending {
<-done
}
}

func (a *accountIdle) run() {
defer close(a.done)

Expand Down
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"path/filepath"
"regexp"
"runtime"
"slices"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -416,6 +417,12 @@ func (m *mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
// Update IDLE watchers to monitor the new folder
for i := range m.config.Accounts {
// Only start IDLE for accounts that actually have this folder
folders := config.GetCachedFolders(m.config.Accounts[i].ID)
if !slices.Contains(folders, msg.FolderName) {
m.idleWatcher.Stop(m.config.Accounts[i].ID)
continue
}
m.idleWatcher.Watch(&m.config.Accounts[i], msg.FolderName)
}
if m.plugins != nil {
Expand Down
Loading