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

View File

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

View File

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