Skip to content
Myr

Init, Update and View are now methods on interface Model

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

authored by Christian Rocha · committed by Christian Rocha · parent 71bd542 · Browse files · 1 file+25 added−29 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+25 added−29 removed

  • other:+25 added−29 removed

1 file+25 added−29 removed

modified:tea.go+25 added-29 removed
21 unmodified lines
22// Msg represents an action and is usually the result of an IO operation. It's23// triggers the Update function, and henceforth, the UI.24type Msg interface{}2526// Model contains the program's state.27type Model interface{}26// Model contains the program's state as well as it's core functions.27type Model interface {28 // Init is the first function that will be called. It returns your an29 // optional initial command.30 Init() Cmd3132 // Update is called when a message is received. Use it to inspect messages33 // and, in response, update the model and/or send a command.34 Update(Msg) (Model, Cmd)3536 // View renders the program's UI, which is just a string. The view is37 // rendered after every Update.38 View() string39}4041// Cmd is an IO operation. If it's nil it's considered a no-op. Use it for42// things like HTTP requests, timers, saving and loading from disk, and so on.43//44// There's almost never a need to use a command to send a message to another
11 unmodified lines
56 return batchMsg(cmds)57 }58}5948// Init is the first function that will be called. It returns your initial49// model and runs an optional command.50type Init func() (Model, Cmd)5152// Update is called when a message is received. Use it to inspect messages and,53// in response, update the model and/or send a command.54type Update func(Msg, Model) (Model, Cmd)5556// View renders the program's UI, which is just a string. The view is rendered57// after every Update.58type View func(Model) string5960// Program is a terminal user interface.61type Program struct {62 init Init63 update Update64 view View62 initialModel Model6364 mtx sync.Mutex65 output *os.File // where to send output. this will usually be os.Stdout.66 renderer *renderer
26 unmodified lines
93 Height int94}9596// NewProgram creates a new Program.99func NewProgram(init Init, update Update, view View) *Program {97func NewProgram(model Model) *Program {98 return &Program{101 init: init,102 update: update,103 view: view,104105 output: os.Stdout,106 CatchPanics: true,99 initialModel: model,100 output: os.Stdout,101 CatchPanics: true,102 }103}104105// Start initializes the program.
24 unmodified lines
130 }131 defer restoreTerminal()132133 // Initialize program139 model, initCmd := p.init()134 model := p.initialModel135 initCmd := model.Init()136 if initCmd != nil {137 go func() {138 cmds <- initCmd139 }()
3 unmodified lines
143 p.renderer.start()144 p.renderer.altScreenActive = p.altScreenActive145146 // Render initial view151 p.renderer.write(p.view(model))147 p.renderer.write(model.View())148149 // Subscribe to user input150 go func() {151 for {
58 unmodified lines
210211 // Process internal messages for the renderer212 p.renderer.handleMessages(msg)213 var cmd Cmd218 model, cmd = p.update(msg, model) // run update219 cmds <- cmd // process command (if any)220 p.renderer.write(p.view(model)) // send view to renderer214 model, cmd = model.Update(msg) // run update215 cmds <- cmd // process command (if any)216 p.renderer.write(model.View()) // send view to renderer217 }218 }219}220
60 unmodified lines