@@ -223,54 +223,48 @@ type sseSection struct {
223223 fetchFn func (h * Handler ) (string , any , error )
224224}
225225
226- var eventToSection = map [string ]* sseSection {
227- events .TypePlayerUpdated : {name : "section-mpris" },
228- events .TypePlayerAdded : {name : "section-mpris" },
229- events .TypePlayerRemoved : {name : "section-mpris" },
230- events .TypePlayerPosition : {name : "section-mpris" },
231-
232- events .TypeAudioUpdated : {name : "section-audio" },
233- events .TypeAudioRemoved : {name : "section-audio" },
234- events .TypeAudioOutputUpdated : {name : "section-audio" },
235- events .TypeAudioOutputRemoved : {name : "section-audio" },
236-
237- events .TypeServiceUpdated : {name : "section-systemd" },
238- events .TypeBluetoothUpdated : {name : "section-bluetooth" },
226+ func fetchMPRIS (h * Handler ) (string , any , error ) {
227+ players , err := h .client .GetPlayers ()
228+ return "section-mpris" , players , err
239229}
240230
241- func init () {
242- mpris := eventToSection [events .TypePlayerUpdated ]
243- mpris .fetchFn = func (h * Handler ) (string , any , error ) {
244- players , err := h .client .GetPlayers ()
245- return "section-mpris" , players , err
246- }
247- // Share the same sseSection pointer for all mpris events
248- eventToSection [events .TypePlayerAdded ] = mpris
249- eventToSection [events .TypePlayerRemoved ] = mpris
250- eventToSection [events .TypePlayerPosition ] = mpris
251-
252- audio := eventToSection [events .TypeAudioUpdated ]
253- audio .fetchFn = func (h * Handler ) (string , any , error ) {
254- data , err := h .client .GetAudio ()
255- return "section-pulseaudio" , data , err
256- }
257- eventToSection [events .TypeAudioRemoved ] = audio
258- eventToSection [events .TypeAudioOutputUpdated ] = audio
259- eventToSection [events .TypeAudioOutputRemoved ] = audio
260-
261- systemd := eventToSection [events .TypeServiceUpdated ]
262- systemd .fetchFn = func (h * Handler ) (string , any , error ) {
263- services , err := h .client .GetServices ()
264- return "section-systemd" , services , err
265- }
231+ func fetchAudio (h * Handler ) (string , any , error ) {
232+ data , err := h .client .GetAudio ()
233+ return "section-pulseaudio" , data , err
234+ }
266235
267- bt := eventToSection [events .TypeBluetoothUpdated ]
268- bt .fetchFn = func (h * Handler ) (string , any , error ) {
269- status , err := h .client .GetBluetoothStatus ()
270- return "section-bluetooth" , status , err
271- }
236+ func fetchSystemd (h * Handler ) (string , any , error ) {
237+ services , err := h .client .GetServices ()
238+ return "section-systemd" , services , err
239+ }
240+
241+ func fetchBluetooth (h * Handler ) (string , any , error ) {
242+ status , err := h .client .GetBluetoothStatus ()
243+ return "section-bluetooth" , status , err
272244}
273245
246+ var (
247+ mprisSection = & sseSection {name : "section-mpris" , fetchFn : fetchMPRIS }
248+ audioSection = & sseSection {name : "section-audio" , fetchFn : fetchAudio }
249+ systemdSection = & sseSection {name : "section-systemd" , fetchFn : fetchSystemd }
250+ bluetoothSection = & sseSection {name : "section-bluetooth" , fetchFn : fetchBluetooth }
251+
252+ eventToSection = map [string ]* sseSection {
253+ events .TypePlayerUpdated : mprisSection ,
254+ events .TypePlayerAdded : mprisSection ,
255+ events .TypePlayerRemoved : mprisSection ,
256+ events .TypePlayerPosition : mprisSection ,
257+
258+ events .TypeAudioUpdated : audioSection ,
259+ events .TypeAudioRemoved : audioSection ,
260+ events .TypeAudioOutputUpdated : audioSection ,
261+ events .TypeAudioOutputRemoved : audioSection ,
262+
263+ events .TypeServiceUpdated : systemdSection ,
264+ events .TypeBluetoothUpdated : bluetoothSection ,
265+ }
266+ )
267+
274268// SSEEvents streams HTML section fragments as SSE events.
275269func (h * Handler ) SSEEvents (w http.ResponseWriter , r * http.Request ) {
276270 flusher , ok := w .(http.Flusher )
0 commit comments