From 2f1cadcf40c08080bf15fb59f010223cee06edbb Mon Sep 17 00:00:00 2001 From: Rob Watson Date: Fri, 25 Apr 2025 18:06:31 +0200 Subject: [PATCH] refactor(app): add DestinationWentOffAirEvent --- internal/app/app.go | 10 ++-------- internal/event/events.go | 11 +++++++++++ internal/terminal/terminal.go | 28 ++++++++++++++-------------- 3 files changed, 27 insertions(+), 22 deletions(-) diff --git a/internal/app/app.go b/internal/app/app.go index 91db45a..c3c3a00 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -197,7 +197,8 @@ func (a *App) Run(ctx context.Context) error { destErrors := applyReplicatorState(replState, state) for _, destError := range destErrors { - handleDestError(destError, repl, ui) + a.eventBus.Send(event.DestinationStreamExitedEvent{Name: destError.name, Err: destError.err}) + repl.StopDestination(destError.url) } updateUI() @@ -316,13 +317,6 @@ func applyReplicatorState(replState replicator.State, appState *domain.AppState) return errorsToDisplay } -// handleDestError displays a modal to the user, and stops the destination. -func handleDestError(destError destinationError, repl *replicator.Actor, ui *terminal.UI) { - ui.ShowDestinationErrorModal(destError.name, destError.err) - - repl.StopDestination(destError.url) -} - // applyConfig applies the config to the app state. For now we only set the // destinations. func applyConfig(cfg config.Config, appState *domain.AppState) { diff --git a/internal/event/events.go b/internal/event/events.go index c7c8ff8..b9d15a7 100644 --- a/internal/event/events.go +++ b/internal/event/events.go @@ -8,6 +8,7 @@ const ( EventNameAppStateChanged Name = "app_state_changed" EventNameDestinationAdded Name = "destination_added" EventNameAddDestinationFailed Name = "add_destination_failed" + EventNameDestinationStreamExited Name = "destination_stream_exited" EventNameStartDestinationFailed Name = "start_destination_failed" EventNameDestinationRemoved Name = "destination_removed" EventNameRemoveDestinationFailed Name = "remove_destination_failed" @@ -48,6 +49,16 @@ func (e AddDestinationFailedEvent) name() Name { return EventNameAddDestinationFailed } +// DestinationStreamExitedEvent is emitted when a destination goes off-air unexpectedly. +type DestinationStreamExitedEvent struct { + Name string + Err error +} + +func (e DestinationStreamExitedEvent) name() Name { + return EventNameDestinationStreamExited +} + // StartDestinationFailedEvent is emitted when a destination fails to start. type StartDestinationFailedEvent struct{} diff --git a/internal/terminal/terminal.go b/internal/terminal/terminal.go index f7e25ab..97ec572 100644 --- a/internal/terminal/terminal.go +++ b/internal/terminal/terminal.go @@ -305,6 +305,8 @@ func (ui *UI) run(ctx context.Context) { ui.handleStartDestinationFailed(evt) case event.AddDestinationFailedEvent: ui.handleDestinationEventError(evt.Err) + case event.DestinationStreamExitedEvent: + ui.handleDestinationStreamExited(evt) case event.DestinationRemovedEvent: ui.handleDestinationRemoved(evt) case event.RemoveDestinationFailedEvent: @@ -431,20 +433,18 @@ func (ui *UI) handleOtherInstanceDetected(event.OtherInstanceDetectedEvent) { ) } -func (ui *UI) ShowDestinationErrorModal(name string, err error) { - ui.app.QueueUpdateDraw(func() { - ui.showModal( - pageNameModalDestinationError, - fmt.Sprintf( - "Streaming to %s failed:\n\n%s", - cmp.Or(name, "this destination"), - err, - ), - []string{"Ok"}, - true, - nil, - ) - }) +func (ui *UI) handleDestinationStreamExited(evt event.DestinationStreamExitedEvent) { + ui.showModal( + pageNameModalDestinationError, + fmt.Sprintf( + "Streaming to %s failed:\n\n%s", + cmp.Or(evt.Name, "this destination"), + evt.Err, + ), + []string{"Ok"}, + true, + nil, + ) } func (ui *UI) handleFatalErrorOccurred(evt event.FatalErrorOccurredEvent) {