mirror of
https://github.com/taigrr/bubbletea.git
synced 2026-04-02 02:59:09 -07:00
Remove entire subscription model
It was a valiant effort, and the implementation was solid and dependable, but at the end of the day we can achieve the same functionality in a much simpler fashion with commands, especially because Go is not held to the same restrictions as Elm.
This commit is contained in:
@@ -13,7 +13,7 @@ import (
|
||||
// A model can be more or less any type of data. It holds all the data for a
|
||||
// program, so often it's a struct. For this simple example, however, all
|
||||
// we'll need is a simple integer.
|
||||
type Model int
|
||||
type model int
|
||||
|
||||
// Messages are events that we respond to in our Update function. This
|
||||
// particular one indicates that the timer has ticked.
|
||||
@@ -21,22 +21,22 @@ type tickMsg time.Time
|
||||
|
||||
func main() {
|
||||
// Initialize our program
|
||||
p := boba.NewProgram(initialize, update, view, subscriptions)
|
||||
p := boba.NewProgram(initialize, update, view)
|
||||
if err := p.Start(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func initialize() (boba.Model, boba.Cmd) {
|
||||
return Model(5), nil
|
||||
return model(5), tick
|
||||
}
|
||||
|
||||
// Update is called when messages are recived. The idea is that you inspect
|
||||
// the message and update the model (or send back a new one) accordingly. You
|
||||
// can also return a commmand, which is a function that peforms I/O and
|
||||
// returns a message.
|
||||
func update(msg boba.Msg, model boba.Model) (boba.Model, boba.Cmd) {
|
||||
m, _ := model.(Model)
|
||||
func update(msg boba.Msg, mdl boba.Model) (boba.Model, boba.Cmd) {
|
||||
m, _ := mdl.(model)
|
||||
|
||||
switch msg.(type) {
|
||||
case boba.KeyMsg:
|
||||
@@ -46,23 +46,19 @@ func update(msg boba.Msg, model boba.Model) (boba.Model, boba.Cmd) {
|
||||
if m <= 0 {
|
||||
return m, boba.Quit
|
||||
}
|
||||
return m, tick
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// Views take data from the model and return a string which will be rendered
|
||||
// to the terminal.
|
||||
func view(model boba.Model) string {
|
||||
m, _ := model.(Model)
|
||||
func view(mdl boba.Model) string {
|
||||
m, _ := mdl.(model)
|
||||
return fmt.Sprintf("Hi. This program will exit in %d seconds. To quit sooner press any key.\n", m)
|
||||
}
|
||||
|
||||
// This is a subscription which we setup in NewProgram(). It waits for one
|
||||
// second, sends a tick, and then restarts.
|
||||
func subscriptions(_ boba.Model) boba.Subs {
|
||||
return boba.Subs{
|
||||
"tick": boba.Every(time.Second, func(t time.Time) boba.Msg {
|
||||
return tickMsg(t)
|
||||
}),
|
||||
}
|
||||
func tick() boba.Msg {
|
||||
time.Sleep(time.Second)
|
||||
return tickMsg{}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user