ref: cfbae8c53a382717f982152d079c5cbad46a2275
parent: 280138663dae174763dadd9bf03d385667ef17b3
parent: 130a67e3ea3b577055f9779f90a5d10fb8f1d85e
author: penny64 <penny64@noreply.codeberg.org>
date: Fri Oct 17 20:56:06 EDT 2025
Merge pull request 'Use dispatcher for most (all?) of main.go' (#11) from dispatch_experiments into main Reviewed-on: https://codeberg.org/penny64/hellclient/pulls/11
--- a/dispatch.go
+++ b/dispatch.go
@@ -25,6 +25,24 @@
err error
}
+type AnonJob struct {+ jobfunc func()
+ wg sync.WaitGroup
+}
+
+func (job *AnonJob) Do() {+ job.jobfunc()
+ job.wg.Done()
+}
+
+func (job *AnonJob) Init() {+ job.wg.Add(1)
+}
+
+func (job *AnonJob) Wait() {+ job.wg.Wait()
+}
+
type GenericJob struct {jobfunc func(job *GenericJob)
result string
@@ -149,6 +167,15 @@
func (hc *Hellclient) dispatchFunc(enclosure func(*GenericJob)) Job { noticeJob := &GenericJob{+ jobfunc: enclosure,
+ }
+ noticeJob.Init()
+ hc.jobdispatch <- noticeJob
+ return noticeJob
+}
+
+func (hc *Hellclient) dispatchAnon(enclosure func()) Job {+ noticeJob := &AnonJob{jobfunc: enclosure,
}
noticeJob.Init()
--- a/main.go
+++ b/main.go
@@ -151,6 +151,7 @@
return
}
fmt.Printf("No version information available.")+ return
case "prev":
if hc.page != nil {hc.page.Prev()
@@ -296,7 +297,10 @@
}
hc.PrintObjectProperties(translated)
case "like":
- _, err := client.Favourite(context.Background(), postItem.ID)
+ likefunc := func() {+ _, err = client.Favourite(context.Background(), postItem.ID)
+ }
+ hc.dispatchAnon(likefunc).Wait()
if err != nil {printMastodonErr(err)
} else {@@ -305,7 +309,8 @@
}
return
case "mark":
- _, err := client.Bookmark(context.Background(), postItem.ID)
+ markfunc := func() {_, err = client.Bookmark(context.Background(), postItem.ID)}+ hc.dispatchAnon(markfunc).Wait()
if err != nil {printMastodonErr(err)
} else {@@ -314,12 +319,20 @@
}
return
case "unmark":
- postCopy, err := client.GetStatus(context.Background(), postItem.ID)
- if !postCopy.Bookmarked.(bool) && err != nil {+ var postCopy *mastodon.Status
+ unmarkfunc := func() {+ postCopy, err = client.GetStatus(context.Background(), postItem.ID)
+ }
+ hc.dispatchAnon(unmarkfunc).Wait()
+ if err != nil {+ fmt.Printf("Error removing bookmark: %s\n", err)+ return
+ }
+ if !postCopy.Bookmarked.(bool) { fmt.Printf("Post not bookmarked.\n")return
}
- _, err = client.Unbookmark(context.Background(), postItem.ID)
+ hc.dispatchAnon(func() {_, err = client.Unbookmark(context.Background(), postItem.ID)}).Wait() if err != nil {printMastodonErr(err)
} else {@@ -371,34 +384,46 @@
savePostImages(postItem, hc.preferences.Save_Location)
return
case "rt":
- rtStatus, err := client.Reblog(context.Background(), postItem.ID)
- if err != nil {- fmt.Println(err)
+ rtfunc := func() {+ rtStatus, err := client.Reblog(context.Background(), postItem.ID)
+ if err != nil {+ fmt.Println(err)
+ return
+ }
+ *recentpost = rtStatus
+ hc.printAndIncrement(hc.ctxref, rtStatus)
return
}
- *recentpost = rtStatus
- hc.printAndIncrement(hc.ctxref, rtStatus)
+ hc.dispatchAnon(rtfunc).Wait()
return
case "parent":
- if postItem.InReplyToID == nil {- fmt.Printf("%v doesn't have a parent\n", index)+ parentfunc := func() {+ if postItem.InReplyToID == nil {+ fmt.Printf("%v doesn't have a parent\n", index)+ return
+ }
+ parentStatus, _ := client.GetStatus(context.Background(), mastodon.ID(postItem.InReplyToID.(string)))
+ hc.printAndIncrement(hc.ctxref, parentStatus)
return
}
- parentStatus, _ := client.GetStatus(context.Background(), mastodon.ID(postItem.InReplyToID.(string)))
- hc.printAndIncrement(hc.ctxref, parentStatus)
+ hc.dispatchAnon(parentfunc).Wait()
return
case "children":
- context, err := client.GetStatusContext(context.Background(), postItem.ID)
- if err != nil {- fmt.Println(err)
+ childfunc := func() {+ context, err := client.GetStatusContext(context.Background(), postItem.ID)
+ if err != nil {+ fmt.Println(err)
+ return
+ }
+ if len(context.Descendants) == 0 {+ fmt.Printf("\"%s\" has no children\n", index)+ }
+ for post := range context.Descendants {+ hc.printAndIncrement(hc.ctxref, context.Descendants[post])
+ }
return
}
- if len(context.Descendants) == 0 {- fmt.Printf("\"%s\" has no children\n", index)- }
- for post := range context.Descendants {- hc.printAndIncrement(hc.ctxref, context.Descendants[post])
- }
+ hc.dispatchAnon(childfunc).Wait()
return
case "edit":
if content == "" || content == " " {@@ -426,11 +451,15 @@
id := mastodon.ID(postItem.InReplyToID.(string))
toot.InReplyToID = id
}
- _, err = client.UpdateStatus(context.Background(), toot, postItem.ID)
- if err != nil {- fmt.Println(err)
- return
+ editfunc := func() {+ _, err = client.UpdateStatus(context.Background(), toot, postItem.ID)
+ if err != nil {+ fmt.Println(err)
+ return
+ }
}
+ hc.dispatchAnon(editfunc).Wait()
+ return
case "thread":
hc.pause(true)
hc.page = &Page{disablereverse: true}--
⑨