← back to Exo
download progress optimization in tinychat
bef585d1effb345a820b8107445ad167c7f7bab2 · 2024-10-01 17:04:09 +0200 · DevEmilio96
non-infinite pooling, it is activated only when the user sends a message, and stops either when the page is refreshed or when the download finishes
Files touched
M tinychat/examples/tinychat/index.js
Diff
commit bef585d1effb345a820b8107445ad167c7f7bab2
Author: DevEmilio96 <minima998@gmail.com>
Date: Tue Oct 1 17:04:09 2024 +0200
download progress optimization in tinychat
non-infinite pooling, it is activated only when the user sends a message, and stops either when the page is refreshed or when the download finishes
---
tinychat/examples/tinychat/index.js | 33 ++++++++++++++++++++++++++++-----
1 file changed, 28 insertions(+), 5 deletions(-)
diff --git a/tinychat/examples/tinychat/index.js b/tinychat/examples/tinychat/index.js
index bf3b831f..b075c05d 100644
--- a/tinychat/examples/tinychat/index.js
+++ b/tinychat/examples/tinychat/index.js
@@ -25,6 +25,7 @@ document.addEventListener("alpine:init", () => {
// download progress
downloadProgress: null,
+ downloadProgressInterval: null, // To keep track of the polling interval
removeHistory(cstate) {
const index = this.histories.findIndex((state) => {
@@ -45,6 +46,7 @@ document.addEventListener("alpine:init", () => {
},
formatDuration(seconds) {
+ if (seconds === null || seconds === undefined || isNaN(seconds)) return '';
const h = Math.floor(seconds / 3600);
const m = Math.floor((seconds % 3600) / 60);
const s = Math.floor(seconds % 60);
@@ -94,6 +96,9 @@ document.addEventListener("alpine:init", () => {
el.style.height = "auto";
el.style.height = el.scrollHeight + "px";
+ // Start polling for download progress
+ this.startDownloadProgressPolling();
+
// reset performance tracking
const prefill_start = Date.now();
let start_time = 0;
@@ -278,6 +283,7 @@ document.addEventListener("alpine:init", () => {
async fetchDownloadProgress() {
try {
console.log("fetching download progress");
+ await new Promise(resolve => setTimeout(resolve, 2000));
const response = await fetch(`${this.endpoint}/download/progress`);
if (response.ok) {
const data = await response.json();
@@ -289,6 +295,8 @@ document.addEventListener("alpine:init", () => {
// Check if download is complete
if (progress.status === "complete" || progress.status === "failed") {
this.downloadProgress = null; // Hide the progress section
+ // Stop polling
+ this.stopDownloadProgressPolling();
} else {
// Compute human-readable strings
progress.downloaded_bytes_display = this.formatBytes(progress.downloaded_bytes);
@@ -302,19 +310,34 @@ document.addEventListener("alpine:init", () => {
} else {
// No ongoing download
this.downloadProgress = null;
+ // Stop polling
+ this.stopDownloadProgressPolling();
}
}
} catch (error) {
console.error("Error fetching download progress:", error);
this.downloadProgress = null;
+ // Stop polling in case of error
+ this.stopDownloadProgressPolling();
}
},
- // Initialize the component
- init() {
- // Start polling for download progress every second
- setInterval(() => {
+
+ startDownloadProgressPolling() {
+ if (this.downloadProgressInterval) {
+ // Already polling
+ return;
+ }
+ this.fetchDownloadProgress(); // Fetch immediately
+ this.downloadProgressInterval = setInterval(() => {
this.fetchDownloadProgress();
- }, 1000);
+ }, 1000); // Poll every second
+ },
+
+ stopDownloadProgressPolling() {
+ if (this.downloadProgressInterval) {
+ clearInterval(this.downloadProgressInterval);
+ this.downloadProgressInterval = null;
+ }
},
}));
});
← 39a8a814 Download Progression in tiny chat
·
back to Exo
·
simplify hardware requirements in readme 71e93a10 →