shithub: mycel

Download patch

ref: 49b9deeb4199bb7c9d402970fb2394a0a94f84dc
parent: b61213d33be35446f1fff9efcb83adbb3eaf4d54
author: Philip Silva <philip.silva@protonmail.com>
date: Fri Sep 24 09:02:39 EDT 2021

Fix race with status msgs

--- a/browser/browser.go
+++ b/browser/browser.go
@@ -1391,11 +1391,11 @@
 	history.History
 	dui       *duit.DUI
 	Website   *Website
-	StatusBar *duit.Label
 	loading bool
 	client    *http.Client
 	Download func(res chan *string)
 	LocCh chan string
+	StatusCh chan string
 }
 
 func NewBrowser(_dui *duit.DUI, initUrl string) (b *Browser) {
@@ -1409,11 +1409,9 @@
 			Jar: jar,
 		},
 		dui: _dui,
-		StatusBar: &duit.Label{
-			Text: "",
-		},
 		Website: &Website{},
 		LocCh: make(chan string, 10),
+		StatusCh: make(chan string, 10),
 	}
 
 	u, err := url.Parse(initUrl)
@@ -1600,33 +1598,10 @@
 	return c.Buf, c.ContentType, err
 }
 
-func (b *Browser) statusBarMsg(msg string, emptyBody bool) {
-	if dui == nil || dui.Top.UI == nil {
-		return
-	}
-
-	go func() {
-		dui.Call <- func() {
-			if msg == "" {
-				b.StatusBar.Text = ""
-			} else {
-				b.StatusBar.Text += msg + "\n"
-			}
-			if emptyBody {
-				b.Website.UI = &duit.Label{}
-			}
-
-			dui.MarkLayout(dui.Top.UI)
-			dui.MarkDraw(dui.Top.UI)
-			dui.Render()
-		}
-	}()
-}
-
 func (b *Browser) get(uri *url.URL, isNewOrigin bool) (buf []byte, contentType opossum.ContentType, err error) {
 	msg := fmt.Sprintf("Get %v", uri.String())
 	log.Printf(msg)
-	b.statusBarMsg(msg, true)
+	b.StatusCh <- msg
 	req, err := http.NewRequest("GET", uri.String(), nil)
 	if err != nil {
 		return
--- a/browser/website.go
+++ b/browser/website.go
@@ -27,7 +27,7 @@
 
 func (w *Website) layout(f opossum.Fetcher, htm string, layouting int) {
 	defer func() {
-		browser.statusBarMsg("", false)
+		browser.StatusCh <- ""
 	}()
 	pass := func(htm string, csss ...string) (*html.Node, map[*html.Node]style.Map) {
 
--- a/cmd/opossum/main.go
+++ b/cmd/opossum/main.go
@@ -39,33 +39,39 @@
 
 type Nav struct {
 	LocationField *duit.Field
+	StatusBar *duit.Label
 }
 
 func NewNav() (n *Nav) {
 	n = &Nav{
-		LocationField: &duit.Field{
-			Text:    loc,
-			Font:    Style.Font(),
-			Keys:    func(k rune, m draw.Mouse) (e duit.Event) {
-				if k == browser.EnterKey && !b.Loading() {
-					a := n.LocationField.Text
-					if !strings.HasPrefix(strings.ToLower(a), "http") {
-						a = "http://" + a
-					}
-					u, err := url.Parse(a)
-					if err != nil {
-						log.Errorf("parse url: %v", err)
-						return
-					}
-					return b.LoadUrl(u)
-				}
-				return
-			},
+		StatusBar: &duit.Label{
+			Text: "",
 		},
 	}
+	n.LocationField = &duit.Field{
+		Text:    loc,
+		Font:    Style.Font(),
+		Keys:    n.keys,
+	}
 	return
 }
 
+func (n *Nav) keys(k rune, m draw.Mouse) (e duit.Event) {
+	if k == browser.EnterKey && !b.Loading() {
+		a := n.LocationField.Text
+		if !strings.HasPrefix(strings.ToLower(a), "http") {
+			a = "http://" + a
+		}
+		u, err := url.Parse(a)
+		if err != nil {
+			log.Errorf("parse url: %v", err)
+			return
+		}
+		return b.LoadUrl(u)
+	}
+	return
+}
+
 func (n *Nav) Render() []*duit.Kid {
 	uis := []duit.UI{
 		&duit.Grid{
@@ -86,9 +92,10 @@
 				},
 			),
 		},
+		n.StatusBar,
 	}
 	if b != nil {
-		uis = append(uis, b.StatusBar, b.Website)
+		uis = append(uis, b.Website)
 	}
 	return duit.NewKids(uis...)
 }
@@ -209,6 +216,18 @@
 			log.Infof("loc=%v", loc)
 			if nav, ok := v.(*Nav); ok {
 				nav.LocationField.Text = loc
+			}
+
+		case msg := <- b.StatusCh:
+			if nav, ok := v.(*Nav); ok {
+				if msg == "" {
+					nav.StatusBar.Text = ""
+				} else {
+					nav.StatusBar.Text += msg + "\n"
+				}
+				dui.MarkLayout(nav.StatusBar)
+				dui.MarkDraw(nav.StatusBar)
+				dui.Render()
 			}
 
 		case err, ok := <-dui.Error: