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 {
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)
}
}