refactor(app): add DestinationWentOffAirEvent

This commit is contained in:
Rob Watson 2025-04-25 18:06:31 +02:00
parent 1f4a931903
commit 2f1cadcf40
3 changed files with 27 additions and 22 deletions

View File

@ -197,7 +197,8 @@ func (a *App) Run(ctx context.Context) error {
destErrors := applyReplicatorState(replState, state) destErrors := applyReplicatorState(replState, state)
for _, destError := range destErrors { for _, destError := range destErrors {
handleDestError(destError, repl, ui) a.eventBus.Send(event.DestinationStreamExitedEvent{Name: destError.name, Err: destError.err})
repl.StopDestination(destError.url)
} }
updateUI() updateUI()
@ -316,13 +317,6 @@ func applyReplicatorState(replState replicator.State, appState *domain.AppState)
return errorsToDisplay 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 // applyConfig applies the config to the app state. For now we only set the
// destinations. // destinations.
func applyConfig(cfg config.Config, appState *domain.AppState) { func applyConfig(cfg config.Config, appState *domain.AppState) {

View File

@ -8,6 +8,7 @@ const (
EventNameAppStateChanged Name = "app_state_changed" EventNameAppStateChanged Name = "app_state_changed"
EventNameDestinationAdded Name = "destination_added" EventNameDestinationAdded Name = "destination_added"
EventNameAddDestinationFailed Name = "add_destination_failed" EventNameAddDestinationFailed Name = "add_destination_failed"
EventNameDestinationStreamExited Name = "destination_stream_exited"
EventNameStartDestinationFailed Name = "start_destination_failed" EventNameStartDestinationFailed Name = "start_destination_failed"
EventNameDestinationRemoved Name = "destination_removed" EventNameDestinationRemoved Name = "destination_removed"
EventNameRemoveDestinationFailed Name = "remove_destination_failed" EventNameRemoveDestinationFailed Name = "remove_destination_failed"
@ -48,6 +49,16 @@ func (e AddDestinationFailedEvent) name() Name {
return EventNameAddDestinationFailed 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. // StartDestinationFailedEvent is emitted when a destination fails to start.
type StartDestinationFailedEvent struct{} type StartDestinationFailedEvent struct{}

View File

@ -305,6 +305,8 @@ func (ui *UI) run(ctx context.Context) {
ui.handleStartDestinationFailed(evt) ui.handleStartDestinationFailed(evt)
case event.AddDestinationFailedEvent: case event.AddDestinationFailedEvent:
ui.handleDestinationEventError(evt.Err) ui.handleDestinationEventError(evt.Err)
case event.DestinationStreamExitedEvent:
ui.handleDestinationStreamExited(evt)
case event.DestinationRemovedEvent: case event.DestinationRemovedEvent:
ui.handleDestinationRemoved(evt) ui.handleDestinationRemoved(evt)
case event.RemoveDestinationFailedEvent: case event.RemoveDestinationFailedEvent:
@ -431,20 +433,18 @@ func (ui *UI) handleOtherInstanceDetected(event.OtherInstanceDetectedEvent) {
) )
} }
func (ui *UI) ShowDestinationErrorModal(name string, err error) { func (ui *UI) handleDestinationStreamExited(evt event.DestinationStreamExitedEvent) {
ui.app.QueueUpdateDraw(func() { ui.showModal(
ui.showModal( pageNameModalDestinationError,
pageNameModalDestinationError, fmt.Sprintf(
fmt.Sprintf( "Streaming to %s failed:\n\n%s",
"Streaming to %s failed:\n\n%s", cmp.Or(evt.Name, "this destination"),
cmp.Or(name, "this destination"), evt.Err,
err, ),
), []string{"Ok"},
[]string{"Ok"}, true,
true, nil,
nil, )
)
})
} }
func (ui *UI) handleFatalErrorOccurred(evt event.FatalErrorOccurredEvent) { func (ui *UI) handleFatalErrorOccurred(evt event.FatalErrorOccurredEvent) {