refactor: Optimize current character initialization in stores.ts

This commit is contained in:
kwaroran
2024-06-24 01:38:24 +09:00
parent 5f59349b84
commit 6e9940df2d

View File

@@ -2,6 +2,7 @@ import { get, writable, type Writable } from "svelte/store";
import { DataBase, type Chat, type character, type groupChat } from "./storage/database"; import { DataBase, type Chat, type character, type groupChat } from "./storage/database";
import { isEqual } from "lodash"; import { isEqual } from "lodash";
import type { simpleCharacterArgument } from "./parser"; import type { simpleCharacterArgument } from "./parser";
import { sleep } from "./util";
function updateSize(){ function updateSize(){
SizeStore.set({ SizeStore.set({
@@ -70,7 +71,9 @@ function createSimpleCharacter(char:character|groupChat){
} }
function updateCurrentCharacter(){ async function preInit(){
await sleep(1)
function updateCurrentCharacter(){
const db = get(DataBase) const db = get(DataBase)
if(!db.characters){ if(!db.characters){
@@ -97,9 +100,9 @@ function updateCurrentCharacter(){
} }
updateCurrentChat() updateCurrentChat()
} }
function updateCurrentChat(){ function updateCurrentChat(){
const currentChar = get(CurrentCharacter) const currentChar = get(CurrentCharacter)
if(!currentChar){ if(!currentChar){
CurrentChat.set(null) CurrentChat.set(null)
@@ -111,9 +114,9 @@ function updateCurrentChat(){
return return
} }
CurrentChat.set(structuredClone(chat)) CurrentChat.set(structuredClone(chat))
} }
DataBase.subscribe((data) => { DataBase.subscribe((data) => {
updateCurrentCharacter() updateCurrentCharacter()
if(data.username !== get(CurrentUsername)){ if(data.username !== get(CurrentUsername)){
CurrentUsername.set(data.username) CurrentUsername.set(data.username)
@@ -124,14 +127,14 @@ DataBase.subscribe((data) => {
if(data.showMemoryLimit !== get(CurrentShowMemoryLimit)){ if(data.showMemoryLimit !== get(CurrentShowMemoryLimit)){
CurrentShowMemoryLimit.set(data.showMemoryLimit) CurrentShowMemoryLimit.set(data.showMemoryLimit)
} }
}) })
selectedCharID.subscribe((id) => { selectedCharID.subscribe((id) => {
updateCurrentCharacter() updateCurrentCharacter()
}) })
CurrentCharacter.subscribe((char) => { CurrentCharacter.subscribe((char) => {
updateCurrentChat() updateCurrentChat()
let db = get(DataBase) let db = get(DataBase)
let charId = get(selectedCharID) let charId = get(selectedCharID)
@@ -144,9 +147,9 @@ CurrentCharacter.subscribe((char) => {
} }
db.characters[charId] = structuredClone(char) db.characters[charId] = structuredClone(char)
DataBase.set(db) DataBase.set(db)
}) })
CurrentChat.subscribe((chat) => { CurrentChat.subscribe((chat) => {
let currentChar = get(CurrentCharacter) let currentChar = get(CurrentCharacter)
if(currentChar){ if(currentChar){
@@ -162,8 +165,10 @@ CurrentChat.subscribe((chat) => {
if(!isEqual(variablePointer, currentState)){ if(!isEqual(variablePointer, currentState)){
CurrentVariablePointer.set(currentState) CurrentVariablePointer.set(currentState)
} }
}) })
}
updateSize() updateSize()
window.addEventListener("resize", updateSize); window.addEventListener("resize", updateSize);
preInit()