From b6e3efc0ec1f047c149c8028e7106f0bf5604aa6 Mon Sep 17 00:00:00 2001 From: Abhiram Date: Fri, 20 Feb 2026 20:20:30 +0530 Subject: [PATCH] fix: remove busy-wait loop and add missing await in EmbeddedChatApi Remove synchronous busy-wait mutex (while loop) from handleTypingEvent that is unnecessary in single-threaded JavaScript and risks freezing the browser tab. Add missing await on fetch() in sendAttachment so the try/catch block properly catches network errors. Add missing await on response.json() in getUserStatus, userInfo, and userData for consistent error handling. --- packages/api/src/EmbeddedChatApi.ts | 37 ++++++++++++----------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/packages/api/src/EmbeddedChatApi.ts b/packages/api/src/EmbeddedChatApi.ts index 72e25a046..b8f1ffbec 100644 --- a/packages/api/src/EmbeddedChatApi.ts +++ b/packages/api/src/EmbeddedChatApi.ts @@ -7,8 +7,6 @@ import { ApiError, } from "@embeddedchat/auth"; -// mutliple typing status can come at the same time they should be processed in order. -let typingHandlerLock = 0; export default class EmbeddedChatApi { host: string; rid: string; @@ -358,13 +356,6 @@ export default class EmbeddedChatApi { typingUser: string; isTyping: boolean; }) { - // don't wait for more than 2 seconds. Though in practical, the waiting time is insignificant. - setTimeout(() => { - typingHandlerLock = 0; - }, 2000); - // eslint-disable-next-line no-empty - while (typingHandlerLock) {} - typingHandlerLock = 1; // move user to front if typing else remove it. const idx = this.typingUsers.indexOf(typingUser); if (idx !== -1) { @@ -373,7 +364,6 @@ export default class EmbeddedChatApi { if (isTyping) { this.typingUsers.unshift(typingUser); } - typingHandlerLock = 0; const newTypingStatus = cloneArray(this.typingUsers); this.onTypingStatusCallbacks.forEach((callback) => callback(newTypingStatus) @@ -1065,15 +1055,18 @@ export default class EmbeddedChatApi { "description", fileDescription.length !== 0 ? fileDescription : "" ); - const response = fetch(`${this.host}/api/v1/rooms.upload/${this.rid}`, { - method: "POST", - body: form, - headers: { - "X-Auth-Token": authToken, - "X-User-Id": userId, - }, - }).then((r) => r.json()); - return response; + const response = await fetch( + `${this.host}/api/v1/rooms.upload/${this.rid}`, + { + method: "POST", + body: form, + headers: { + "X-Auth-Token": authToken, + "X-User-Id": userId, + }, + } + ); + return await response.json(); } catch (err) { console.log(err); } @@ -1243,7 +1236,7 @@ export default class EmbeddedChatApi { }, } ); - const data = response.json(); + const data = await response.json(); return data; } @@ -1260,7 +1253,7 @@ export default class EmbeddedChatApi { }, } ); - const data = response.json(); + const data = await response.json(); return data; } @@ -1277,7 +1270,7 @@ export default class EmbeddedChatApi { }, } ); - const data = response.json(); + const data = await response.json(); return data; } }