From e15944622d3741137f443e7fa0b5d193b4ce28d9 Mon Sep 17 00:00:00 2001
From: Ilion Beyst <ilion.beyst@gmail.com>
Date: Tue, 22 Feb 2022 19:44:29 +0100
Subject: poll for matches to complete

---
 web/pw-server/src/routes/index.svelte | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)

(limited to 'web/pw-server/src/routes/index.svelte')

diff --git a/web/pw-server/src/routes/index.svelte b/web/pw-server/src/routes/index.svelte
index 59552bc..7b364be 100644
--- a/web/pw-server/src/routes/index.svelte
+++ b/web/pw-server/src/routes/index.svelte
@@ -55,15 +55,30 @@
 
     let matchData = responseData["match"];
 
-    matches.push(matchData);
+    matches.unshift(matchData);
     matches = matches;
+    selectMatch(matchData["id"]);
   }
 
   async function selectMatch(matchId: string) {
-    console.log("showing match " + matchId);
-    let matchLog = await getMatchLog(matchId);
     selectedMatchId = matchId;
-    selectedMatchLog = matchLog;
+    selectedMatchLog = null;
+    fetchSelectedMatchLog(matchId);
+  }
+
+  async function fetchSelectedMatchLog(matchId: string) {
+    if (matchId !== selectedMatchId) {
+      return;
+    }
+
+    let matchLog = await getMatchLog(matchId);
+
+    if (matchLog) {
+      selectedMatchLog = matchLog;
+    } else {
+      // try again in 1 second
+      setTimeout(fetchSelectedMatchLog, 1000, matchId);
+    }
   }
 
   async function getMatchData(matchId: string) {
-- 
cgit v1.2.3

