forked from Mirrors/bubbletea
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:
parent
b6c8792309
commit
5999ff458c
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
|
// Process commands
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
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
|
// Handle updates and draw
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
|
Loading…
Reference in New Issue