From 5999ff458cca5bb70ff6998a990dfb1edfaeab59 Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Tue, 14 Jan 2020 16:13:13 -0500 Subject: [PATCH] Refactor subscription implementation There was a bug in the previous implementation where it didn't allow for more than one subscription. --- tea.go | 35 +++++++++++++---------------------- 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/tea.go b/tea.go index c2befea..a424c0c 100644 --- a/tea.go +++ b/tea.go @@ -99,6 +99,19 @@ func (p *Program) Start() error { } }() + // Process subscriptions + go func() { + if len(p.subscriptions) > 0 { + for _, sub := range p.subscriptions { + go func(s Sub) { + for { + msgs <- s(p.model) + } + }(sub) + } + } + }() + // Process commands go func() { for { @@ -115,28 +128,6 @@ func (p *Program) Start() error { } }() - // Subscriptions. Subscribe to user input. - // TODO: should the blocking `for` be here, or in the end-user portion - // of the program? - go func() { - select { - case <-done: - return - default: - if len(p.subscriptions) > 0 { - for _, sub := range p.subscriptions { - if sub != nil { - go func() { - for { - msgs <- sub(p.model) - } - }() - } - } - } - } - }() - // Handle updates and draw for { select {