aerc

Working clone of aerc-mail.org
git clone git://git.danielmoch.com/aerc.git
Log | Files | Refs | README | LICENSE

commit cfe82414c42ca56fb198b24a0ce2785d7e36512c
parent 3139148c7b9ad6ed4fb9cd8cd3e4160a9b9ee46f
Author: Drew DeVault <sir@cmpwn.com>
Date:   Thu,  1 Feb 2018 08:48:33 -0500

Add worker callbacks to account UI

Diffstat:
Mui/account.go | 47++++++++++++++++++++++++++++++++---------------
1 file changed, 32 insertions(+), 15 deletions(-)

diff --git a/ui/account.go b/ui/account.go @@ -11,11 +11,12 @@ import ( ) type AccountTab struct { - Config *config.AccountConfig - Worker worker.Worker - Parent *UIState - logger *log.Logger - counter int + Config *config.AccountConfig + Worker worker.Worker + Parent *UIState + logger *log.Logger + counter int + callbacks map[types.WorkerMessage]func(msg types.WorkerMessage) } func NewAccountTab(conf *config.AccountConfig, @@ -26,13 +27,21 @@ func NewAccountTab(conf *config.AccountConfig, return nil, err } go work.Run() - work.PostAction(types.Configure{Config: conf}) - work.PostAction(types.Connect{}) - return &AccountTab{ - Config: conf, - Worker: work, - logger: logger, - }, nil + acc := &AccountTab{ + Config: conf, + Worker: work, + logger: logger, + callbacks: make(map[types.WorkerMessage]func(msg types.WorkerMessage)), + } + acc.postAction(types.Configure{Config: conf}, nil) + acc.postAction(types.Connect{}, func(msg types.WorkerMessage) { + if _, ok := msg.(types.Ack); ok { + acc.logger.Println("Connected.") + } else { + acc.logger.Println("Connection failed.") + } + }) + return acc, nil } func (acc *AccountTab) Name() string { @@ -62,13 +71,21 @@ func (acc *AccountTab) GetChannel() chan types.WorkerMessage { return acc.Worker.GetMessages() } -func (acc *AccountTab) postAction(msg types.WorkerMessage) { +func (acc *AccountTab) postAction(msg types.WorkerMessage, + cb func(msg types.WorkerMessage)) { + acc.logger.Printf("-> %T\n", msg) acc.Worker.PostAction(msg) + if cb != nil { + acc.callbacks[msg] = cb + } } func (acc *AccountTab) HandleMessage(msg types.WorkerMessage) { acc.logger.Printf("<- %T\n", msg) + if cb, ok := acc.callbacks[msg.InResponseTo()]; ok { + cb(msg) + } switch msg.(type) { case types.Ack: // no-op @@ -77,10 +94,10 @@ func (acc *AccountTab) HandleMessage(msg types.WorkerMessage) { acc.logger.Println("Approving certificate") acc.postAction(types.Ack{ Message: types.RespondTo(msg), - }) + }, nil) default: acc.postAction(types.Unsupported{ Message: types.RespondTo(msg), - }) + }, nil) } }