diff options
author | Drew DeVault <sir@cmpwn.com> | 2018-02-01 08:48:33 -0500 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2018-02-01 08:48:33 -0500 |
commit | cfe82414c42ca56fb198b24a0ce2785d7e36512c (patch) | |
tree | 0a87cbbeab56130901eb74954d2f63ed78035ea4 | |
parent | 3139148c7b9ad6ed4fb9cd8cd3e4160a9b9ee46f (diff) |
Add worker callbacks to account UI
-rw-r--r-- | ui/account.go | 47 |
1 files changed, 32 insertions, 15 deletions
diff --git a/ui/account.go b/ui/account.go index 0cd61c8..f8dcce8 100644 --- 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) } } |