refactor: extract commands from domain

This commit is contained in:
Rob Watson 2025-04-26 16:23:04 +02:00
parent 8403d751b6
commit caa543703e
3 changed files with 20 additions and 20 deletions

View File

@ -211,7 +211,7 @@ func (a *App) Run(ctx context.Context) error {
// should not continue, i.e. quit.
func (a *App) handleCommand(
ctx context.Context,
cmd domain.Command,
cmd event.Command,
state *domain.AppState,
repl *replicator.Actor,
containerClient *container.Client,
@ -219,7 +219,7 @@ func (a *App) handleCommand(
) (bool, error) {
a.logger.Debug("Command received", "cmd", cmd.Name())
switch c := cmd.(type) {
case domain.CommandAddDestination:
case event.CommandAddDestination:
newCfg := a.cfg
newCfg.Destinations = append(newCfg.Destinations, config.Destination{
Name: c.DestinationName,
@ -233,7 +233,7 @@ func (a *App) handleCommand(
a.cfg = newCfg
a.handleConfigUpdate(state)
a.eventBus.Send(event.DestinationAddedEvent{URL: c.URL})
case domain.CommandRemoveDestination:
case event.CommandRemoveDestination:
repl.StopDestination(c.URL) // no-op if not live
newCfg := a.cfg
newCfg.Destinations = slices.DeleteFunc(newCfg.Destinations, func(dest config.Destination) bool {
@ -247,22 +247,22 @@ func (a *App) handleCommand(
a.cfg = newCfg
a.handleConfigUpdate(state)
a.eventBus.Send(event.DestinationRemovedEvent{URL: c.URL})
case domain.CommandStartDestination:
case event.CommandStartDestination:
if !state.Source.Live {
a.eventBus.Send(event.StartDestinationFailedEvent{})
break
}
repl.StartDestination(c.URL)
case domain.CommandStopDestination:
case event.CommandStopDestination:
repl.StopDestination(c.URL)
case domain.CommandCloseOtherInstance:
case event.CommandCloseOtherInstance:
if err := closeOtherInstances(ctx, containerClient); err != nil {
return false, fmt.Errorf("close other instances: %w", err)
}
startMediaServerC <- struct{}{}
case domain.CommandQuit:
case event.CommandQuit:
return false, nil
}

View File

@ -1,4 +1,4 @@
package domain
package event
// CommandAddDestination adds a destination.
type CommandAddDestination struct {

View File

@ -42,7 +42,7 @@ const (
// UI is responsible for managing the terminal user interface.
type UI struct {
eventBus *event.Bus
commandC chan domain.Command
commandC chan event.Command
clipboardAvailable bool
configFilePath string
rtmpURL, rtmpsURL string
@ -109,7 +109,7 @@ const defaultChanSize = 64
// StartUI starts the terminal user interface.
func StartUI(ctx context.Context, params StartParams) (*UI, error) {
chanSize := cmp.Or(params.ChanSize, defaultChanSize)
commandCh := make(chan domain.Command, chanSize)
commandCh := make(chan event.Command, chanSize)
app := tview.NewApplication()
@ -272,7 +272,7 @@ func (ui *UI) renderAboutView() {
}
// C returns a channel that receives commands from the user interface.
func (ui *UI) C() <-chan domain.Command {
func (ui *UI) C() <-chan event.Command {
return ui.commandC
}
@ -425,9 +425,9 @@ func (ui *UI) handleOtherInstanceDetected(event.OtherInstanceDetectedEvent) {
false,
func(buttonIndex int, _ string) {
if buttonIndex == 0 {
ui.commandC <- domain.CommandCloseOtherInstance{}
ui.commandC <- event.CommandCloseOtherInstance{}
} else {
ui.commandC <- domain.CommandQuit{}
ui.commandC <- event.CommandQuit{}
}
},
)
@ -457,7 +457,7 @@ func (ui *UI) handleFatalErrorOccurred(evt event.FatalErrorOccurredEvent) {
[]string{"Quit"},
false,
func(int, string) {
ui.commandC <- domain.CommandQuit{}
ui.commandC <- event.CommandQuit{}
},
)
}
@ -702,7 +702,7 @@ func (ui *UI) handleMediaServerClosed(exitReason string) {
SetBackgroundColor(tcell.ColorBlack).
SetTextColor(tcell.ColorWhite).
SetDoneFunc(func(int, string) {
ui.commandC <- domain.CommandQuit{}
ui.commandC <- event.CommandQuit{}
})
modal.SetBorderStyle(tcell.StyleDefault.Background(tcell.ColorBlack).Foreground(tcell.ColorWhite))
@ -873,7 +873,7 @@ func (ui *UI) addDestination() {
AddInputField(inputLabelName, "My stream", inputLen, nil, nil).
AddInputField(inputLabelURL, "rtmp://", inputLen, nil, nil).
AddButton("Add", func() {
ui.commandC <- domain.CommandAddDestination{
ui.commandC <- event.CommandAddDestination{
DestinationName: form.GetFormItemByLabel(inputLabelName).(*tview.InputField).GetText(),
URL: form.GetFormItemByLabel(inputLabelURL).(*tview.InputField).GetText(),
}
@ -931,7 +931,7 @@ func (ui *UI) removeDestination() {
false,
func(buttonIndex int, _ string) {
if buttonIndex == 0 {
ui.commandC <- domain.CommandRemoveDestination{URL: url}
ui.commandC <- event.CommandRemoveDestination{URL: url}
}
},
)
@ -1007,12 +1007,12 @@ func (ui *UI) toggleDestination() {
switch ss {
case startStateNotStarted:
ui.urlsToStartState[url] = startStateStarting
ui.commandC <- domain.CommandStartDestination{URL: url}
ui.commandC <- event.CommandStartDestination{URL: url}
case startStateStarting:
// do nothing
return
case startStateStarted:
ui.commandC <- domain.CommandStopDestination{URL: url}
ui.commandC <- event.CommandStopDestination{URL: url}
}
}
@ -1065,7 +1065,7 @@ func (ui *UI) confirmQuit() {
false,
func(buttonIndex int, _ string) {
if buttonIndex == 0 {
ui.commandC <- domain.CommandQuit{}
ui.commandC <- event.CommandQuit{}
}
},
)