/
postview.go
169 lines (137 loc) · 3.53 KB
/
postview.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package postview
import (
"context"
"time"
_ "embed"
"github.com/diamondburned/gotk4-examples/gtk4/hackernews/internal/components/frontpage"
"github.com/diamondburned/gotk4-examples/gtk4/hackernews/internal/gtkutil"
"github.com/diamondburned/gotk4-examples/gtk4/hackernews/internal/hackernews"
"github.com/diamondburned/gotk4/pkg/core/glib"
"github.com/diamondburned/gotk4/pkg/gtk/v4"
"golang.org/x/sync/semaphore"
)
//go:embed postview.css
var css string
func init() { gtkutil.AddCSS(css) }
// CommentAppender describes any widget that we can add comments to. It is
// implemented by View and Comment.
type CommentAppender interface {
Append(*Comment)
}
var (
_ CommentAppender = (*Comment)(nil)
_ CommentAppender = (*View)(nil)
)
// View describes a view of a single post with all its comments.
type View struct {
*gtk.ScrolledWindow
item *hackernews.Item
box *gtk.Box
post *frontpage.Post
}
// NewView creates a new View.
func NewView(item *hackernews.Item) *View {
v := View{item: item}
v.post = frontpage.NewPost(item.ID)
v.post.SetItem(item)
v.box = gtk.NewBox(gtk.OrientationVertical, 0)
v.box.AddCSSClass("postview-post")
v.box.Append(v.post)
v.ScrolledWindow = gtk.NewScrolledWindow()
v.ScrolledWindow.SetPolicy(gtk.PolicyNever, gtk.PolicyAutomatic)
v.ScrolledWindow.SetChild(v.box)
// Make a context that's cancelled when the view is swapped out with
// something else.
ctx, cancel := context.WithCancel(context.Background())
v.ConnectUnmap(cancel)
fetcher := commentsFetcher{
ctx: ctx,
sema: semaphore.NewWeighted(8),
}
go fetcher.fetch(&v, item.Kids)
return &v
}
type commentsFetcher struct {
ctx context.Context
sema *semaphore.Weighted
}
func (f *commentsFetcher) fetch(parent CommentAppender, ids []hackernews.ItemID) {
for _, id := range ids {
select {
case <-f.ctx.Done():
return
default:
}
f.fetchOne(parent, id)
}
}
func (f *commentsFetcher) fetchOne(parent CommentAppender, id hackernews.ItemID) {
if err := f.sema.Acquire(f.ctx, 1); err != nil {
return
}
defer f.sema.Release(1)
item, err := hackernews.DefaultClient.Item(f.ctx, id)
if f.ctx.Err() != nil {
// Context expired; don't do anything.
return
}
if item != nil && item.Text == "" {
// The comment's content is empty for some reason. Ignore it.
return
}
glib.IdleAdd(func() {
comment := NewComment()
parent.Append(comment)
if err != nil {
comment.SetError(err)
return
}
comment.SetItem(item)
if len(item.Kids) > 0 {
go f.fetch(comment, item.Kids)
}
})
// Add a bit of delay. The user doesn't read this fast, usually.
sleep(f.ctx, 250*time.Millisecond)
}
func sleep(ctx context.Context, d time.Duration) {
timer := time.NewTimer(d)
select {
case <-timer.C:
case <-ctx.Done():
}
}
// type commentFetchJob struct {
// parent CommentAppender
// kids []hackernews.ItemID
// }
// func (v *View) fetch(ctx context.Context, ch chan commentFetchJob) {
// var jobs []commentFetchJob
// var job commentFetchJob
// for {
// if len(jobs) > 0 {
// select {
// case <-ctx.Done():
// return
// case ch <- jobs[0]:
// jobs = append(jobs[:0], jobs[1:]...) // pop first
// case job = <-ch:
// }
// } else {
// select {
// case <-ctx.Done():
// return
// case job = <-ch:
// }
// }
// for _, id := range job.kids {
// item, err := hackernews.DefaultClient.Item(ctx, id)
// }
// }
// }
// func fetchCommentsCh(ch <-chan commentFetchJob) {
// }
// Append adds a comment to the toplevel.
func (v *View) Append(comment *Comment) {
v.box.Append(comment)
}