Skip to content

Commit

Permalink
refactor: reduce socket.emits for typing
Browse files Browse the repository at this point in the history
if textarea changess between empty/full emit right away
  • Loading branch information
barisusakli committed Sep 6, 2023
1 parent e4ecb96 commit aebd927
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions public/src/client/chats.js
Expand Up @@ -331,9 +331,22 @@ define('forum/chats', [

textarea.on('focus', () => textarea.val() && emitTyping(true));
textarea.on('blur', () => emitTyping(false));
textarea.on('input', utils.throttle(function () {
emitTyping(!!textarea.val());
}, 2500, true));
let timeoutid = 0;
let hasText = !!textarea.val();
textarea.on('input', function () {
const _hasText = !!textarea.val();
if (_hasText !== hasText) {
clearTimeout(timeoutid);
timeoutid = 0;
hasText = _hasText;
emitTyping(hasText);
} else if (!timeoutid) {
timeoutid = setTimeout(() => {
emitTyping(!!textarea.val());
timeoutid = 0;
}, 5000);
}
});
};

Chats.addActionHandlers = function (element, roomId) {
Expand Down

0 comments on commit aebd927

Please sign in to comment.