Changeset - a10f0f0e4dae
[Not reviewed]
default
0 3 0
drewp@bigasterisk.com - 20 months ago 2023-05-31 20:34:06
drewp@bigasterisk.com
update web ui with one SSE, not repeated requests
3 files changed with 29 insertions and 49 deletions:
0 comments (0 inline, 0 general)
light9/ascoltami/main.py
Show inline comments
 
@@ -50,6 +50,7 @@ class Ascoltami:
 

	
 

	
 
def main():
 
    logging.getLogger('sse_starlette.sse').setLevel(logging.INFO)
 
    Gst.init(None)
 

	
 
    graph = showconfig.getGraph()
light9/ascoltami/main.ts
Show inline comments
 
@@ -17,8 +17,15 @@ export interface TimingUpdate {
 
  let currentHighlightedSong = "";
 
  // let lastPlaying = false;
 

	
 
  async function updateCurrent() {
 
    const data: TimingUpdate = await (await fetch("api/time")).json();
 
  
 
  const events = new EventSource("api/time/stream");
 
  events.addEventListener("message", (m)=>{
 
    const update = JSON.parse(m.data) as TimingUpdate
 
    updateCurrent(update)
 
    markUpdateTiming();
 
  })
 

	
 
  async function updateCurrent(data:TimingUpdate) {
 
    byId("currentSong").innerText = data.song;
 
    if (data.song != currentHighlightedSong) {
 
      showCurrentSong(data.song);
 
@@ -30,6 +37,24 @@ export interface TimingUpdate {
 
    //   document.querySelector("#timeSlider").slider({ value: data.t, max: data.duration });
 
    timingUpdate(data);
 
  }
 
  let recentUpdates: Array<number> = [];
 
  function markUpdateTiming() {
 
    recentUpdates.push(+new Date());
 
    recentUpdates = recentUpdates.slice(Math.max(recentUpdates.length - 5, 0));
 
  }
 

	
 
  function refreshUpdateFreqs() {
 
    if (recentUpdates.length > 1) {
 
      if (+new Date() - recentUpdates[recentUpdates.length - 1] > 1000) {
 
        byId("updateActual").innerText = "(stalled)";
 
        return;
 
      }
 

	
 
      var avgMs = (recentUpdates[recentUpdates.length - 1] - recentUpdates[0]) / (recentUpdates.length - 1);
 
      byId("updateActual").innerText = "" + Math.round(1000 / avgMs);
 
    }
 
  }
 
  setInterval(refreshUpdateFreqs, 2000);
 

	
 
  function showCurrentSong(uri: string) {
 
    document.querySelectorAll(".songs div").forEach((row: Element, i: number) => {
 
@@ -72,50 +97,4 @@ export interface TimingUpdate {
 
    document.querySelector(".songs")!.appendChild(dv);
 
  });
 

	
 
  //   var pendingSlide = false;
 
  //   $("#timeSlider").slider({
 
  //     step: 0.01,
 
  //     slide: function (event, ui) {
 
  //       if (pendingSlide) {
 
  //         return;
 
  //       }
 
  //       pendingSlide = true;
 
  //       $.post("time", '{"t" : ' + ui.value + "}", function (data, status, xhr) {
 
  //         pendingSlide = false;
 
  //       });
 
  //     },
 
  //   });
 

	
 
  let recentUpdates: Array<number> = [];
 
  function onUpdate() {
 
    recentUpdates.push(+new Date());
 
    recentUpdates = recentUpdates.slice(Math.max(recentUpdates.length - 5, 0));
 
    refreshUpdateFreqs();
 
  }
 

	
 
  function refreshUpdateFreqs() {
 
    if (recentUpdates.length > 1) {
 
      if (+new Date() - recentUpdates[recentUpdates.length - 1] > 1000) {
 
        byId("updateActual").innerText = "(stalled)";
 
        document.querySelectorAll(".dimStalled").forEach((el) => el.classList.add("stalled"));
 
        return;
 
      }
 

	
 
      var avgMs = (recentUpdates[recentUpdates.length - 1] - recentUpdates[0]) / (recentUpdates.length - 1);
 
      byId("updateActual").innerText = "" + Math.round(1000 / avgMs);
 
    }
 
  }
 
  setInterval(refreshUpdateFreqs, 2000);
 

	
 
  async function updateLoop() {
 
    var whenDone = function () {
 
      setTimeout(function () {
 
        requestAnimationFrame(updateLoop);
 
      }, 1000 / updateFreq);
 
    };
 
    onUpdate();
 
    await updateCurrent();
 
    whenDone();
 
  }
 
  updateLoop();
 
};
light9/ascoltami/webapp.py
Show inline comments
 
@@ -109,7 +109,7 @@ async def timeStream(request: Request):
 
                last_sent = msg
 
                last_sent_time = now
 

	
 
            await asyncio.sleep(0.2)
 
            await asyncio.sleep(0.1)
 

	
 
    return EventSourceResponse(event_generator())
 

	
0 comments (0 inline, 0 general)