Skip to content
Myr

Model doesn't need to be a member of the Program struct

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This also fixes a race condition

authored by Christian Rocha · committed by Christian Rocha · parent 4351c9f · Browse files · 1 file+11 added−11 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+11 added−11 removed

  • other:+11 added−11 removed

1 file+11 added−11 removed

modified:tea.go+11 added-11 removed
90 unmodified lines
91 init Init92 update Update93 view View94 subscriptions Subscriptions95 model Model95}9697// ErrMsg is just a regular message containing an error. We handle it in Update98// just like a regular message by case switching. Of course, the developer
44 unmodified lines
143144// Start initializes the program145func (p *Program) Start() error {146 var (147 model Model148 cmd Cmd149 subs = make(subManager)150 cmds = make(chan Cmd)151 msgs = make(chan Msg)
7 unmodified lines
159 }160 defer restoreTerminal()161162 // Initialize program163 p.model, cmd = p.init()163 model, cmd = p.init()164 if cmd != nil {165 go func() {166 cmds <- cmd167 }()168 }169170 // Render initial view171 linesRendered = p.render(p.model, linesRendered)171 linesRendered = p.render(model, linesRendered)172173 // Subscribe to user input. We could move this out of here and offer it174 // as a subscription, but it blocks nicely and seems to be a common enough175 // need that we're enabling it by default.
4 unmodified lines
180 }181 }()182183 // Initialize subscriptions184 subs = p.processSubs(msgs, subs)184 subs = p.processSubs(msgs, model, subs)185186 // Process commands187 go func() {188 for {
28 unmodified lines
217 }218 continue219 }220221 p.model, cmd = p.update(msg, p.model) // run update222 cmds <- cmd // process command (if any)223 subs = p.processSubs(msgs, subs) // check for new and outdated subscriptions224 linesRendered = p.render(p.model, linesRendered) // render to terminal221 model, cmd = p.update(msg, model) // run update222 cmds <- cmd // process command (if any)223 subs = p.processSubs(msgs, model, subs) // check for new and outdated subscriptions224 linesRendered = p.render(model, linesRendered) // render to terminal225 }226 }227}228
18 unmodified lines
247// by ending the Goroutines they run on. If we notice new subscriptions which248// aren't currently running, we run them as loops in a new Goroutine.249//250// This function should be called on initialization and after every update.251func (p *Program) processSubs(msgs chan Msg, activeSubs subManager) subManager {251func (p *Program) processSubs(msgs chan Msg, model Model, activeSubs subManager) subManager {252253 // Nothing to do.254 if p.subscriptions == nil && activeSubs == nil {255 return activeSubs
4 unmodified lines
260 activeSubs.endAll()261 return activeSubs262 }263264 newSubs := p.subscriptions(p.model)264 newSubs := p.subscriptions(model)265266 // newSubs is an empty map. Cancel any active subscriptions and return.267 if newSubs == nil {268 activeSubs.endAll()
30 unmodified lines
299 for {300 select {301 case <-done:302 return303 case msgs <- s(p.model):303 case msgs <- s(model):304 continue305 }306 }307 }(activeSubs[key].done, activeSubs[key].sub)
19 unmodified lines