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

Commit 358c7b1

Browse files
de-duplicate code
1 parent 165ae7c commit 358c7b1

File tree

2 files changed

+35
-57
lines changed

2 files changed

+35
-57
lines changed

internal/ui/live.go

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -47,35 +47,7 @@ func (s *UIState) checkLive() {
4747
}
4848

4949
func (s *UIState) runLiveHook(hook cmd.MultiCommand, mainStream f1tv.ContentContainer, perspectives []f1tv.AdditionalStream, meta cmd.MetaData) {
50-
var commands []cmd.CommandContext
51-
for _, target := range hook.Targets {
52-
mainFeed, perspective, err := findPerspectiveByName(target.MatchTitle, perspectives, mainStream)
53-
if err != nil {
54-
continue
55-
}
56-
57-
var urlFunc func() (string, error)
58-
if mainFeed != nil {
59-
urlFunc = func() (string, error) { return s.v2.GetPlaybackURL(f1tv.BIG_SCREEN_HLS, mainStream.Metadata.ContentID) }
60-
} else {
61-
urlFunc = func() (string, error) {
62-
return s.v2.GetPerspectivePlaybackURL(f1tv.BIG_SCREEN_HLS, perspective.PlaybackURL)
63-
}
64-
}
65-
targetCmd := s.cmd.GetCommand(target)
66-
if len(targetCmd.Command) == 0 {
67-
s.logger.Errorf("could not determine command for %q - %q", hook.Title, target.MatchTitle)
68-
continue
69-
}
70-
71-
// If we have a match, run the given command!
72-
context := cmd.CommandContext{
73-
MetaData: cmd.MetaData{PerspectiveTitle: hook.Title},
74-
CustomOptions: targetCmd,
75-
URL: urlFunc,
76-
}
77-
commands = append(commands, context)
78-
}
50+
commands := s.extractCommands(hook, perspectives, mainStream)
7951
// If no streams are matched, continue
8052
if len(commands) == 0 {
8153
return

internal/ui/nodeV2.go

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -133,35 +133,8 @@ func (s *UIState) v2MultiCommandNodes(perspectives []f1tv.AdditionalStream, main
133133

134134
for _, multi := range s.cmd.MultiCommads {
135135
s.logger.Infof("checking %q", multi.Title)
136-
var commands []cmd.CommandContext
137-
for _, target := range multi.Targets {
138-
mainFeed, perspective, err := findPerspectiveByName(target.MatchTitle, perspectives, mainStream)
139-
if err != nil {
140-
continue
141-
}
142-
143-
var urlFunc func() (string, error)
144-
if mainFeed != nil {
145-
urlFunc = func() (string, error) { return s.v2.GetPlaybackURL(f1tv.BIG_SCREEN_HLS, mainStream.Metadata.ContentID) }
146-
} else {
147-
urlFunc = func() (string, error) {
148-
return s.v2.GetPerspectivePlaybackURL(f1tv.BIG_SCREEN_HLS, perspective.PlaybackURL)
149-
}
150-
}
151-
targetCmd := s.cmd.GetCommand(target)
152-
if len(targetCmd.Command) == 0 {
153-
s.logger.Errorf("could not determine command for %q - %q", multi.Title, target.MatchTitle)
154-
continue
155-
}
136+
commands := s.extractCommands(multi, perspectives, mainStream)
156137

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

161+
func (s *UIState) extractCommands(multi cmd.MultiCommand, perspectives []f1tv.AdditionalStream, mainStream f1tv.ContentContainer) []cmd.CommandContext {
162+
var commands []cmd.CommandContext
163+
for _, target := range multi.Targets {
164+
mainFeed, perspective, err := findPerspectiveByName(target.MatchTitle, perspectives, mainStream)
165+
if err != nil {
166+
continue
167+
}
168+
169+
var urlFunc func() (string, error)
170+
if mainFeed != nil {
171+
urlFunc = func() (string, error) { return s.v2.GetPlaybackURL(f1tv.BIG_SCREEN_HLS, mainStream.Metadata.ContentID) }
172+
} else {
173+
urlFunc = func() (string, error) {
174+
return s.v2.GetPerspectivePlaybackURL(f1tv.BIG_SCREEN_HLS, perspective.PlaybackURL)
175+
}
176+
}
177+
targetCmd := s.cmd.GetCommand(target)
178+
if len(targetCmd.Command) == 0 {
179+
s.logger.Errorf("could not determine command for %q - %q", multi.Title, target.MatchTitle)
180+
continue
181+
}
182+
183+
// If we have a match, run the given command!
184+
context := cmd.CommandContext{
185+
MetaData: cmd.MetaData{PerspectiveTitle: multi.Title},
186+
CustomOptions: targetCmd,
187+
URL: urlFunc,
188+
}
189+
commands = append(commands, context)
190+
}
191+
return commands
192+
}
193+
188194
func findPerspectiveByName(name string, perspectives []f1tv.AdditionalStream, mainStream f1tv.ContentContainer) (*f1tv.ContentContainer, *f1tv.AdditionalStream, error) {
189195
notFound := fmt.Errorf("found no perspective matching '%s'", name)
190196
for _, perspective := range perspectives {

0 commit comments

Comments
 (0)