mirror of
https://github.com/taigrr/bubbletea.git
synced 2026-04-02 02:59:09 -07:00
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:
35
tea.go
35
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 {
|
||||
|
||||
Reference in New Issue
Block a user