charmbracelet / bubbletea

A powerful little TUI framework 🏗
MIT License
25.65k stars 744 forks source link

fix: prefer `tea.Tick` over goroutines #1022

Closed jshawl closed 1 month ago

jshawl commented 1 month ago

This pull request replaces the goroutine with tea.Tick() in the send-msg example.

From the blog post Commands in Bubble Tea:

Never use goroutines within a Bubble Tea program

I used this example to implement sending messages at an interval, but later discovered the tip in the blog post. Hopefully this example will communicate the best practice in addition to the blog post.

Thanks!

meowgorithm commented 1 month ago

Thanks for the PR. So in this particular example, we're illustrating how to inject messages from outside a program with Program.Send(). You’re correct that Goroutines shouldn't be used within a program, however in this case the Goroutine is actually outside the program in main.

So all said, your changes are good ones in terms of how we generally recommend writing Bubble Tea programs, but in this case I'd prefer if we left this example as-is to illustrate how Program.Send works.

jshawl commented 1 month ago

Thank you @meowgorithm! Hopefully I’ll be back with another contribution in the future :)