Skip to content

Commit

Permalink
feat!: make Init return the model (#1112)
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Aug 23, 2024
1 parent b2e983a commit ad68c42
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
4 changes: 2 additions & 2 deletions exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ type testExecModel struct {
err error
}

func (m testExecModel) Init() Cmd {
func (m *testExecModel) Init() (Model, Cmd) {
c := exec.Command(m.cmd) //nolint:gosec
return ExecProcess(c, func(err error) Msg {
return m, ExecProcess(c, func(err error) Msg {
return execFinishedMsg{err}
})
}
Expand Down
6 changes: 4 additions & 2 deletions tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type Msg interface{}
type Model interface {
// Init is the first function that will be called. It returns an optional
// initial command. To not perform an initial command return nil.
Init() Cmd
Init() (Model, Cmd)

// Update is called when a message is received. Use it to inspect messages
// and, in response, update the model and/or send a command.
Expand Down Expand Up @@ -762,7 +762,9 @@ func (p *Program) Run() (Model, error) {
p.startRenderer()

// Initialize the program.
if initCmd := model.Init(); initCmd != nil {
var initCmd Cmd
model, initCmd = model.Init()
if initCmd != nil {
ch := make(chan struct{})
handlers.add(ch)

Expand Down
4 changes: 2 additions & 2 deletions tea_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type testModel struct {
counter atomic.Value
}

func (m testModel) Init() Cmd {
return nil
func (m *testModel) Init() (Model, Cmd) {
return m, nil
}

func (m *testModel) Update(msg Msg) (Model, Cmd) {
Expand Down

0 comments on commit ad68c42

Please sign in to comment.