Skip to content
This repository has been archived by the owner on Feb 10, 2023. It is now read-only.

Commit

Permalink
de-duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
SoMuchForSubtlety committed Mar 14, 2022
1 parent 165ae7c commit 358c7b1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 57 deletions.
30 changes: 1 addition & 29 deletions internal/ui/live.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,35 +47,7 @@ func (s *UIState) checkLive() {
}

func (s *UIState) runLiveHook(hook cmd.MultiCommand, mainStream f1tv.ContentContainer, perspectives []f1tv.AdditionalStream, meta cmd.MetaData) {
var commands []cmd.CommandContext
for _, target := range hook.Targets {
mainFeed, perspective, err := findPerspectiveByName(target.MatchTitle, perspectives, mainStream)
if err != nil {
continue
}

var urlFunc func() (string, error)
if mainFeed != nil {
urlFunc = func() (string, error) { return s.v2.GetPlaybackURL(f1tv.BIG_SCREEN_HLS, mainStream.Metadata.ContentID) }
} else {
urlFunc = func() (string, error) {
return s.v2.GetPerspectivePlaybackURL(f1tv.BIG_SCREEN_HLS, perspective.PlaybackURL)
}
}
targetCmd := s.cmd.GetCommand(target)
if len(targetCmd.Command) == 0 {
s.logger.Errorf("could not determine command for %q - %q", hook.Title, target.MatchTitle)
continue
}

// If we have a match, run the given command!
context := cmd.CommandContext{
MetaData: cmd.MetaData{PerspectiveTitle: hook.Title},
CustomOptions: targetCmd,
URL: urlFunc,
}
commands = append(commands, context)
}
commands := s.extractCommands(hook, perspectives, mainStream)
// If no streams are matched, continue
if len(commands) == 0 {
return
Expand Down
62 changes: 34 additions & 28 deletions internal/ui/nodeV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,35 +133,8 @@ func (s *UIState) v2MultiCommandNodes(perspectives []f1tv.AdditionalStream, main

for _, multi := range s.cmd.MultiCommads {
s.logger.Infof("checking %q", multi.Title)
var commands []cmd.CommandContext
for _, target := range multi.Targets {
mainFeed, perspective, err := findPerspectiveByName(target.MatchTitle, perspectives, mainStream)
if err != nil {
continue
}

var urlFunc func() (string, error)
if mainFeed != nil {
urlFunc = func() (string, error) { return s.v2.GetPlaybackURL(f1tv.BIG_SCREEN_HLS, mainStream.Metadata.ContentID) }
} else {
urlFunc = func() (string, error) {
return s.v2.GetPerspectivePlaybackURL(f1tv.BIG_SCREEN_HLS, perspective.PlaybackURL)
}
}
targetCmd := s.cmd.GetCommand(target)
if len(targetCmd.Command) == 0 {
s.logger.Errorf("could not determine command for %q - %q", multi.Title, target.MatchTitle)
continue
}
commands := s.extractCommands(multi, perspectives, mainStream)

// If we have a match, run the given command!
context := cmd.CommandContext{
MetaData: cmd.MetaData{PerspectiveTitle: multi.Title},
CustomOptions: targetCmd,
URL: urlFunc,
}
commands = append(commands, context)
}
// If no streams are matched, continue
if len(commands) == 0 {
continue
Expand All @@ -185,6 +158,39 @@ func (s *UIState) v2MultiCommandNodes(perspectives []f1tv.AdditionalStream, main
return nodes
}

func (s *UIState) extractCommands(multi cmd.MultiCommand, perspectives []f1tv.AdditionalStream, mainStream f1tv.ContentContainer) []cmd.CommandContext {
var commands []cmd.CommandContext
for _, target := range multi.Targets {
mainFeed, perspective, err := findPerspectiveByName(target.MatchTitle, perspectives, mainStream)
if err != nil {
continue
}

var urlFunc func() (string, error)
if mainFeed != nil {
urlFunc = func() (string, error) { return s.v2.GetPlaybackURL(f1tv.BIG_SCREEN_HLS, mainStream.Metadata.ContentID) }
} else {
urlFunc = func() (string, error) {
return s.v2.GetPerspectivePlaybackURL(f1tv.BIG_SCREEN_HLS, perspective.PlaybackURL)
}
}
targetCmd := s.cmd.GetCommand(target)
if len(targetCmd.Command) == 0 {
s.logger.Errorf("could not determine command for %q - %q", multi.Title, target.MatchTitle)
continue
}

// If we have a match, run the given command!
context := cmd.CommandContext{
MetaData: cmd.MetaData{PerspectiveTitle: multi.Title},
CustomOptions: targetCmd,
URL: urlFunc,
}
commands = append(commands, context)
}
return commands
}

func findPerspectiveByName(name string, perspectives []f1tv.AdditionalStream, mainStream f1tv.ContentContainer) (*f1tv.ContentContainer, *f1tv.AdditionalStream, error) {
notFound := fmt.Errorf("found no perspective matching '%s'", name)
for _, perspective := range perspectives {
Expand Down

0 comments on commit 358c7b1

Please sign in to comment.