Move msgstore map to dirstore

This map represents a mapping from directory names to their associated
messagestores anyway so they should be under dirstore. This simply moves
them there and adds some methods required to interact with them.
This commit is contained in:
Jeffas 2019-07-21 22:39:36 +01:00 committed by Drew DeVault
parent dc4c36adbf
commit 1b673b5ea7
3 changed files with 48 additions and 27 deletions

View File

@ -1,11 +1,13 @@
package lib package lib
type DirStore struct { type DirStore struct {
dirs []string dirs []string
msgStores map[string]*MessageStore
} }
func NewDirStore() *DirStore { func NewDirStore() *DirStore {
return &DirStore{} msgStores := make(map[string]*MessageStore)
return &DirStore{msgStores: msgStores}
} }
func (store *DirStore) Update(dirs []string) { func (store *DirStore) Update(dirs []string) {
@ -16,3 +18,12 @@ func (store *DirStore) Update(dirs []string) {
func (store *DirStore) List() []string { func (store *DirStore) List() []string {
return store.dirs return store.dirs
} }
func (store *DirStore) MessageStore(dirname string) (*MessageStore, bool) {
msgStore, ok := store.msgStores[dirname]
return msgStore, ok
}
func (store *DirStore) SetMessageStore(name string, msgStore *MessageStore) {
store.msgStores[name] = msgStore
}

View File

@ -16,15 +16,14 @@ import (
) )
type AccountView struct { type AccountView struct {
acct *config.AccountConfig acct *config.AccountConfig
conf *config.AercConfig conf *config.AercConfig
dirlist *DirectoryList dirlist *DirectoryList
grid *ui.Grid grid *ui.Grid
host TabHost host TabHost
logger *log.Logger logger *log.Logger
msglist *MessageList msglist *MessageList
msgStores map[string]*lib.MessageStore worker *types.Worker
worker *types.Worker
} }
func NewAccountView(conf *config.AercConfig, acct *config.AccountConfig, func NewAccountView(conf *config.AercConfig, acct *config.AccountConfig,
@ -58,15 +57,14 @@ func NewAccountView(conf *config.AercConfig, acct *config.AccountConfig,
grid.AddChild(msglist).At(0, 1) grid.AddChild(msglist).At(0, 1)
view := &AccountView{ view := &AccountView{
acct: acct, acct: acct,
conf: conf, conf: conf,
dirlist: dirlist, dirlist: dirlist,
grid: grid, grid: grid,
host: host, host: host,
logger: logger, logger: logger,
msglist: msglist, msglist: msglist,
msgStores: make(map[string]*lib.MessageStore), worker: worker,
worker: worker,
} }
go worker.Backend.Run() go worker.Backend.Run()
@ -187,7 +185,7 @@ func (acct *AccountView) onMessage(msg types.WorkerMessage) {
case *types.Done: case *types.Done:
switch msg.InResponseTo().(type) { switch msg.InResponseTo().(type) {
case *types.OpenDirectory: case *types.OpenDirectory:
if store, ok := acct.msgStores[acct.dirlist.selected]; ok { if store, ok := acct.dirlist.SelectedMsgStore(); ok {
// If we've opened this dir before, we can re-render it from // If we've opened this dir before, we can re-render it from
// memory while we wait for the update and the UI feels // memory while we wait for the update and the UI feels
// snappier. If not, we'll unset the store and show the spinner // snappier. If not, we'll unset the store and show the spinner
@ -200,7 +198,7 @@ func (acct *AccountView) onMessage(msg types.WorkerMessage) {
acct.dirlist.UpdateList(nil) acct.dirlist.UpdateList(nil)
} }
case *types.DirectoryInfo: case *types.DirectoryInfo:
if store, ok := acct.msgStores[msg.Info.Name]; ok { if store, ok := acct.dirlist.MsgStore(msg.Info.Name); ok {
store.Update(msg) store.Update(msg)
} else { } else {
store = lib.NewMessageStore(acct.worker, msg.Info, store = lib.NewMessageStore(acct.worker, msg.Info,
@ -208,26 +206,26 @@ func (acct *AccountView) onMessage(msg types.WorkerMessage) {
acct.conf.Triggers.ExecNewEmail(acct.acct, acct.conf.Triggers.ExecNewEmail(acct.acct,
acct.conf, msg) acct.conf, msg)
}) })
acct.msgStores[msg.Info.Name] = store acct.dirlist.SetMsgStore(msg.Info.Name, store)
store.OnUpdate(func(_ *lib.MessageStore) { store.OnUpdate(func(_ *lib.MessageStore) {
store.OnUpdate(nil) store.OnUpdate(nil)
acct.msglist.SetStore(store) acct.msglist.SetStore(store)
}) })
} }
case *types.DirectoryContents: case *types.DirectoryContents:
if store, ok := acct.msgStores[acct.dirlist.selected]; ok { if store, ok := acct.dirlist.SelectedMsgStore(); ok {
store.Update(msg) store.Update(msg)
} }
case *types.FullMessage: case *types.FullMessage:
if store, ok := acct.msgStores[acct.dirlist.selected]; ok { if store, ok := acct.dirlist.SelectedMsgStore(); ok {
store.Update(msg) store.Update(msg)
} }
case *types.MessageInfo: case *types.MessageInfo:
if store, ok := acct.msgStores[acct.dirlist.selected]; ok { if store, ok := acct.dirlist.SelectedMsgStore(); ok {
store.Update(msg) store.Update(msg)
} }
case *types.MessagesDeleted: case *types.MessagesDeleted:
if store, ok := acct.msgStores[acct.dirlist.selected]; ok { if store, ok := acct.dirlist.SelectedMsgStore(); ok {
store.Update(msg) store.Update(msg)
} }
case *types.Error: case *types.Error:

View File

@ -178,3 +178,15 @@ func (dirlist *DirectoryList) filterDirsByFoldersConfig() {
} }
dirlist.dirs = filtered dirlist.dirs = filtered
} }
func (dirlist *DirectoryList) SelectedMsgStore() (*lib.MessageStore, bool) {
return dirlist.store.MessageStore(dirlist.selected)
}
func (dirlist *DirectoryList) MsgStore(name string) (*lib.MessageStore, bool) {
return dirlist.store.MessageStore(name)
}
func (dirlist *DirectoryList) SetMsgStore(name string, msgStore *lib.MessageStore) {
dirlist.store.SetMessageStore(name, msgStore)
}