Merge pull request #2073 from ThisIsPIRI/hide

Performance improvement for /hide, /unhide
This commit is contained in:
Cohee 2024-04-12 14:19:59 +03:00 committed by GitHub
commit b09cabaeb2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 54 deletions

View File

@ -44,22 +44,29 @@ function isConvertible(type) {
} }
/** /**
* Mark message as hidden (system message). * Mark a range of messages as hidden ("is_system") or not.
* @param {number} messageId Message ID * @param {number} start Starting message ID
* @param {JQuery<Element>} messageBlock Message UI element * @param {number} end Ending message ID (inclusive)
* @returns * @param {boolean} unhide If true, unhide the messages instead.
* @returns {Promise<void>}
*/ */
export async function hideChatMessage(messageId, messageBlock) { export async function hideChatMessageRange(start, end, unhide) {
const chatId = getCurrentChatId(); if (!getCurrentChatId()) return;
if (!chatId || isNaN(messageId)) return; if (isNaN(start)) return;
if (!end) end = start;
const hide = !unhide;
for (let messageId = start; messageId <= end; messageId++) {
const message = chat[messageId]; const message = chat[messageId];
if (!message) continue;
if (!message) return; const messageBlock = $(`.mes[mesid="${messageId}"]`);
if (!messageBlock.length) continue;
message.is_system = true; message.is_system = hide;
messageBlock.attr('is_system', String(true)); messageBlock.attr('is_system', String(hide));
}
// Reload swipes. Useful when a last message is hidden. // Reload swipes. Useful when a last message is hidden.
hideSwipeButtons(); hideSwipeButtons();
@ -69,28 +76,25 @@ export async function hideChatMessage(messageId, messageBlock) {
} }
/** /**
* Mark message as visible (non-system message). * Mark message as hidden (system message).
* @deprecated Use hideChatMessageRange.
* @param {number} messageId Message ID * @param {number} messageId Message ID
* @param {JQuery<Element>} messageBlock Message UI element * @param {JQuery<Element>} _messageBlock Unused
* @returns * @returns {Promise<void>}
*/ */
export async function unhideChatMessage(messageId, messageBlock) { export async function hideChatMessage(messageId, _messageBlock) {
const chatId = getCurrentChatId(); return hideChatMessageRange(messageId, messageId, false);
}
if (!chatId || isNaN(messageId)) return; /**
* Mark message as visible (non-system message).
const message = chat[messageId]; * @deprecated Use hideChatMessageRange.
* @param {number} messageId Message ID
if (!message) return; * @param {JQuery<Element>} _messageBlock Unused
* @returns {Promise<void>}
message.is_system = false; */
messageBlock.attr('is_system', String(false)); export async function unhideChatMessage(messageId, _messageBlock) {
return hideChatMessageRange(messageId, messageId, true);
// Reload swipes. Useful when a last message is hidden.
hideSwipeButtons();
showSwipeButtons();
saveChatDebounced();
} }
/** /**
@ -476,13 +480,13 @@ jQuery(function () {
$(document).on('click', '.mes_hide', async function () { $(document).on('click', '.mes_hide', async function () {
const messageBlock = $(this).closest('.mes'); const messageBlock = $(this).closest('.mes');
const messageId = Number(messageBlock.attr('mesid')); const messageId = Number(messageBlock.attr('mesid'));
await hideChatMessage(messageId, messageBlock); await hideChatMessageRange(messageId, messageId, false);
}); });
$(document).on('click', '.mes_unhide', async function () { $(document).on('click', '.mes_unhide', async function () {
const messageBlock = $(this).closest('.mes'); const messageBlock = $(this).closest('.mes');
const messageId = Number(messageBlock.attr('mesid')); const messageId = Number(messageBlock.attr('mesid'));
await unhideChatMessage(messageId, messageBlock); await hideChatMessageRange(messageId, messageId, true);
}); });
$(document).on('click', '.mes_file_delete', async function () { $(document).on('click', '.mes_file_delete', async function () {

View File

@ -38,7 +38,7 @@ import {
this_chid, this_chid,
} from '../script.js'; } from '../script.js';
import { getMessageTimeStamp } from './RossAscends-mods.js'; import { getMessageTimeStamp } from './RossAscends-mods.js';
import { hideChatMessage, unhideChatMessage } from './chats.js'; import { hideChatMessageRange } from './chats.js';
import { getContext, saveMetadataDebounced } from './extensions.js'; import { getContext, saveMetadataDebounced } from './extensions.js';
import { getRegexedString, regex_placement } from './extensions/regex/engine.js'; import { getRegexedString, regex_placement } from './extensions/regex/engine.js';
import { findGroupMemberId, groups, is_group_generating, openGroupById, resetSelectedGroup, saveGroupChat, selected_group } from './group-chats.js'; import { findGroupMemberId, groups, is_group_generating, openGroupById, resetSelectedGroup, saveGroupChat, selected_group } from './group-chats.js';
@ -917,16 +917,7 @@ async function hideMessageCallback(_, arg) {
return; return;
} }
for (let messageId = range.start; messageId <= range.end; messageId++) { await hideChatMessageRange(range.start, range.end, false);
const messageBlock = $(`.mes[mesid="${messageId}"]`);
if (!messageBlock.length) {
console.warn(`WARN: No message found with ID ${messageId}`);
return;
}
await hideChatMessage(messageId, messageBlock);
}
} }
async function unhideMessageCallback(_, arg) { async function unhideMessageCallback(_, arg) {
@ -942,17 +933,7 @@ async function unhideMessageCallback(_, arg) {
return ''; return '';
} }
for (let messageId = range.start; messageId <= range.end; messageId++) { await hideChatMessageRange(range.start, range.end, true);
const messageBlock = $(`.mes[mesid="${messageId}"]`);
if (!messageBlock.length) {
console.warn(`WARN: No message found with ID ${messageId}`);
return '';
}
await unhideChatMessage(messageId, messageBlock);
}
return ''; return '';
} }