-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplaylist-updates.go
148 lines (118 loc) · 2.6 KB
/
playlist-updates.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
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"log"
"net/http"
"sync/atomic"
"time"
"go.tmthrgd.dev/spork/dbus"
"go.tmthrgd.dev/spork/web"
)
type updateJSON struct {
Position uint32 `json:"pos"`
Title string `json:"title,omitempty"`
Length int32 `json:"length,omitempty"`
}
func (u *updateJSON) fill(ctx context.Context) error {
var (
data updateJSON
err error
)
if data.Position, err = dbus.GetPlaylistPosition(ctx); err != nil {
return err
}
if data.Title, err = dbus.GetSongTitle(ctx, data.Position); err != nil {
return err
}
if data.Length, err = dbus.GetSongLength(ctx, data.Position); err != nil {
return err
}
*u = data
return nil
}
func (u *updateJSON) marshal() []byte {
data, err := json.Marshal(u)
if err != nil {
panic("spork: internal error: unable to marshal *updateJSON: " + err.Error())
}
return data
}
func playlistUpdateHandler(ctx context.Context) http.HandlerFunc {
type updateData struct {
pos uint32
data []byte
nextCh chan struct{}
next *updateData
}
var update atomic.Value // *updateData
go func() {
var data updateJSON
if err := data.fill(ctx); err != nil && !dbus.IsUnknownServiceError(err) {
log.Printf("spork: playlist update poller error: %v", err)
}
old := &updateData{
pos: data.Position,
data: data.marshal(),
nextCh: make(chan struct{}),
}
update.Store(old)
t := time.NewTicker(time.Second)
defer t.Stop()
for {
select {
case <-t.C:
case <-ctx.Done():
return
}
if err := data.fill(ctx); err != nil {
if !dbus.IsUnknownServiceError(err) {
log.Printf("spork: playlist update poller error: %v", err)
}
continue
}
nextData := data.marshal()
if bytes.Equal(old.data, nextData) {
continue
}
next := &updateData{
pos: data.Position,
data: nextData,
nextCh: make(chan struct{}),
}
update.Store(next)
old.next = next
close(old.nextCh)
old = next
}
}()
return web.ErrorHandler(func(w http.ResponseWriter, r *http.Request) error {
f, ok := w.(http.Flusher)
if !ok {
return errors.New("spork: http.ResponseWriter does not implement http.Flusher")
}
hdr := w.Header()
hdr.Set("Content-Type", "text/event-stream")
hdr.Set("Connection", "keep-alive")
w.WriteHeader(http.StatusOK)
f.Flush()
update := update.Load().(*updateData)
for {
select {
case <-update.nextCh:
update = update.next
case <-r.Context().Done():
return nil
case <-shutdown:
return nil
}
io.WriteString(w, "data: ")
w.Write(update.data)
io.WriteString(w, "\n\n")
f.Flush()
}
})
}