From a00221e25ec98d0e7b47bb0a574f963ea9a3004b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torjus=20H=C3=A5kestad?= Date: Wed, 6 Dec 2023 02:52:56 +0100 Subject: [PATCH] Fix streamList updating --- src/js/app.tsx | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/src/js/app.tsx b/src/js/app.tsx index a332c3d..73d754d 100644 --- a/src/js/app.tsx +++ b/src/js/app.tsx @@ -4,6 +4,7 @@ import { createContext, useEffect, useState } from "react"; import { MediaContainer } from "./media"; import { MinistreamApiClient, StreamInfo } from "./api"; import React from "react"; +import { Log } from "./log"; interface AppProps { api: MinistreamApiClient @@ -33,24 +34,10 @@ export function App({ api }: AppProps) { setSelectedStream(item) } - const updateStreamList = () => { - api.listStreams().then((list) => { - setStreamList(list) - if (list.length != streamList.length) { - setStreamList(list) - return - } - if (!list.every((_, idx) => { - return list[idx].streamKey === streamList[idx].streamKey - })) { - setStreamList(list) - } - }) - } const updateTitle = () => { - api.siteInfo().then((info) => { + api.siteInfo().then((info) => { if (info.siteName != document.title) { setTitle(info.siteName) localStorage.setItem(titleKey, info.siteName) @@ -59,12 +46,32 @@ export function App({ api }: AppProps) { return } - setInterval(() => { - updateStreamList() - }, 10000) useEffect(() => { + const updateStreamList = () => { + api.listStreams().then((list) => { + setStreamList((current) => { + if (list.length != current.length) { + Log.Debug("Updated streamList!") + return list + } + if (!list.every((_, idx) => { + return list[idx].streamKey === current[idx].streamKey + })) { + Log.Debug("Updated streamList..") + return list + } + return current + }) + }) + + } + updateStreamList() + const updateInterval = setInterval(() => { + updateStreamList() + }, 10000) + return () => clearInterval(updateInterval) }, []) useEffect(() => {