diff --git a/packages/api/src/EmbeddedChatApi.ts b/packages/api/src/EmbeddedChatApi.ts index 72e25a046..676f5a51e 100644 --- a/packages/api/src/EmbeddedChatApi.ts +++ b/packages/api/src/EmbeddedChatApi.ts @@ -120,7 +120,7 @@ export default class EmbeddedChatApi { async login(userOrEmail: string, password: string, code: string) { let credentials; if (!code) { - credentials = credentials = { + credentials = { user: userOrEmail.trim(), password, }; @@ -351,33 +351,37 @@ export default class EmbeddedChatApi { ); } - handleTypingEvent({ + async handleTypingEvent({ typingUser, isTyping, }: { 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) { - this.typingUsers.splice(idx, 1); + // wait for lock to release + while (typingHandlerLock) { + await new Promise((resolve) => setTimeout(resolve, 100)); } - if (isTyping) { - this.typingUsers.unshift(typingUser); + + typingHandlerLock = 1; + + try { + // move user to front if typing else remove it. + const idx = this.typingUsers.indexOf(typingUser); + if (idx !== -1) { + this.typingUsers.splice(idx, 1); + } + if (isTyping) { + this.typingUsers.unshift(typingUser); + } + + const newTypingStatus = cloneArray(this.typingUsers); + this.onTypingStatusCallbacks.forEach((callback) => + callback(newTypingStatus) + ); + } finally { + typingHandlerLock = 0; } - typingHandlerLock = 0; - const newTypingStatus = cloneArray(this.typingUsers); - this.onTypingStatusCallbacks.forEach((callback) => - callback(newTypingStatus) - ); } async getRCAppInfo() {