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
Provenance
- Commit
26899c7- Tree
2f6ab52- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <christian@rocha.is>- Parent
71bd542
1 file changed+25 added−29 removed
modified:tea.go+25 added-29 removed
21 unmodified lines22 // Msg represents an action and is usually the result of an IO operation. It's23 // triggers the Update function, and henceforth, the UI.24 type Msg interface{}25 26-// Model contains the program's state.27-type Model interface{}26+// Model contains the program's state as well as it's core functions.27+type Model interface {28+ // Init is the first function that will be called. It returns your an29+ // optional initial command.30+ Init() Cmd31 32+ // 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)35+36+ // View renders the program's UI, which is just a string. The view is37+ // rendered after every Update.38+ View() string39+}40+41 // 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 another11 unmodified lines56 return batchMsg(cmds)57 }58 }59 48-// Init is the first function that will be called. It returns your initial49-// model and runs an optional command.50-type Init func() (Model, Cmd)51-52-// 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.54-type Update func(Msg, Model) (Model, Cmd)55-56-// View renders the program's UI, which is just a string. The view is rendered57-// after every Update.58-type View func(Model) string59-60 // Program is a terminal user interface.61 type Program struct {62- init Init63- update Update64- view View62+ initialModel Model63 64 mtx sync.Mutex65 output *os.File // where to send output. this will usually be os.Stdout.66 renderer *renderer26 unmodified lines93 Height int94 }95 96 // NewProgram creates a new Program.99-func NewProgram(init Init, update Update, view View) *Program {97+func NewProgram(model Model) *Program {98 return &Program{101- init: init,102- update: update,103- view: view,104-105- output: os.Stdout,106- CatchPanics: true,99+ initialModel: model,100+ output: os.Stdout,101+ CatchPanics: true,102 }103 }104 105 // Start initializes the program.24 unmodified lines130 }131 defer restoreTerminal()132 133 // 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 lines143 p.renderer.start()144 p.renderer.altScreenActive = p.altScreenActive145 146 // Render initial view151- p.renderer.write(p.view(model))147+ p.renderer.write(model.View())148 149 // Subscribe to user input150 go func() {151 for {58 unmodified lines210 211 // 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