Add worker callbacks to account UI

This commit is contained in:
Drew DeVault 2018-02-01 08:48:33 -05:00
parent 3139148c7b
commit cfe82414c4
1 changed files with 32 additions and 15 deletions

View File

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