Refactor subscription implementation

There was a bug in the  previous implementation where it didn't
allow for more than one subscription.
This commit is contained in:
Christian Rocha 2020-01-14 16:13:13 -05:00
parent b6c8792309
commit 5999ff458c
No known key found for this signature in database
GPG Key ID: D6CC7A16E5878018
1 changed files with 13 additions and 22 deletions

35
tea.go
View File

@ -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 {