From a15ea01cfb0a303355b2e6bb31e85ece0d048ac2 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Sun, 19 May 2019 09:49:57 +0000 Subject: Update internal state and draw from the same goroutine This commit introduces a new Aerc.Tick function that should be called to refresh the internal state. This in turn makes each AccountView process worker events. The UI goroutine repeatedly refreshes the internal state before drawing a new frame. The reason for this is that many worker messages may need to be processed for a single frame, and drawing the UI is far slower than refreshing the internal state. This has been confirmed in my testing (calling Aerc.Tick only once per frame results in a slower display). Many synchronization code has been removed. We can now write widgets without having to care so much about races. The remaining sync users are: - widgets/spinner: the spinner value is updated from inside an internal goroutine - lib/ui/invalidatable: Invalidate may be called from any goroutine - lib/ui/grid: same - lib/ui/ui: an internal goroutine needs read access to UI.exit - worker/types/worker: Worker.callbacks is used for both worker and UI callbacks The exact goroutine requirements for Drawable have been documented. --- widgets/account.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'widgets/account.go') diff --git a/widgets/account.go b/widgets/account.go index afae1d2..431e7b8 100644 --- a/widgets/account.go +++ b/widgets/account.go @@ -65,13 +65,6 @@ func NewAccountView(conf *config.AercConfig, acct *config.AccountConfig, } go worker.Backend.Run() - go func() { - for { - msg := <-worker.Messages - msg = worker.ProcessMessage(msg) - view.onMessage(msg) - } - }() worker.PostAction(&types.Configure{Config: acct}, nil) worker.PostAction(&types.Connect{}, view.connected) @@ -80,6 +73,17 @@ func NewAccountView(conf *config.AercConfig, acct *config.AccountConfig, return view } +func (acct *AccountView) Tick() bool { + select { + case msg := <-acct.worker.Messages: + msg = acct.worker.ProcessMessage(msg) + acct.onMessage(msg) + return true + default: + return false + } +} + func (acct *AccountView) AccountConfig() *config.AccountConfig { return acct.acct } -- cgit 1.4.1-2-gfad0