fix: fix memory leaking in Chat.svelte (#21962)

* fix: fix memory leaking in Chat.svelte

* chore: remove useless chatIdUnsubscriber var

* fix: fix async tick
This commit is contained in:
Shirasawa authored and GitHub committed 2026-03-01 15:04:47 -05:00
1 parent 2054ee0b73
commit 9904566513
2 files changed
+69 -66

No files matched your search

+67 -65
View File
@@ -110,10 +110,10 @@
let loading = true; let loading = true;
const eventTarget = new EventTarget(); const eventTarget = new EventTarget();
let controlPane; let controlPane: Pane | undefined;
let controlPaneComponent; let controlPaneComponent: ChatControls | undefined;
let messageInput; let messageInput: MessageInput | undefined;
let autoScroll = true; let autoScroll = true;
let processing = ''; let processing = '';
@@ -130,8 +130,6 @@
let eventConfirmationInputType = ''; let eventConfirmationInputType = '';
let eventCallback = null; let eventCallback = null;
let chatIdUnsubscriber: Unsubscriber | undefined;
let selectedModels = ['']; let selectedModels = [''];
let atSelectedModel: Model | undefined; let atSelectedModel: Model | undefined;
let selectedModelIds = []; let selectedModelIds = [];
@@ -657,24 +655,23 @@
savedModelIds(); savedModelIds();
} }
let pageSubscribe = null;
let showControlsSubscribe = null;
let selectedFolderSubscribe = null;
const stopAudio = () => { const stopAudio = () => {
try { try {
speechSynthesis.cancel(); speechSynthesis.cancel();
$audioQueue.stop(); $audioQueue?.stop();
} catch {} } catch {}
}; };
onMount(async () => { onMount(() => {
loading = true; loading = true;
console.log('mounted'); console.log('mounted');
window.addEventListener('message', onMessageHandler); window.addEventListener('message', onMessageHandler);
$socket?.on('events', chatEventHandler); $socket?.on('events', chatEventHandler);
audioQueue.set(new AudioQueue(document.getElementById('audioElement'))); $audioQueue?.destroy();
const audioQueueInstance = new AudioQueue(document.getElementById('audioElement'));
audioQueue.set(audioQueueInstance);
// Reset direct terminal enabled states — selectedTerminalId starts null on every page load // Reset direct terminal enabled states — selectedTerminalId starts null on every page load
if ($settings?.terminalServers?.some((s) => s.enabled)) { if ($settings?.terminalServers?.some((s) => s.enabled)) {
@@ -684,7 +681,7 @@
}); });
} }
pageSubscribe = page.subscribe(async (p) => { const pageSubscribe = page.subscribe(async (p) => {
if (p.url.pathname === '/') { if (p.url.pathname === '/') {
await tick(); await tick();
initNewChat(); initNewChat();
@@ -693,46 +690,12 @@
stopAudio(); stopAudio();
}); });
const storageChatInput = sessionStorage.getItem( const showControlsSubscribe = showControls.subscribe(async (value) => {
`chat-input${chatIdProp ? `-${chatIdProp}` : ''}`
);
if (!chatIdProp) {
loading = false;
await tick(); await tick();
}
if (storageChatInput) {
prompt = '';
messageInput?.setText('');
files = [];
selectedToolIds = [];
selectedFilterIds = [];
webSearchEnabled = false;
imageGenerationEnabled = false;
codeInterpreterEnabled = false;
try {
const input = JSON.parse(storageChatInput);
if (!$temporaryChatEnabled) {
messageInput?.setText(input.prompt);
files = input.files;
selectedToolIds = input.selectedToolIds;
selectedFilterIds = input.selectedFilterIds;
webSearchEnabled = input.webSearchEnabled;
imageGenerationEnabled = input.imageGenerationEnabled;
codeInterpreterEnabled = input.codeInterpreterEnabled;
}
} catch (e) {}
}
showControlsSubscribe = showControls.subscribe(async (value) => {
if (controlPane && !$mobile) { if (controlPane && !$mobile) {
try { try {
if (value) { if (value) {
controlPaneComponent.openPane(); controlPaneComponent?.openPane();
} else { } else {
controlPane.collapse(); controlPane.collapse();
} }
@@ -748,7 +711,8 @@
} }
}); });
selectedFolderSubscribe = selectedFolder.subscribe(async (folder) => { const selectedFolderSubscribe = selectedFolder.subscribe(async (folder) => {
await tick();
if ( if (
folder?.data?.model_ids && folder?.data?.model_ids &&
JSON.stringify(selectedModels) !== JSON.stringify(folder.data.model_ids) JSON.stringify(selectedModels) !== JSON.stringify(folder.data.model_ids)
@@ -759,22 +723,60 @@
} }
}); });
const chatInput = document.getElementById('chat-input'); const storageChatInput = sessionStorage.getItem(
chatInput?.focus(); `chat-input${chatIdProp ? `-${chatIdProp}` : ''}`
}); );
onDestroy(() => { const init = async () => {
try { if (!chatIdProp) {
pageSubscribe(); loading = false;
showControlsSubscribe(); await tick();
selectedFolderSubscribe(); }
chatIdUnsubscriber?.();
window.removeEventListener('message', onMessageHandler); if (storageChatInput) {
$socket?.off('events', chatEventHandler); prompt = '';
$audioQueue?.destroy(); messageInput?.setText('');
} catch (e) {
console.error(e); files = [];
} selectedToolIds = [];
selectedFilterIds = [];
webSearchEnabled = false;
imageGenerationEnabled = false;
codeInterpreterEnabled = false;
try {
const input = JSON.parse(storageChatInput);
if (!$temporaryChatEnabled) {
messageInput?.setText(input.prompt);
files = input.files;
selectedToolIds = input.selectedToolIds;
selectedFilterIds = input.selectedFilterIds;
webSearchEnabled = input.webSearchEnabled;
imageGenerationEnabled = input.imageGenerationEnabled;
codeInterpreterEnabled = input.codeInterpreterEnabled;
}
} catch (e) {}
}
const chatInput = document.getElementById('chat-input');
chatInput?.focus();
};
init();
return () => {
try {
pageSubscribe();
showControlsSubscribe();
selectedFolderSubscribe();
window.removeEventListener('message', onMessageHandler);
$socket?.off('events', chatEventHandler);
audioQueueInstance?.destroy();
audioQueue.set(null);
} catch (e) {
console.error(e);
}
};
}); });
// File upload functions // File upload functions
+2 -1
View File
@@ -3,6 +3,7 @@ import { type Writable, writable } from 'svelte/store';
import type { ModelConfig } from '$lib/apis'; import type { ModelConfig } from '$lib/apis';
import type { Banner } from '$lib/types'; import type { Banner } from '$lib/types';
import type { Socket } from 'socket.io-client'; import type { Socket } from 'socket.io-client';
import type { AudioQueue } from '$lib/utils/audio';
import emojiShortCodes from '$lib/emoji-shortcodes.json'; import emojiShortCodes from '$lib/emoji-shortcodes.json';
@@ -75,7 +76,7 @@ export const banners: Writable<Banner[]> = writable([]);
export const settings: Writable<Settings> = writable({}); export const settings: Writable<Settings> = writable({});
export const audioQueue = writable(null); export const audioQueue = writable<AudioQueue | null>(null);
export const sidebarWidth = writable(260); export const sidebarWidth = writable(260);