]> git.lizzy.rs Git - go-anidb.git/blobdiff - episodecache.go
misc: Support partial episodes
[go-anidb.git] / episodecache.go
index 67d12b0fefaab471f407d2cbca53313987a2a447..cd2e4aa0c6d4a17e6d48677cb9249e2c6155e68b 100644 (file)
@@ -48,6 +48,12 @@ func (adb *AniDB) EpisodeByID(eid EID) <-chan *Episode {
        keys := []cacheKey{"eid", eid}
        ch := make(chan *Episode, 1)
 
+       if eid < 1 {
+               ch <- nil
+               close(ch)
+               return ch
+       }
+
        ic := make(chan Cacheable, 1)
        go func() { ch <- (<-ic).(*Episode); close(ch) }()
        if intentMap.Intent(ic, keys...) {
@@ -55,13 +61,13 @@ func (adb *AniDB) EpisodeByID(eid EID) <-chan *Episode {
        }
 
        if !cache.CheckValid(keys...) {
-               intentMap.Notify((*Episode)(nil), keys...)
+               intentMap.NotifyClose((*Episode)(nil), keys...)
                return ch
        }
 
        e := eid.Episode()
        if !e.IsStale() {
-               intentMap.Notify(e, keys...)
+               intentMap.NotifyClose(e, keys...)
                return ch
        }
 
@@ -102,35 +108,19 @@ func (adb *AniDB) EpisodeByID(eid EID) <-chan *Episode {
                                }
                                udpDone = true
                        }
-                       a := <-adb.AnimeByID(AID(aid)) // this caches episodes...
-                       ep := eid.Episode()            // ...so this is now a cache hit
+                       a := <-adb.AnimeByID(AID(aid)) // updates the episode cache as well
+                       ep := a.EpisodeByEID(eid)
 
-                       if !ep.IsStale() {
+                       if ep != nil {
                                e = ep
                                break
                        } else {
-                               // check to see if we looked in the right AID
-                               found := false
-                               if a != nil {
-                                       for _, ep := range a.Episodes {
-                                               if eid == ep.EID {
-                                                       found = true
-                                                       break
-                                               }
-                                       }
-                               }
-
-                               // if found, then it's just that the anime is also stale (offline?)
-                               if found {
-                                       break
-                               } else {
-                                       // otherwise, the EID<->AID map broke
-                                       ok = false
-                                       cache.Delete("aid", "by-eid", eid)
-                               }
+                               // the EID<->AID map broke
+                               ok = false
+                               cache.Delete("aid", "by-eid", eid)
                        }
                }
-               intentMap.Notify(e, keys...)
+               intentMap.NotifyClose(e, keys...)
        }()
        return ch
 }