[ref] performace improvement stage 2

This commit is contained in:
kwaroran
2023-08-06 15:41:34 +09:00
parent f3e96d8ba1
commit 936c8f2dab
5 changed files with 75 additions and 26 deletions

View File

@@ -2,7 +2,7 @@ import DOMPurify from 'isomorphic-dompurify';
import showdown from 'showdown';
import { Marked } from 'marked';
import { DataBase, type Database, type Message, type character, type groupChat } from './storage/database';
import { DataBase, type Database, type Message, type character, type customscript, type groupChat } from './storage/database';
import { getFileSrc } from './storage/globalApi';
import { processScript, processScriptFull } from './process/scripts';
import { get } from 'svelte/store';
@@ -58,7 +58,7 @@ DOMPurify.addHook("uponSanitizeAttribute", (node, data) => {
const assetRegex = /{{(raw|img|video|audio|bg)::(.+?)}}/g
async function parseAdditionalAssets(data:string, char:character, mode:'normal'|'back'){
async function parseAdditionalAssets(data:string, char:simpleCharacterArgument|character, mode:'normal'|'back'){
const db = get(DataBase)
const assetWidthString = (db.assetWidth && db.assetWidth !== -1 || db.assetWidth === 0) ? `max-width:${db.assetWidth}rem;` : ''
@@ -97,9 +97,19 @@ async function parseAdditionalAssets(data:string, char:character, mode:'normal'|
return data
}
export async function ParseMarkdown(data:string, char:(character | groupChat) = null, mode:'normal'|'back' = 'normal', chatID=-1) {
export interface simpleCharacterArgument{
type: 'simple'
additionalAssets?: [string, string, string][]
customscript: customscript[]
chaId: string,
}
export async function ParseMarkdown(data:string, charArg:(simpleCharacterArgument | groupChat | string) = null, mode:'normal'|'back' = 'normal', chatID=-1) {
let firstParsed = ''
const orgDat = data
let char = (typeof(charArg) === 'string') ? (findCharacterbyId(charArg)) : (charArg)
if(char && char.type !== 'group'){
data = await parseAdditionalAssets(data, char, mode)
firstParsed = data

View File

@@ -5,7 +5,7 @@ import { downloadFile } from "../storage/globalApi";
import { alertError, alertNormal } from "../alert";
import { language } from "src/lang";
import { selectSingleFile } from "../util";
import { risuChatParser as risuChatParserOrg } from "../parser";
import { risuChatParser as risuChatParserOrg, type simpleCharacterArgument } from "../parser";
import { autoMarkPlugin } from "../plugins/automark";
const dreg = /{{data}}/g
@@ -54,7 +54,7 @@ export async function importRegex(){
}
}
export function processScriptFull(char:character|groupChat, data:string, mode:ScriptMode, chatID = -1){
export function processScriptFull(char:character|groupChat|simpleCharacterArgument, data:string, mode:ScriptMode, chatID = -1){
let db = get(DataBase)
let emoChanged = false
const scripts = (db.globalscript ?? []).concat(char.customscript)
@@ -83,14 +83,16 @@ export function processScriptFull(char:character|groupChat, data:string, mode:Sc
if(tempEmotion.length > 4){
tempEmotion.splice(0, 1)
}
for(const emo of char.emotionImages){
if(emo[0] === emoName){
const emos:[string, string,number] = [emo[0], emo[1], Date.now()]
tempEmotion.push(emos)
charemotions[char.chaId] = tempEmotion
CharEmotion.set(charemotions)
emoChanged = true
break
if(char.type !== 'simple'){
for(const emo of char.emotionImages){
if(emo[0] === emoName){
const emos:[string, string,number] = [emo[0], emo[1], Date.now()]
tempEmotion.push(emos)
charemotions[char.chaId] = tempEmotion
CharEmotion.set(charemotions)
emoChanged = true
break
}
}
}
}

View File

@@ -1,6 +1,7 @@
import { get, writable } from "svelte/store";
import { DataBase } from "./storage/database";
import { DataBase, type character, type groupChat } from "./storage/database";
import { cloneDeep, isEqual } from "lodash";
import type { simpleCharacterArgument } from "./parser";
function updateSize(){
SizeStore.set({
@@ -30,7 +31,28 @@ let currentChar = get(selectedCharID)
let currentCharacter = db.characters ? (db.characters[currentChar]) : null
let currentChat = currentCharacter ? (currentCharacter.chats[currentCharacter.chatPage]) : null
export const CurrentCharacter = writable(cloneDeep(currentCharacter))
export const CurrentSimpleCharacter = writable(createSimpleCharacter(currentCharacter))
export const CurrentChat = writable(cloneDeep(currentChat))
export const CurrentUsername = writable(db.username)
export const CurrentUserIcon = writable(db.userIcon)
export const CurrentShowMemoryLimit = writable(db.showMemoryLimit)
function createSimpleCharacter(char:character|groupChat){
if((!char) || char.type === 'group'){
return null
}
const simpleChar:simpleCharacterArgument = {
type: "simple",
customscript: cloneDeep(char.customscript),
chaId: char.chaId,
additionalAssets: char.additionalAssets
}
return simpleChar
}
function updateCurrentCharacter(){
const db = get(DataBase)
@@ -48,6 +70,12 @@ function updateCurrentCharacter(){
}
console.log("Character updated")
CurrentCharacter.set(cloneDeep(currentChar))
const simp = createSimpleCharacter(currentChar)
if(!isEqual(get(CurrentSimpleCharacter), simp)){
CurrentSimpleCharacter.set(simp)
}
updateCurrentChat()
}
@@ -67,6 +95,15 @@ function updateCurrentChat(){
DataBase.subscribe((data) => {
updateCurrentCharacter()
if(data.username !== get(CurrentUsername)){
CurrentUsername.set(data.username)
}
if(data.userIcon !== get(CurrentUserIcon)){
CurrentUserIcon.set(data.userIcon)
}
if(data.showMemoryLimit !== get(CurrentShowMemoryLimit)){
CurrentShowMemoryLimit.set(data.showMemoryLimit)
}
})
selectedCharID.subscribe((id) => {