Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatic command chaining: Cmd -> Cmd -> Msg #984

Open
antonmedv opened this issue Apr 20, 2024 · 1 comment
Open

Automatic command chaining: Cmd -> Cmd -> Msg #984

antonmedv opened this issue Apr 20, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@antonmedv
Copy link

Is your feature request related to a problem? Please describe.
No.

Describe the solution you'd like

	switch msg := msg.(type) {
	case tea.Cmd:
		return m, msg
	}

Describe alternatives you've considered
N/A

Additional context
I have a Cmd which produces other Cmds:

func (m *Model) Create(name string) tea.Cmd {
	return func() tea.Msg {
		if !isAlphaNumeric(name) {
			return tea.Println("Only alphanumeric characters are allowed.")
		}

		var game db.Game
		m.app.db.Where("name = ?", name).First(&game)
		if game.ID != 0 {
			return tea.Printf("Game %q already exists. Please choose another name.", name)
		}


		game = db.Game{
			Name:       name,
		}
		if err := m.app.db.Create(&game).Error; err != nil {
			slog.Error("Failed to create game", "err", err.Error())
		}
		return tea.Printf("Game created!")
	}
}

It will be cool it cmds can produce other cmds.

This is easily solvable via:

	switch msg := msg.(type) {
	case tea.Cmd:
		return m, msg
	}

But as Msg == any it took me some time to understand why nothing is printed. What about adding this as default behavior?

@antonmedv antonmedv added the enhancement New feature or request label Apr 20, 2024
@KevM
Copy link

KevM commented Apr 20, 2024

When I run into this concern. I return a message type that when handled in the Update creates the desired tea.Cmd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants