rename Update to Transact

This commit is contained in:
Balakrishnan Balasubramanian 2022-06-30 12:49:15 -04:00
parent bef160745a
commit 587b7f38ce
2 changed files with 9 additions and 9 deletions

View File

@ -56,7 +56,7 @@ func (d *Db) Add(i Item) (int, bool) {
return i.Id, true return i.Id, true
} }
func (d *Db) Update(id int, persist bool, f func(*Item)) error { func (d *Db) Transact(id int, persist bool, f func(*Item)) error {
d.mutex.Lock() d.mutex.Lock()
defer d.mutex.Unlock() defer d.mutex.Unlock()
for i, _ := range d.items { for i, _ := range d.items {

16
main.go
View File

@ -108,7 +108,7 @@ func main() {
} }
fmt.Println("path is", p, "id is", id) fmt.Println("path is", p, "id is", id)
var sc <-chan string var sc <-chan string
d.Update(id, false, func(i *db.Item) { d.Transact(id, false, func(i *db.Item) {
pt := i.Pt pt := i.Pt
if pt != nil { if pt != nil {
sc = pt.Subscribe() sc = pt.Subscribe()
@ -127,7 +127,7 @@ func main() {
} }
var status db.DownloadStatus var status db.DownloadStatus
var fname string var fname string
d.Update(id, true, func(i *db.Item) { d.Transact(id, false, func(i *db.Item) {
status = i.Status status = i.Status
fname = i.FileName fname = i.FileName
}) })
@ -154,12 +154,12 @@ func main() {
} }
var tc <-chan struct{} var tc <-chan struct{}
d.Update(id, false, func(i *db.Item) { d.Transact(id, false, func(i *db.Item) {
tc = i.TitleChan tc = i.TitleChan
}) })
<-tc <-tc
var title string var title string
d.Update(id, false, func(i *db.Item) { d.Transact(id, false, func(i *db.Item) {
title = i.Title title = i.Title
}) })
w.Write([]byte(title)) w.Write([]byte(title))
@ -171,7 +171,7 @@ func main() {
func getTitle(id int, yturl string) { func getTitle(id int, yturl string) {
tc := make(chan struct{}) tc := make(chan struct{})
defer close(tc) defer close(tc)
d.Update(id, false, func(i *db.Item) { d.Transact(id, false, func(i *db.Item) {
i.TitleChan = tc i.TitleChan = tc
}) })
args := append(ytdlCmd, "--get-title", yturl) args := append(ytdlCmd, "--get-title", yturl)
@ -183,14 +183,14 @@ func getTitle(id int, yturl string) {
} else { } else {
title = string(op) title = string(op)
} }
d.Update(id, true, func(i *db.Item) { d.Transact(id, true, func(i *db.Item) {
i.Title = title i.Title = title
}) })
} }
func download(id int, yturl string) { func download(id int, yturl string) {
pt := pubsub.NewProgressTracker() pt := pubsub.NewProgressTracker()
d.Update(id, true, func(i *db.Item) { d.Transact(id, true, func(i *db.Item) {
i.Status = db.InProgress i.Status = db.InProgress
i.Pt = pt i.Pt = pt
}) })
@ -220,7 +220,7 @@ func download(id int, yturl string) {
} else { } else {
status = db.Done status = db.Done
} }
d.Update(id, true, func(i *db.Item) { d.Transact(id, true, func(i *db.Item) {
i.Status = status i.Status = status
i.FileName = fname i.FileName = fname
}) })