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

View File

@ -38,7 +38,7 @@ import {
this_chid,
} from '../script.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 { getRegexedString, regex_placement } from './extensions/regex/engine.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;
}
for (let messageId = range.start; messageId <= range.end; messageId++) {
const messageBlock = $(`.mes[mesid="${messageId}"]`);
if (!messageBlock.length) {
console.warn(`WARN: No message found with ID ${messageId}`);
return;
}
await hideChatMessage(messageId, messageBlock);
}
await hideChatMessageRange(range.start, range.end, false);
}
async function unhideMessageCallback(_, arg) {
@ -942,17 +933,7 @@ async function unhideMessageCallback(_, arg) {
return '';
}
for (let messageId = range.start; messageId <= range.end; messageId++) {
const messageBlock = $(`.mes[mesid="${messageId}"]`);
if (!messageBlock.length) {
console.warn(`WARN: No message found with ID ${messageId}`);
return '';
}
await unhideChatMessage(messageId, messageBlock);
}
await hideChatMessageRange(range.start, range.end, true);
return '';
}