From f4021a28865cbc283eba9d06fdedeccad61a8384 Mon Sep 17 00:00:00 2001 From: Rob Watson Date: Wed, 23 Apr 2025 20:32:38 +0200 Subject: [PATCH] refactor(app): add destination error events --- internal/app/app.go | 8 +++---- internal/event/events.go | 43 +++++++++++++++++++++++++--------- internal/terminal/terminal.go | 44 +++++++++++++++++++++-------------- 3 files changed, 62 insertions(+), 33 deletions(-) diff --git a/internal/app/app.go b/internal/app/app.go index b7c2ecc..c74bada 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -220,8 +220,8 @@ func (a *App) handleCommand( URL: c.URL, }) if err := a.configService.SetConfig(newCfg); err != nil { - a.logger.Error("Config update failed", "err", err) - ui.ConfigUpdateFailed(err) + a.logger.Error("Add destination failed", "err", err) + a.eventBus.Send(event.AddDestinationFailedEvent{Err: err}) break } a.cfg = newCfg @@ -234,8 +234,8 @@ func (a *App) handleCommand( return dest.URL == c.URL }) if err := a.configService.SetConfig(newCfg); err != nil { - a.logger.Error("Config update failed", "err", err) - ui.ConfigUpdateFailed(err) + a.logger.Error("Remove destination failed", "err", err) + a.eventBus.Send(event.RemoveDestinationFailedEvent{Err: err}) break } a.cfg = newCfg diff --git a/internal/event/events.go b/internal/event/events.go index 8220528..921b3d1 100644 --- a/internal/event/events.go +++ b/internal/event/events.go @@ -5,11 +5,13 @@ import "git.netflux.io/rob/octoplex/internal/domain" type Name string const ( - EventNameAppStateChanged Name = "app_state_changed" - EventNameDestinationAdded Name = "destination_added" - EventNameDestinationRemoved Name = "destination_removed" - EventNameMediaServerStarted Name = "media_server_started" - EventNameFatalErrorOccurred Name = "fatal_error_occurred" + EventNameAppStateChanged Name = "app_state_changed" + EventNameDestinationAdded Name = "destination_added" + EventNameAddDestinationFailed Name = "add_destination_failed" + EventNameDestinationRemoved Name = "destination_removed" + EventNameRemoveDestinationFailed Name = "remove_destination_failed" + EventNameFatalErrorOccurred Name = "fatal_error_occurred" + EventNameMediaServerStarted Name = "media_server_started" ) // Event represents something which happened in the appllication. @@ -35,6 +37,15 @@ func (e DestinationAddedEvent) name() Name { return EventNameDestinationAdded } +// AddDestinationFailedEvent is emitted when a destination fails to be added. +type AddDestinationFailedEvent struct { + Err error +} + +func (e AddDestinationFailedEvent) name() Name { + return EventNameAddDestinationFailed +} + // DestinationRemovedEvent is emitted when a destination is successfully // removed. type DestinationRemovedEvent struct { @@ -45,14 +56,14 @@ func (e DestinationRemovedEvent) name() Name { return EventNameDestinationRemoved } -// MediaServerStartedEvent is emitted when the mediaserver component starts successfully. -type MediaServerStartedEvent struct { - RTMPURL string - RTMPSURL string +// RemoveDestinationFailedEvent is emitted when a destination fails to be +// removed. +type RemoveDestinationFailedEvent struct { + Err error } -func (e MediaServerStartedEvent) name() Name { - return "media_server_started" +func (e RemoveDestinationFailedEvent) name() Name { + return EventNameRemoveDestinationFailed } // FatalErrorOccurredEvent is emitted when a fatal application @@ -64,3 +75,13 @@ type FatalErrorOccurredEvent struct { func (e FatalErrorOccurredEvent) name() Name { return "fatal_error_occurred" } + +// MediaServerStartedEvent is emitted when the mediaserver component starts successfully. +type MediaServerStartedEvent struct { + RTMPURL string + RTMPSURL string +} + +func (e MediaServerStartedEvent) name() Name { + return "media_server_started" +} diff --git a/internal/terminal/terminal.go b/internal/terminal/terminal.go index a6bdbfa..2e38ca9 100644 --- a/internal/terminal/terminal.go +++ b/internal/terminal/terminal.go @@ -281,7 +281,9 @@ func (ui *UI) run(ctx context.Context) { appStateChangedC := ui.eventBus.Register(event.EventNameAppStateChanged) destinationAddedC := ui.eventBus.Register(event.EventNameDestinationAdded) + addDestinationFailedC := ui.eventBus.Register(event.EventNameAddDestinationFailed) destinationRemovedC := ui.eventBus.Register(event.EventNameDestinationRemoved) + removeDestinationFailedC := ui.eventBus.Register(event.EventNameRemoveDestinationFailed) mediaServerStartedC := ui.eventBus.Register(event.EventNameMediaServerStarted) fatalErrorOccurredC := ui.eventBus.Register(event.EventNameFatalErrorOccurred) @@ -306,10 +308,18 @@ func (ui *UI) run(ctx context.Context) { ui.app.QueueUpdateDraw(func() { ui.handleDestinationAdded(evt.(event.DestinationAddedEvent)) }) + case evt := <-addDestinationFailedC: + ui.app.QueueUpdateDraw(func() { + ui.handleDestinationEventError(evt.(event.AddDestinationFailedEvent).Err) + }) case evt := <-destinationRemovedC: ui.app.QueueUpdateDraw(func() { ui.handleDestinationRemoved(evt.(event.DestinationRemovedEvent)) }) + case evt := <-removeDestinationFailedC: + ui.app.QueueUpdateDraw(func() { + ui.handleDestinationEventError(evt.(event.RemoveDestinationFailedEvent).Err) + }) case evt := <-mediaServerStartedC: ui.app.QueueUpdateDraw(func() { ui.handleMediaServerStarted(evt.(event.MediaServerStartedEvent)) @@ -866,24 +876,6 @@ func (ui *UI) Close() { ui.app.Stop() } -func (ui *UI) ConfigUpdateFailed(err error) { - ui.app.QueueUpdateDraw(func() { - ui.showModal( - pageNameConfigUpdateFailed, - "Configuration update failed:\n\n"+err.Error(), - []string{"Ok"}, - false, - func(int, string) { - pageName, frontPage := ui.pages.GetFrontPage() - if pageName != pageNameAddDestination { - ui.logger.Warn("Unexpected page when configuration form closed", "page", pageName) - } - ui.app.SetFocus(frontPage) - }, - ) - }) -} - func (ui *UI) addDestination() { const ( inputLen = 60 @@ -981,6 +973,22 @@ func (ui *UI) handleDestinationRemoved(event.DestinationRemovedEvent) { ui.selectPreviousDestination() } +func (ui *UI) handleDestinationEventError(err error) { + ui.showModal( + pageNameConfigUpdateFailed, + "Configuration update failed:\n\n"+err.Error(), + []string{"Ok"}, + false, + func(int, string) { + pageName, frontPage := ui.pages.GetFrontPage() + if pageName != pageNameAddDestination { + ui.logger.Warn("Unexpected page when configuration form closed", "page", pageName) + } + ui.app.SetFocus(frontPage) + }, + ) +} + func (ui *UI) closeAddDestinationForm() { var hasDestinations bool ui.mu.Lock()