Sfoglia il codice sorgente

Ensures pingo tui is thread safe

arianagiroux 1 settimana fa
parent
commit
7653a7c6b3

+ 0 - 3
issues/bug:-polling-stops-randomly/description

@@ -1,3 +0,0 @@
-The polling stops randomly?
-
-Is it due errors being thrown that the goroutines sop?

+ 0 - 1
issues/bug:-polling-stops-randomly/status

@@ -1 +0,0 @@
-open

+ 0 - 5
issues/bug:-type-Address-fails-to-update-new-max_length/description

@@ -1,5 +0,0 @@
-type Address sometimes doesnt update max_length?
-
-I'm not entirely sure, but I think it might have to do with the pointer
-implementation of the Address struct? It would probably make threading
-sense to de-pointer(?) the struct

+ 0 - 1
issues/bug:-type-Address-fails-to-update-new-max_length/status

@@ -1 +0,0 @@
-open

+ 0 - 0
issues/bug:-type-Address-fails-to-update-new-max_length/tags/v0.1


+ 0 - 1
issues/consider-moving-ping-code-to-main/description

@@ -1 +0,0 @@
-Should ping code live in main.go?

+ 0 - 1
issues/consider-moving-ping-code-to-main/status

@@ -1 +0,0 @@
-open

+ 0 - 0
issues/consider-moving-ping-code-to-main/tags/v0.1


+ 14 - 11
tui.go

@@ -151,11 +151,14 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 
 	case tickMsg:
 		cmds = append(cmds, m.Tick())
-		cmds = append(cmds, m.Poll)
+		cmds = append(cmds, m.Poll())
+
+	case pollResultMsg:
+		m.Addresses[msg.index].results = msg.results
 	}
 
-	m.viewport, cmd = m.viewport.Update(msg)
 	m.viewport.SetContent(m.content())
+	m.viewport, cmd = m.viewport.Update(msg)
 	cmds = append(cmds, cmd)
 	// cmds = append(cmds, m.Poll)
 
@@ -180,15 +183,15 @@ func (m Model) View() string {
 	return fmt.Sprintf("\n%s\n%s\n%s", header, m.viewport.View(), footer)
 }
 
-// A wrapper for the underlying [tui.Address.Poll] function. For each address in
-// [tui.Model.Addresses], run its respective Poll function and update [tui.Model]
-//
-// NOTE(async): this function fully blocks execution of the current thread.
-func (m Model) Poll() tea.Msg {
+// Returns a batched set of tea.Cmd functions for each address.
+func (m Model) Poll() tea.Cmd {
+	var cmds []tea.Cmd
+
 	for i, element := range m.Addresses {
-		element.Poll()
-		// element.results = append(element.results, -1)
-		m.Addresses[i] = element
+		cmds = append(cmds, func() tea.Msg {
+			results, err := element.Poll()
+			return pollResultMsg{results: results, err: err, index: i}
+		})
 	}
-	return nil
+	return tea.Batch(cmds...)
 }