refactor: remove chats.initialized, all events handlers are removed before being added

This commit is contained in:
Barış Soner Uşaklı
2026-01-20 19:09:32 -05:00
parent ab39e7f8ae
commit fffe039f46

View File

@@ -26,7 +26,6 @@ define('forum/chats', [
api, uploadHelpers api, uploadHelpers
) { ) {
const Chats = { const Chats = {
initialised: false,
activeAutocomplete: {}, activeAutocomplete: {},
newMessage: false, newMessage: false,
}; };
@@ -55,10 +54,9 @@ define('forum/chats', [
socket.emit('modules.chats.enterPublic', ajaxify.data.publicRooms.map(r => r.roomId)); socket.emit('modules.chats.enterPublic', ajaxify.data.publicRooms.map(r => r.roomId));
const env = utils.findBootstrapEnvironment(); const env = utils.findBootstrapEnvironment();
chatNavWrapper = $('[component="chat/nav-wrapper"]'); chatNavWrapper = $('[component="chat/nav-wrapper"]');
if (!Chats.initialised) {
Chats.addSocketListeners(); Chats.addSocketListeners();
Chats.addGlobalEventListeners(); Chats.addGlobalEventListeners();
}
recentChats.init(); recentChats.init();
@@ -69,7 +67,6 @@ define('forum/chats', [
Chats.addHotkeys(); Chats.addHotkeys();
} }
Chats.initialised = true;
const chatContentEl = $('[component="chat/message/content"]'); const chatContentEl = $('[component="chat/message/content"]');
messages.wrapImagesInLinks(chatContentEl); messages.wrapImagesInLinks(chatContentEl);
if (ajaxify.data.scrollToIndex) { if (ajaxify.data.scrollToIndex) {
@@ -676,14 +673,18 @@ define('forum/chats', [
}; };
Chats.addGlobalEventListeners = function () { Chats.addGlobalEventListeners = function () {
$(window).on('mousemove keypress click', function () { $(window).off('mousemove keypress click', onUserInteraction)
if (Chats.newMessage && ajaxify.data.roomId) { .on('mousemove keypress click', onUserInteraction);
api.del(`/chats/${ajaxify.data.roomId}/state`, {});
Chats.newMessage = false;
}
});
}; };
function onUserInteraction() {
if (Chats.newMessage && ajaxify.data.roomId) {
// mark current room read on user interaction
api.del(`/chats/${ajaxify.data.roomId}/state`, {});
Chats.newMessage = false;
}
}
Chats.addSocketListeners = function () { Chats.addSocketListeners = function () {
events.init(); events.init();