-
Notifications
You must be signed in to change notification settings - Fork 2
/
progress.go
201 lines (162 loc) · 4.23 KB
/
progress.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package geektime
import (
"errors"
"fmt"
"sync"
"sync/atomic"
)
type progress struct {
name string
total, current int32
err error
}
func (p *progress) String() string {
return fmt.Sprintf(
"progress:[name:%s,total:%d,current:%d,err:%v]", p.name, p.total, p.current, p.err,
)
}
func (p *progress) advance(d int32) {
atomic.AddInt32(&p.current, d)
}
func (p *progress) abort(err error) {
p.err = err
}
func (p *progress) isEnd() bool {
return p.err != nil || atomic.LoadInt32(&p.current) >= atomic.LoadInt32(&p.total)
}
type courseProgress struct {
bus *bus
articleProgresses sync.Map // artice id -> *progress
progress progress
articles sync.Map // article id -> articleVideo
}
func (cp *courseProgress) isEnd() bool {
ok := true
cp.articleProgresses.Range(func(_, v interface{}) bool {
if !v.(*progress).isEnd() {
ok = false
return false
}
return true
})
return ok
}
func (cp *courseProgress) advance(d int32) {
cp.progress.advance(d)
}
func (cp *courseProgress) abort(err error) {
cp.progress.abort(err)
}
func (cp *courseProgress) subscribeEvents() {
b := cp.bus
b.subscribe(eventCourse, func(v interface{}) {
c := v.(courseRet)
if c.err != nil {
b.post(eventUIProgressTotal, []*progress{&progress{name: c.course.Title, total: 1}})
cp.abort(wrapErr("fetch course", c.err))
b.post(eventUIUpdateProgress, &cp.progress)
return
}
cp.progress = progress{name: c.course.Title, total: int32(c.course.ArticleCount)}
})
b.subscribe(eventArticles, func(v interface{}) {
as := v.(articles)
if as.err != nil {
b.post(eventUIProgressTotal, []*progress{&cp.progress})
cp.abort(wrapErr("fetch articles", as.err))
b.post(eventUIUpdateProgress, &cp.progress)
return
}
ps := make([]*progress, len(as.articles))
for i, a := range as.articles {
p := &progress{name: a.Title, total: 1}
cp.articleProgresses.Store(a.ID, p)
ps[i] = p
}
b.post(eventUIProgressTotal, ps)
})
b.subscribe(eventArticleFinished, func(v interface{}) {
articleID := v.(int)
v, ok := cp.articleProgresses.Load(v)
if !ok {
panic(fmt.Sprintf("[BUG] article progress not exist:%d", articleID))
}
p := v.(*progress)
p.advance(1)
b.post(eventUIUpdateProgress, p)
cp.advance(1)
})
b.subscribe(eventArticleVideo, func(v interface{}) {
av := v.(articleVideo)
if av.err == nil {
cp.articles.LoadOrStore(av.articleID, av)
return
}
v, ok := cp.articleProgresses.Load(av.articleID)
if !ok {
panic(fmt.Sprintf("[BUG] article progress not exist:%d", av.articleID))
}
p := v.(*progress)
p.abort(wrapErr("fetch article video", av.err))
b.post(eventUIUpdateProgress, p)
cp.advance(1)
})
b.subscribe(eventPlayAuth, func(v interface{}) {
pa := v.(playAuth)
if pa.err == nil {
return
}
v, ok := cp.articleProgresses.Load(pa.articleID)
if !ok {
panic(fmt.Sprintf("[BUG] article progress not exist:%d", pa.articleID))
}
p := v.(*progress)
p.abort(wrapErr("play auth", pa.err))
b.post(eventUIUpdateProgress, p)
cp.advance(1)
})
b.subscribe(eventDownloadTS, func(v interface{}) {
articleID := v.(downloadTS).articleID
v, ok := cp.articleProgresses.Load(articleID)
if !ok {
panic(fmt.Sprintf("[BUG] article progress not exist:%d", articleID))
}
p := v.(*progress)
p.advance(1)
b.post(eventUIUpdateProgress, p)
if p.isEnd() {
cp.advance(1)
}
})
b.subscribe(eventM3U8Parsed, func(v interface{}) {
m := v.(m3u8)
v, ok := cp.articleProgresses.Load(m.articleID)
if !ok {
panic(fmt.Sprintf("[BUG] article progress not exist:%s", m))
}
p := v.(*progress)
if m.err == nil {
p.name, p.total = m.name, int32(len(m.ts))
} else {
p.abort(fmt.Errorf("m3u8 parsed error:%s", m.err))
cp.advance(1)
}
b.post(eventUIUpdateProgress, p)
})
b.subscribe(eventCreateArticleFoldFailed, func(v interface{}) {
v, ok := cp.articleProgresses.Load(v)
if !ok {
panic(fmt.Sprintf("[BUG] article progress not exist:%s", v))
}
p := v.(*progress)
p.abort(errors.New("create article fold failed"))
b.post(eventUIUpdateProgress, p)
cp.advance(1)
})
}
func newProgress(bus *bus) *courseProgress {
return &courseProgress{bus: bus}
}
func wrapErr(msg string, err error) error {
return fmt.Errorf(msg+" error:%s", err)
}