forked from Mirrors/bubbletea
Model doesn't need to be a member of the Program struct
This also fixes a race condition
This commit is contained in:
parent
4351c9f903
commit
50b16d2df1
20
tea.go
20
tea.go
|
@ -92,7 +92,6 @@ type Program struct {
|
||||||
update Update
|
update Update
|
||||||
view View
|
view View
|
||||||
subscriptions Subscriptions
|
subscriptions Subscriptions
|
||||||
model Model
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ErrMsg is just a regular message containing an error. We handle it in Update
|
// ErrMsg is just a regular message containing an error. We handle it in Update
|
||||||
|
@ -145,6 +144,7 @@ func NewProgram(init Init, update Update, view View, subs Subscriptions) *Progra
|
||||||
// Start initializes the program
|
// Start initializes the program
|
||||||
func (p *Program) Start() error {
|
func (p *Program) Start() error {
|
||||||
var (
|
var (
|
||||||
|
model Model
|
||||||
cmd Cmd
|
cmd Cmd
|
||||||
subs = make(subManager)
|
subs = make(subManager)
|
||||||
cmds = make(chan Cmd)
|
cmds = make(chan Cmd)
|
||||||
|
@ -160,7 +160,7 @@ func (p *Program) Start() error {
|
||||||
defer restoreTerminal()
|
defer restoreTerminal()
|
||||||
|
|
||||||
// Initialize program
|
// Initialize program
|
||||||
p.model, cmd = p.init()
|
model, cmd = p.init()
|
||||||
if cmd != nil {
|
if cmd != nil {
|
||||||
go func() {
|
go func() {
|
||||||
cmds <- cmd
|
cmds <- cmd
|
||||||
|
@ -168,7 +168,7 @@ func (p *Program) Start() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Render initial view
|
// Render initial view
|
||||||
linesRendered = p.render(p.model, linesRendered)
|
linesRendered = p.render(model, linesRendered)
|
||||||
|
|
||||||
// Subscribe to user input. We could move this out of here and offer it
|
// Subscribe to user input. We could move this out of here and offer it
|
||||||
// as a subscription, but it blocks nicely and seems to be a common enough
|
// as a subscription, but it blocks nicely and seems to be a common enough
|
||||||
|
@ -181,7 +181,7 @@ func (p *Program) Start() error {
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Initialize subscriptions
|
// Initialize subscriptions
|
||||||
subs = p.processSubs(msgs, subs)
|
subs = p.processSubs(msgs, model, subs)
|
||||||
|
|
||||||
// Process commands
|
// Process commands
|
||||||
go func() {
|
go func() {
|
||||||
|
@ -218,10 +218,10 @@ func (p *Program) Start() error {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
p.model, cmd = p.update(msg, p.model) // run update
|
model, cmd = p.update(msg, model) // run update
|
||||||
cmds <- cmd // process command (if any)
|
cmds <- cmd // process command (if any)
|
||||||
subs = p.processSubs(msgs, subs) // check for new and outdated subscriptions
|
subs = p.processSubs(msgs, model, subs) // check for new and outdated subscriptions
|
||||||
linesRendered = p.render(p.model, linesRendered) // render to terminal
|
linesRendered = p.render(model, linesRendered) // render to terminal
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -248,7 +248,7 @@ func (p *Program) render(model Model, linesRendered int) int {
|
||||||
// aren't currently running, we run them as loops in a new Goroutine.
|
// aren't currently running, we run them as loops in a new Goroutine.
|
||||||
//
|
//
|
||||||
// This function should be called on initialization and after every update.
|
// This function should be called on initialization and after every update.
|
||||||
func (p *Program) processSubs(msgs chan Msg, activeSubs subManager) subManager {
|
func (p *Program) processSubs(msgs chan Msg, model Model, activeSubs subManager) subManager {
|
||||||
|
|
||||||
// Nothing to do.
|
// Nothing to do.
|
||||||
if p.subscriptions == nil && activeSubs == nil {
|
if p.subscriptions == nil && activeSubs == nil {
|
||||||
|
@ -261,7 +261,7 @@ func (p *Program) processSubs(msgs chan Msg, activeSubs subManager) subManager {
|
||||||
return activeSubs
|
return activeSubs
|
||||||
}
|
}
|
||||||
|
|
||||||
newSubs := p.subscriptions(p.model)
|
newSubs := p.subscriptions(model)
|
||||||
|
|
||||||
// newSubs is an empty map. Cancel any active subscriptions and return.
|
// newSubs is an empty map. Cancel any active subscriptions and return.
|
||||||
if newSubs == nil {
|
if newSubs == nil {
|
||||||
|
@ -300,7 +300,7 @@ func (p *Program) processSubs(msgs chan Msg, activeSubs subManager) subManager {
|
||||||
select {
|
select {
|
||||||
case <-done:
|
case <-done:
|
||||||
return
|
return
|
||||||
case msgs <- s(p.model):
|
case msgs <- s(model):
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue