Allow detection of waiting_file_selection
This commit is contained in:
@@ -7,23 +7,107 @@ import (
|
||||
)
|
||||
|
||||
type LibraryState struct {
|
||||
TotalCount int
|
||||
FirstTorrent *realdebrid.Torrent
|
||||
DownloadingCount int
|
||||
TotalCount int
|
||||
ActiveCount int
|
||||
FirstTorrent realdebrid.Torrent
|
||||
FirstActiveTorrent realdebrid.Torrent
|
||||
}
|
||||
|
||||
func (ls LibraryState) equal(a LibraryState) bool {
|
||||
return a.TotalCount == ls.TotalCount && a.FirstTorrent.ID == ls.FirstTorrent.ID && a.DownloadingCount == ls.DownloadingCount
|
||||
return a.TotalCount == ls.TotalCount &&
|
||||
a.ActiveCount == ls.ActiveCount &&
|
||||
a.FirstTorrent.ID == ls.FirstTorrent.ID &&
|
||||
a.FirstTorrent.Status == ls.FirstTorrent.Status &&
|
||||
a.FirstActiveTorrent.ID == ls.FirstActiveTorrent.ID &&
|
||||
a.FirstActiveTorrent.Status == ls.FirstActiveTorrent.Status
|
||||
}
|
||||
|
||||
func EmptyState() LibraryState {
|
||||
oldestTime := time.Time{}
|
||||
empty := realdebrid.Torrent{
|
||||
ID: "",
|
||||
Added: (time.Time{}).Format(time.RFC3339),
|
||||
}
|
||||
return LibraryState{
|
||||
TotalCount: 0,
|
||||
FirstTorrent: &realdebrid.Torrent{
|
||||
ID: "",
|
||||
Added: oldestTime.Format(time.RFC3339),
|
||||
},
|
||||
DownloadingCount: 0,
|
||||
TotalCount: 0,
|
||||
ActiveCount: 0,
|
||||
FirstTorrent: empty,
|
||||
FirstActiveTorrent: empty,
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TorrentManager) SetNewLatestState(checksum LibraryState) {
|
||||
t.latestState.ActiveCount = checksum.ActiveCount
|
||||
t.latestState.TotalCount = checksum.TotalCount
|
||||
t.latestState.FirstTorrent = checksum.FirstTorrent
|
||||
t.latestState.FirstActiveTorrent = checksum.FirstActiveTorrent
|
||||
}
|
||||
|
||||
type torrentsResp struct {
|
||||
torrents []realdebrid.Torrent
|
||||
totalCount int
|
||||
}
|
||||
|
||||
// generates a checksum based on the number of torrents, the first torrent id and the number of active torrents
|
||||
func (t *TorrentManager) getCurrentState() LibraryState {
|
||||
torrentChan := make(chan torrentsResp, 1)
|
||||
activeChan := make(chan torrentsResp, 1)
|
||||
countChan := make(chan int, 1)
|
||||
errChan := make(chan error, 3) // accommodate errors from both goroutines
|
||||
defer close(torrentChan)
|
||||
defer close(activeChan)
|
||||
defer close(countChan)
|
||||
defer close(errChan)
|
||||
|
||||
_ = t.workerPool.Submit(func() {
|
||||
torrents, totalCount, err := t.Api.GetTorrents(1, false)
|
||||
if err != nil {
|
||||
errChan <- err
|
||||
return
|
||||
}
|
||||
torrentChan <- torrentsResp{torrents: torrents, totalCount: totalCount}
|
||||
})
|
||||
|
||||
_ = t.workerPool.Submit(func() {
|
||||
torrents, totalCount, err := t.Api.GetTorrents(1, true)
|
||||
if err != nil {
|
||||
errChan <- err
|
||||
return
|
||||
}
|
||||
activeChan <- torrentsResp{torrents: torrents, totalCount: totalCount}
|
||||
})
|
||||
|
||||
_ = t.workerPool.Submit(func() {
|
||||
count, err := t.Api.GetActiveTorrentCount()
|
||||
if err != nil {
|
||||
errChan <- err
|
||||
return
|
||||
}
|
||||
countChan <- count.DownloadingCount
|
||||
})
|
||||
|
||||
// Existing goroutines for GetTorrents and GetActiveTorrentCount
|
||||
var first realdebrid.Torrent
|
||||
var active realdebrid.Torrent
|
||||
var totalCount, count int
|
||||
|
||||
for i := 0; i < 3; i++ {
|
||||
select {
|
||||
case firstResp := <-torrentChan:
|
||||
first = firstResp.torrents[0]
|
||||
totalCount = firstResp.totalCount
|
||||
case activeResp := <-activeChan:
|
||||
active = activeResp.torrents[0]
|
||||
case count = <-countChan:
|
||||
case err := <-errChan:
|
||||
t.log.Warnf("Checksum API Error: %v\n", err)
|
||||
return EmptyState()
|
||||
}
|
||||
}
|
||||
|
||||
return LibraryState{
|
||||
TotalCount: totalCount,
|
||||
ActiveCount: count,
|
||||
FirstTorrent: first,
|
||||
FirstActiveTorrent: active,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user