Massive speedup in loading of long chats

This commit is contained in:
Grzegorz Gidel
2023-04-23 01:35:03 +02:00
parent 4fd5d90e70
commit 130ddf8117

View File

@ -846,7 +846,7 @@ async function replaceCurrentChat() {
function printMessages() { function printMessages() {
chat.forEach(function (item, i, arr) { chat.forEach(function (item, i, arr) {
addOneMessage(item); addOneMessage(item, { scroll: i === arr.length - 1 });
}); });
} }
@ -916,7 +916,7 @@ function appendImageToMessage(mes, messageElement) {
} }
} }
function addOneMessage(mes, type = "normal", insertAfter = null) { function addOneMessage(mes, { type = "normal", insertAfter = null, scroll = true } = {}) {
var messageText = mes["mes"]; var messageText = mes["mes"];
var characterName = name1; var characterName = name1;
var avatarImg = "User Avatars/" + user_avatar; var avatarImg = "User Avatars/" + user_avatar;
@ -1000,7 +1000,7 @@ function addOneMessage(mes, type = "normal", insertAfter = null) {
*/ */
// Don't scroll if not inserting last // Don't scroll if not inserting last
if (!insertAfter) { if (!insertAfter && scroll) {
$('#chat .mes').last().addClass('last_mes'); $('#chat .mes').last().addClass('last_mes');
$('#chat .mes').eq(-2).removeClass('last_mes'); $('#chat .mes').eq(-2).removeClass('last_mes');
@ -2224,7 +2224,7 @@ function saveReply(type, getMessage, this_mes_is_name) {
//console.log(getMessage); //console.log(getMessage);
chat[chat.length - 1]['mes'] = getMessage; chat[chat.length - 1]['mes'] = getMessage;
// console.log('runGenerate calls addOneMessage for swipe'); // console.log('runGenerate calls addOneMessage for swipe');
addOneMessage(chat[chat.length - 1], 'swipe'); addOneMessage(chat[chat.length - 1], { type: 'swipe' });
} else { } else {
chat[chat.length - 1]['mes'] = getMessage; chat[chat.length - 1]['mes'] = getMessage;
} }
@ -3574,7 +3574,7 @@ $(document).ready(function () {
} else { } else {
//console.log('showing previously generated swipe candidate, or "..."'); //console.log('showing previously generated swipe candidate, or "..."');
//console.log('onclick right swipe calling addOneMessage'); //console.log('onclick right swipe calling addOneMessage');
addOneMessage(chat[chat.length - 1], 'swipe'); addOneMessage(chat[chat.length - 1], { type: 'swipe' });
} }
let new_height = this_mes_div_height - (this_mes_block_height - this_mes_block[0].scrollHeight); let new_height = this_mes_div_height - (this_mes_block_height - this_mes_block[0].scrollHeight);
if (new_height < 103) new_height = 103; if (new_height < 103) new_height = 103;
@ -3682,7 +3682,7 @@ $(document).ready(function () {
complete: function () { complete: function () {
const is_animation_scroll = ($('#chat').scrollTop() >= ($('#chat').prop("scrollHeight") - $('#chat').outerHeight()) - 10); const is_animation_scroll = ($('#chat').scrollTop() >= ($('#chat').prop("scrollHeight") - $('#chat').outerHeight()) - 10);
//console.log('on left swipe click calling addOneMessage'); //console.log('on left swipe click calling addOneMessage');
addOneMessage(chat[chat.length - 1], 'swipe'); addOneMessage(chat[chat.length - 1], { type: 'swipe' });
let new_height = this_mes_div_height - (this_mes_block_height - this_mes_block[0].scrollHeight); let new_height = this_mes_div_height - (this_mes_block_height - this_mes_block[0].scrollHeight);
if (new_height < 103) new_height = 103; if (new_height < 103) new_height = 103;
this_mes_div.animate({ height: new_height + 'px' }, { this_mes_div.animate({ height: new_height + 'px' }, {
@ -4826,7 +4826,7 @@ $(document).ready(function () {
clone.mes = $(this).closest(".mes").find('.edit_textarea').val().trim(); clone.mes = $(this).closest(".mes").find('.edit_textarea').val().trim();
chat.splice(Number(this_edit_mes_id) + 1, 0, clone); chat.splice(Number(this_edit_mes_id) + 1, 0, clone);
addOneMessage(clone, 'normal', this_edit_mes_id); addOneMessage(clone, { insertAfter: this_edit_mes_id });
updateViewMessageIds(); updateViewMessageIds();
saveChatConditional(); saveChatConditional();