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
Provenance
- Commit
50b16d2- Tree
f26f751- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <christian@rocha.is>- Parent
4351c9f
1 file changed+11 added−11 removed
modified:tea.go+11 added-11 removed
90 unmodified lines91 init Init92 update Update93 view View94 subscriptions Subscriptions95- model Model95 }96 97 // 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 developer44 unmodified lines143 144 // Start initializes the program145 func (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 lines159 }160 defer restoreTerminal()161 162 // Initialize program163- p.model, cmd = p.init()163+ model, cmd = p.init()164 if cmd != nil {165 go func() {166 cmds <- cmd167 }()168 }169 170 // Render initial view171- linesRendered = p.render(p.model, linesRendered)171+ linesRendered = p.render(model, linesRendered)172 173 // 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 lines180 }181 }()182 183 // Initialize subscriptions184- subs = p.processSubs(msgs, subs)184+ subs = p.processSubs(msgs, model, subs)185 186 // Process commands187 go func() {188 for {28 unmodified lines217 }218 continue219 }220 221- 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 lines247 // 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.251-func (p *Program) processSubs(msgs chan Msg, activeSubs subManager) subManager {251+func (p *Program) processSubs(msgs chan Msg, model Model, activeSubs subManager) subManager {252 253 // Nothing to do.254 if p.subscriptions == nil && activeSubs == nil {255 return activeSubs4 unmodified lines260 activeSubs.endAll()261 return activeSubs262 }263 264- newSubs := p.subscriptions(p.model)264+ newSubs := p.subscriptions(model)265 266 // newSubs is an empty map. Cancel any active subscriptions and return.267 if newSubs == nil {268 activeSubs.endAll()30 unmodified lines299 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