[object Object]

← back to Exo

ensuring requests do not stack up by moving polling to while loop with 5 second delay after

ded80b0ffdf1f7b9e2da21e577fd9aa461f6d311 · 2024-11-25 16:25:52 -0800 · cadenmackenzie

Files touched

Diff

commit ded80b0ffdf1f7b9e2da21e577fd9aa461f6d311
Author: cadenmackenzie <cadenmackenzie@gmail.com>
Date:   Mon Nov 25 16:25:52 2024 -0800

    ensuring requests do not stack up by moving polling to while loop with 5 second delay after
---
 exo/tinychat/index.js | 69 +++++++++++++++++++++++++++++++--------------------
 1 file changed, 42 insertions(+), 27 deletions(-)

diff --git a/exo/tinychat/index.js b/exo/tinychat/index.js
index 3a5b5e67..e724b490 100644
--- a/exo/tinychat/index.js
+++ b/exo/tinychat/index.js
@@ -46,11 +46,48 @@ document.addEventListener("alpine:init", () => {
       // Start polling for download progress
       this.startDownloadProgressPolling();
       
-      // Call populateSelector immediately after initialization
-      this.populateSelector();
-      this.modelPoolInterval = setInterval(() => {
-        this.populateSelector();
-      }, 5000);
+      // Start model polling with the new pattern
+      this.startModelPolling();
+    },
+
+    async startModelPolling() {
+      while (true) {
+        try {
+          await this.populateSelector();
+          // Wait 5 seconds before next poll
+          await new Promise(resolve => setTimeout(resolve, 5000));
+        } catch (error) {
+          console.error('Model polling error:', error);
+          // If there's an error, wait before retrying
+          await new Promise(resolve => setTimeout(resolve, 5000));
+        }
+      }
+    },
+
+    async populateSelector() {
+      return new Promise((resolve, reject) => {
+        const evtSource = new EventSource(`${window.location.origin}/modelpool`);
+        
+        evtSource.onmessage = (event) => {
+          if (event.data === "[DONE]") {
+            evtSource.close();
+            resolve();
+            return;
+          }
+          
+          const modelData = JSON.parse(event.data);
+          this.models = {
+            ...this.models,
+            ...modelData
+          };
+        };
+        
+        evtSource.onerror = (error) => {
+          console.error('EventSource failed:', error);
+          evtSource.close();
+          reject(error);
+        };
+      });
     },
 
     removeHistory(cstate) {
@@ -87,28 +124,6 @@ document.addEventListener("alpine:init", () => {
       return `${s}s`;
     },
 
-    async populateSelector() {
-      const evtSource = new EventSource(`${window.location.origin}/modelpool`);
-      
-      evtSource.onmessage = (event) => {
-        if (event.data === "[DONE]") {
-          evtSource.close();
-          return;
-        }
-        
-        const modelData = JSON.parse(event.data);
-        this.models = {
-          ...this.models,
-          ...modelData
-        };
-      };
-      
-      evtSource.onerror = (error) => {
-        console.error('EventSource failed:', error);
-        evtSource.close();
-      };
-    },
-
     async handleImageUpload(event) {
       const file = event.target.files[0];
       if (file) {

← 3f6ea173 remove redundant imports  ·  back to Exo  ·  adding a fetch to get initail model object to show models be e99a7394 →