Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:zap: Dispose AI settings polling when panels are removed https://github.com/siyuan-note/siyuan/issues/20273

Daniele692b574c75d
diff --git a/app/src/config/tabs/ai/aiUi.ts b/app/src/config/tabs/ai/aiUi.ts
index 6175ca0689..a416021301 100644
--- a/app/src/config/tabs/ai/aiUi.ts
+++ b/app/src/config/tabs/ai/aiUi.ts
@@ -8,6 +8,7 @@ import {escapeHtmlTextAndAttr, escapeHtml} from "../../../util/escape";
import {aiConfigApi} from "./aiRuntime";
import {openByMobile} from "../../../editor/openLink";
import {canOpenExternalURL} from "../../../util/hostCapabilities";
+import {startMountedPolling} from "./mountedPolling";
/// #if !BROWSER
import {shell} from "electron";
/// #endif
@@ -160,17 +161,7 @@ export const mountEmbeddingStatsBlock = (root: HTMLElement) => {
});
});
- render();
- const timer = window.setInterval(render, 3000);
- // block 从 DOM 移除(设置页关闭/切换)时清理定时器,避免内存泄漏
- const cleanup = () => {
- if (!document.contains(block)) {
- window.clearInterval(timer);
- return;
- }
- window.requestAnimationFrame(cleanup);
- };
- window.requestAnimationFrame(cleanup);
+ startMountedPolling(block, render);
};
const mountModelTestButton = (root: HTMLElement, inputId: string, buttonId: string,
@@ -505,17 +496,7 @@ export const mountMcpServersBlock = (root: HTMLElement) => {
}
});
};
- renderMcpStatus();
- const statusTimer = window.setInterval(renderMcpStatus, 3000);
- // 设置页关闭/切换时清理定时器,避免内存泄漏(与 embedding 轮询清理模式一致)。
- const cleanupStatus = () => {
- if (!document.contains(block)) {
- window.clearInterval(statusTimer);
- return;
- }
- window.requestAnimationFrame(cleanupStatus);
- };
- window.requestAnimationFrame(cleanupStatus);
+ startMountedPolling(block, renderMcpStatus);
const getMcpServerName = (el: HTMLElement): string | undefined => {
return el.closest<HTMLElement>("[data-mcp-server-name]")?.dataset.mcpServerName;
diff --git a/app/src/config/tabs/ai/mountedPolling.test.ts b/app/src/config/tabs/ai/mountedPolling.test.ts
new file mode 100644
index 0000000000..bf6db69667
--- /dev/null
+++ b/app/src/config/tabs/ai/mountedPolling.test.ts
@@ -0,0 +1,43 @@
+import {test} from "node:test";
+import * as assert from "node:assert/strict";
+import {startMountedPolling} from "./mountedPolling";
+
+test("polling stops on removal and replacing a mount disposes the previous timer", (t) => {
+ const callbacks: (() => void)[] = [];
+ const cleared: number[] = [];
+ const disconnected: number[] = [];
+ let rendered = 0;
+ let nextTimer = 0;
+ const values = {
+ window: {setInterval: () => ++nextTimer, clearInterval: (id: number) => cleared.push(id)},
+ document: {body: {}},
+ MutationObserver: class {
+ private id: number;
+ constructor(callback: () => void) {
+ this.id = callbacks.push(callback);
+ }
+ observe() {}
+ disconnect() { disconnected.push(this.id); }
+ },
+ };
+ for (const [name, value] of Object.entries(values)) {
+ const original = Object.getOwnPropertyDescriptor(globalThis, name);
+ Object.defineProperty(globalThis, name, {value, configurable: true});
+ t.after(() => {
+ if (original) Object.defineProperty(globalThis, name, original);
+ else Reflect.deleteProperty(globalThis, name);
+ });
+ }
+ const block = {isConnected: true} as Element;
+ startMountedPolling(block, () => rendered++);
+ callbacks[0]();
+ assert.deepEqual(cleared, []);
+ startMountedPolling(block, () => rendered++);
+ assert.equal(rendered, 2);
+ assert.deepEqual(cleared, [1]);
+ assert.deepEqual(disconnected, [1]);
+ Object.assign(block, {isConnected: false});
+ callbacks[1]();
+ assert.deepEqual(cleared, [1, 2]);
+ assert.deepEqual(disconnected, [1, 2]);
+});
diff --git a/app/src/config/tabs/ai/mountedPolling.ts b/app/src/config/tabs/ai/mountedPolling.ts
new file mode 100644
index 0000000000..63e5a06eea
--- /dev/null
+++ b/app/src/config/tabs/ai/mountedPolling.ts
@@ -0,0 +1,20 @@
+const cleanups = new WeakMap<Element, () => void>();
+
+// 重复挂载或面板移除时清理轮询和观察器。
+export const startMountedPolling = (block: Element, render: () => void, interval = 3000) => {
+ cleanups.get(block)?.();
+ const timer = window.setInterval(render, interval);
+ const cleanup = () => {
+ window.clearInterval(timer);
+ observer.disconnect();
+ cleanups.delete(block);
+ };
+ const observer = new MutationObserver(() => {
+ if (!block.isConnected) {
+ cleanup();
+ }
+ });
+ observer.observe(document.body, {childList: true, subtree: true});
+ cleanups.set(block, cleanup);
+ render();
+};