]> git.lizzy.rs Git - go-anidb.git/blobdiff - misc/episodelist.go
misc: Make the iterator part of EpisodeContainer interface
[go-anidb.git] / misc / episodelist.go
index 61be68bf0257717d16854bf415440ea053c5c452..10747a30e1f90a4503b37345132b13495dac0b49 100644 (file)
@@ -87,6 +87,46 @@ func (el EpisodeList) FormatLog(ec EpisodeCount) string {
        return strings.Join(parts, ",")
 }
 
+func (el EpisodeList) Infinite() bool {
+       for i := range el {
+               if el[i].Infinite() {
+                       return true
+               }
+       }
+       return false
+}
+
+// Returns a channel that can be used to iterate using for/range.
+//
+// If the EpisodeList is infinite, then the channel is also infinite.
+// The caller is allowed to close the channel in such case.
+//
+// NOTE: Not thread safe.
+func (el EpisodeList) Episodes() chan Episode {
+       ch := make(chan Episode, 1)
+
+       go func() {
+               abort := false
+
+               if el.Infinite() {
+                       defer func() { recover(); abort = true }()
+               } else {
+                       defer close(ch)
+               }
+
+               for _, er := range el {
+                       for ep := range er.Episodes() {
+                               ch <- ep
+
+                               if abort {
+                                       return
+                               }
+                       }
+               }
+       }()
+       return ch
+}
+
 // Returns true if any of the contained EpisodeRanges contain the
 // given EpisodeContainer.
 func (el EpisodeList) ContainsEpisodes(ec EpisodeContainer) bool {