···552552 nowPlaying: querySelector("now-playing", HTMLNowPlayingElement),
553553 };
554554555555+ /************
556556+ * LISTENER *
557557+ ************/
558558+559559+ let prev: nowPlaying = null;
560560+561561+ const ev = new EventSource("/now-playing-sse");
562562+563563+ let i = -1;
564564+ ev.addEventListener("playing", (event) => {
565565+ i++;
566566+ const data = (() => {
567567+ try {
568568+ return JSON.parse(event.data);
569569+ } catch (e) {
570570+ return e;
571571+ }
572572+ })();
573573+ if (!isNowPlaying(data))
574574+ return console.warn("Unexpected package from server:", data, event.data);
575575+576576+ // data is valid nowPlayingData
577577+ // first call so assume prev is in an invalid state (which it is)
578578+ if (i === 0) console.log("SKIP: i = 0");
579579+ // if both are null, quit since re-rendering is pointless
580580+ else if (data == prev) console.log("MATCH: null = null");
581581+ // now if both are valid play items, so check the ID for equality
582582+ else if (data !== null && prev !== null && data.id === prev.id)
583583+ console.log("MATCH: nowPlaying = nowPlaying");
584584+ else {
585585+ console.log("DIFFERENT");
586586+ // there is now a difference between the previous setting and the new setting
587587+ // so it is worth updating the UI
588588+589589+ elements.recordArt.src = data
590590+ ? data?.album.images[0].url
591591+ : "https://undefined";
592592+593593+ if (data) elements.nowPlaying.updateMetadata(data);
594594+ else elements.nowPlaying.nothingPlaying();
595595+ }
596596+597597+ prev = data;
598598+ });
555599</script>