Fixed upload progress (svelte) - added emit await

This commit is contained in:
Niklas Jensen
2026-01-31 21:31:56 +01:00
parent cc2f6d4747
commit 9d8c79f9f8
3 changed files with 36 additions and 10 deletions
+6 -4
View File
@@ -343,7 +343,8 @@ export class FileSystemClient {
this.activeUploads.set(transferId, upload) this.activeUploads.set(transferId, upload)
// Stream all chunks without waiting for ACKs // Stream chunks one at a time, waiting for each to be sent
;(async () => {
for (let chunkIndex = 0; chunkIndex < totalChunks; chunkIndex++) { for (let chunkIndex = 0; chunkIndex < totalChunks; chunkIndex++) {
const offset = chunkIndex * chunkSize const offset = chunkIndex * chunkSize
const end = Math.min(offset + chunkSize, fileSize) const end = Math.min(offset + chunkSize, fileSize)
@@ -355,12 +356,12 @@ export class FileSystemClient {
data: chunkData data: chunkData
} }
// Send chunk as fire-and-forget message // Wait for chunk to be sent before continuing
socket.emit(FSMessages.FSUploadData, uploadData) await socket.emit(FSMessages.FSUploadData, uploadData)
upload.chunksSent++ upload.chunksSent++
// Report progress // Report progress after chunk is actually sent
if (onProgress) { if (onProgress) {
onProgress({ onProgress({
transferId, transferId,
@@ -375,6 +376,7 @@ export class FileSystemClient {
// All chunks sent - now wait for completion message from server // All chunks sent - now wait for completion message from server
// The timeout will handle if server doesn't respond // The timeout will handle if server doesn't respond
})()
}) })
} }
+21 -6
View File
@@ -198,12 +198,27 @@ function createWebSocket() {
unresponsiveTimeoutId = setTimeout(() => disconnect('unresponsive'), reconnectTimeoutTime) unresponsiveTimeoutId = setTimeout(() => disconnect('unresponsive'), reconnectTimeoutTime)
} }
function emit<T>(event: MessageFns<T>, data: T) { function emit<T>(event: MessageFns<T>, data: T): Promise<void> {
if (!ws || ws.readyState !== WebSocket.OPEN) return return new Promise((resolve) => {
const type = getNameFromMessageType(event) if (!ws || ws.readyState !== WebSocket.OPEN) {
const wsm = Message.create() as Record<string, unknown> resolve()
wsm[type] = data return
send(wsm as Message) }
const type = getNameFromMessageType(event)
const wsm = Message.create() as Record<string, unknown>
wsm[type] = data
send(wsm as Message)
// Wait for buffer to flush before resolving
const checkBuffer = () => {
if (!ws || ws.bufferedAmount === 0) {
resolve()
} else {
setTimeout(checkBuffer, 5)
}
}
checkBuffer()
})
} }
function unsubscribeToMessageFromServer<T>(event_type: MessageFns<T>) { function unsubscribeToMessageFromServer<T>(event_type: MessageFns<T>) {
+9
View File
@@ -268,6 +268,15 @@ esp_err_t WebServer::wsSend(int sockfd, const uint8_t* data, size_t len) {
xSemaphoreTake(wsMutex_, portMAX_DELAY); xSemaphoreTake(wsMutex_, portMAX_DELAY);
esp_err_t ret = httpd_ws_send_frame_async(server_, sockfd, &frame); esp_err_t ret = httpd_ws_send_frame_async(server_, sockfd, &frame);
xSemaphoreGive(wsMutex_); xSemaphoreGive(wsMutex_);
if (ret != ESP_OK) {
if (httpd_ws_get_fd_info(server_, sockfd) != HTTPD_WS_CLIENT_WEBSOCKET) {
ESP_LOGW(TAG, "Removing disconnected client %d", sockfd);
removeWsClient(sockfd);
return ESP_ERR_INVALID_STATE;
}
}
return ret; return ret;
} }