hopefully channel working

This commit is contained in:
2022-06-28 15:26:16 -04:00
parent 882f8aea2f
commit e2dc04c233
3 changed files with 81 additions and 61 deletions

View File

@ -6,6 +6,8 @@ import (
"fmt"
"os"
"sync"
"gitlab.com/balki/ytui/pubsub"
)
type DownloadStatus string
@ -18,14 +20,13 @@ var (
)
type Item struct {
Id int `json:"id"`
Date string `json:"date"`
URL string `json:"url"`
Title string `json:"title"`
Approved bool `json:"approved"`
Status DownloadStatus `json:"status"`
FileName string `json:"file_name"`
Progress string `json:"-"`
Id int `json:"id"`
Date string `json:"date"`
URL string `json:"url"`
Title string `json:"title"`
Status DownloadStatus `json:"status"`
FileName string `json:"file_name"`
Pt pubsub.ProgressTracker `json:"-"`
}
type Jdb struct {
@ -37,16 +38,21 @@ type Db struct {
mutex sync.Mutex
lastId int
path string
index map[string]int
}
func (d *Db) Add(i Item) int {
func (d *Db) Add(i Item) (int, bool) {
d.mutex.Lock()
defer d.mutex.Unlock()
if id, ok := d.index[i.URL]; ok {
return id, false
}
i.Id = d.lastId
d.lastId++
d.items = append(d.items, i)
d.save()
return i.Id
d.index[i.URL] = i.Id
return i.Id, true
}
func (d *Db) Update(id int, persist bool, f func(*Item)) error {
@ -101,14 +107,17 @@ func Load(path string) (*Db, error) {
return nil, err
}
m := 0
indexMap := map[string]int{}
for _, item := range jd.Items {
if item.Id > m {
m = item.Id
}
indexMap[item.URL] = item.Id
}
return &Db{
items: jd.Items,
path: path,
lastId: m + 1,
index: indexMap,
}, nil
}