Skip to content

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

:bug: Release tab model resources on close https://github.com/siyuan-note/siyuan/issues/18763

Danieldfc20273e916
diff --git a/app/src/asset/index.ts b/app/src/asset/index.ts
index a79230a392..dac620e9c5 100644
--- a/app/src/asset/index.ts
+++ b/app/src/asset/index.ts
@@ -16,6 +16,7 @@ import {setStorageVal, updateHotkeyTip} from "../protyle/util/compatibility";
import type {App} from "../index";
import {clearOBG} from "../layout/dock/util";
import {getDisplayName} from "../util/pathName";
+import {PdfLoadState} from "./pdfLoadState";
export class Asset extends Model {
public path: string;
@@ -23,6 +24,7 @@ export class Asset extends Model {
private pdfId: number | string;
private pdfPage: number;
public pdfObject: any;
+ private pdfLoadState = new PdfLoadState();
constructor(options: { app: App, tab: Tab, path: string, page?: number | string }) {
super({app: options.app});
@@ -60,6 +62,9 @@ export class Asset extends Model {
fetchPost("/api/asset/getFileAnnotation", {
path: this.path + ".sya",
}, (response) => {
+ if (this.pdfLoadState.isDestroyed) {
+ return;
+ }
if (response.code !== 1) {
const config = JSON.parse(response.data.data);
if (config[this.pdfId]) {
@@ -93,6 +98,14 @@ export class Asset extends Model {
}
private render(isInit = true) {
+ if (this.pdfLoadState.isDestroyed) {
+ return;
+ }
+ this.pdfLoadState.clearPending();
+ if (!isInit && this.pdfObject) {
+ void this.pdfObject.close();
+ this.pdfObject = undefined;
+ }
const type = this.path.substr(this.path.lastIndexOf(".")).toLowerCase().split("?")[0];
// 对资源路径进行 HTML 转义后再拼入 src 属性,避免路径中包含 " 等字符导致属性逃逸引发 XSS
const src = Lute.EscapeHTMLStr(this.path.startsWith("file") ? this.path : document.getElementById("baseURL").getAttribute("href") + "/" + this.path);
@@ -104,9 +117,6 @@ export class Asset extends Model {
this.element.innerHTML = `<div class="asset"><video controls="controls" src="${src}"></video></div>`;
} else if (type === ".pdf") {
/// #if !MOBILE
- if (!isInit) {
- this.pdfObject.close();
- }
this.element.innerHTML = `<div class="pdf__outer" id="outerContainer">
<div id="sidebarContainer">
<div id="toolbarSidebar">
@@ -523,25 +533,47 @@ export class Asset extends Model {
setStorageVal(Constants.LOCAL_PDFTHEME, window.siyuan.storage[Constants.LOCAL_PDFTHEME]);
});
// 初始化完成后需等待页签是否显示设置完成,才可以判断 pdf 是否能进行渲染
- setTimeout(() => {
+ this.pdfLoadState.setTimeout(window.setTimeout(() => {
+ if (!this.pdfLoadState.consumeTimeout() || !this.element.isConnected) {
+ return;
+ }
if (this.element.clientWidth === 0) {
const observer = new MutationObserver(() => {
- this.pdfObject = webViewerLoad(this.path.startsWith("file") ? this.path : document.getElementById("baseURL").getAttribute("href") + "/" + this.path,
- this.element, this.pdfPage, this.pdfId);
- this.element.setAttribute("data-loading", "true");
- observer.disconnect();
+ if (!this.pdfLoadState.consumeObserver()) {
+ return;
+ }
+ this.loadPdf();
});
- observer.observe(this.element, {attributeFilter: ["class"]});
+ if (this.pdfLoadState.setObserver(observer)) {
+ observer.observe(this.element, {attributeFilter: ["class"]});
+ }
} else {
- this.pdfObject = webViewerLoad(this.path.startsWith("file") ? this.path : document.getElementById("baseURL").getAttribute("href") + "/" + this.path,
- this.element, this.pdfPage, this.pdfId);
- this.element.setAttribute("data-loading", "true");
+ this.loadPdf();
}
/// #if !BROWSER
setModelsHash();
/// #endif
- }, Constants.TIMEOUT_LOAD);
+ }, Constants.TIMEOUT_LOAD));
/// #endif
}
}
+
+ private loadPdf() {
+ if (this.pdfLoadState.isDestroyed || !this.element.isConnected) {
+ return;
+ }
+ this.pdfObject = webViewerLoad(this.path.startsWith("file") ? this.path : document.getElementById("baseURL").getAttribute("href") + "/" + this.path,
+ this.element, this.pdfPage, this.pdfId);
+ this.element.setAttribute("data-loading", "true");
+ }
+
+ public destroy() {
+ if (!this.pdfLoadState.destroy()) {
+ return;
+ }
+ if (this.pdfObject?.pdfLoadingTask) {
+ void this.pdfObject.pdfLoadingTask.destroy();
+ }
+ this.pdfObject = undefined;
+ }
}
diff --git a/app/src/asset/pdfLoadState.test.ts b/app/src/asset/pdfLoadState.test.ts
new file mode 100644
index 0000000000..13d801d6ad
--- /dev/null
+++ b/app/src/asset/pdfLoadState.test.ts
@@ -0,0 +1,82 @@
+import {after, before, beforeEach, describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {PdfLoadState} from "./pdfLoadState";
+
+describe("PDF load state", () => {
+ const windowDescriptor = Object.getOwnPropertyDescriptor(globalThis, "window");
+ const clearedTimeouts: number[] = [];
+
+ before(() => {
+ Object.defineProperty(globalThis, "window", {
+ configurable: true,
+ value: {
+ clearTimeout(timeout: number) {
+ clearedTimeouts.push(timeout);
+ },
+ },
+ });
+ });
+
+ beforeEach(() => {
+ clearedTimeouts.length = 0;
+ });
+
+ after(() => {
+ if (windowDescriptor) {
+ Object.defineProperty(globalThis, "window", windowDescriptor);
+ } else {
+ Reflect.deleteProperty(globalThis, "window");
+ }
+ });
+
+ it("clears pending work when destroyed", () => {
+ const state = new PdfLoadState();
+ let disconnectCount = 0;
+ state.setTimeout(7);
+ state.setObserver({
+ disconnect() {
+ disconnectCount++;
+ },
+ });
+
+ assert.equal(state.destroy(), true);
+ assert.equal(state.isDestroyed, true);
+ assert.deepEqual(clearedTimeouts, [7]);
+ assert.equal(disconnectCount, 1);
+ assert.equal(state.consumeTimeout(), false);
+ assert.equal(state.consumeObserver(), false);
+ });
+
+ it("rejects work registered after destruction", () => {
+ const state = new PdfLoadState();
+ let disconnectCount = 0;
+ state.destroy();
+
+ assert.equal(state.setTimeout(11), false);
+ assert.equal(state.setObserver({
+ disconnect() {
+ disconnectCount++;
+ },
+ }), false);
+ assert.equal(disconnectCount, 1);
+ assert.equal(clearedTimeouts.at(-1), 11);
+ });
+
+ it("replaces previously registered pending work", () => {
+ const state = new PdfLoadState();
+ let firstDisconnectCount = 0;
+ state.setTimeout(17);
+ state.setTimeout(19);
+ state.setObserver({
+ disconnect() {
+ firstDisconnectCount++;
+ },
+ });
+ state.setObserver({disconnect() {}});
+
+ assert.equal(clearedTimeouts.at(-1), 17);
+ assert.equal(firstDisconnectCount, 1);
+ assert.equal(state.consumeTimeout(), true);
+ assert.equal(state.consumeObserver(), true);
+ });
+});
diff --git a/app/src/asset/pdfLoadState.ts b/app/src/asset/pdfLoadState.ts
new file mode 100644
index 0000000000..fea7c8b5ea
--- /dev/null
+++ b/app/src/asset/pdfLoadState.ts
@@ -0,0 +1,70 @@
+interface IDisconnectable {
+ disconnect: () => void;
+}
+
+export class PdfLoadState {
+ private destroyed = false;
+ private timeout?: number;
+ private observer?: IDisconnectable;
+
+ public get isDestroyed() {
+ return this.destroyed;
+ }
+
+ public setTimeout(timeout: number) {
+ this.clearTimeout();
+ if (this.destroyed) {
+ window.clearTimeout(timeout);
+ return false;
+ }
+ this.timeout = timeout;
+ return true;
+ }
+
+ public consumeTimeout() {
+ this.timeout = undefined;
+ return !this.destroyed;
+ }
+
+ public setObserver(observer: IDisconnectable) {
+ this.clearObserver();
+ if (this.destroyed) {
+ observer.disconnect();
+ return false;
+ }
+ this.observer = observer;
+ return true;
+ }
+
+ public consumeObserver() {
+ this.clearObserver();
+ return !this.destroyed;
+ }
+
+ public clearPending() {
+ this.clearTimeout();
+ this.clearObserver();
+ }
+
+ public destroy() {
+ if (this.destroyed) {
+ return false;
+ }
+ this.destroyed = true;
+ this.clearPending();
+ return true;
+ }
+
+ private clearTimeout() {
+ if (typeof this.timeout === "undefined") {
+ return;
+ }
+ window.clearTimeout(this.timeout);
+ this.timeout = undefined;
+ }
+
+ private clearObserver() {
+ this.observer?.disconnect();
+ this.observer = undefined;
+ }
+}
diff --git a/app/src/layout/Model.ts b/app/src/layout/Model.ts
index 0613985b65..c6c93b0221 100644
--- a/app/src/layout/Model.ts
+++ b/app/src/layout/Model.ts
@@ -136,4 +136,8 @@ export class Model {
// 6:非自我应用主会话广播
}));
}
+
+ public destroy() {
+ // 子类按需释放模型持有的资源。
+ }
}
diff --git a/app/src/layout/Wnd.ts b/app/src/layout/Wnd.ts
index cbc8f863e5..1520cc8e8d 100644
--- a/app/src/layout/Wnd.ts
+++ b/app/src/layout/Wnd.ts
@@ -26,7 +26,6 @@ import {Constants} from "../constants";
import {ipcRenderer, webFrame} from "electron";
import {setModelsHash} from "../window/setHeader";
/// #endif
-import {Search} from "../search";
import {showMessage} from "../dialog/message";
import {openFileById, updatePanelByEditor} from "../editor/util";
import {scrollCenter} from "../util/highlightById";
@@ -805,23 +804,8 @@ export class Wnd {
item.destroy();
}
});
- model.destroy();
- return;
- }
- if (model instanceof Search) {
- model.destroy();
- return;
- }
- if (model instanceof Asset) {
- if (model.pdfObject && model.pdfObject.pdfLoadingTask) {
- model.pdfObject.pdfLoadingTask.destroy();
- }
- }
- if (model instanceof Custom) {
- if (model.destroy) {
- model.destroy();
- }
}
+ model.destroy();
model.send("closews", {});
}
diff --git a/app/src/layout/dock/Custom.ts b/app/src/layout/dock/Custom.ts
index e9d64cb443..18a581bed9 100644
--- a/app/src/layout/dock/Custom.ts
+++ b/app/src/layout/dock/Custom.ts
@@ -36,7 +36,9 @@ export class Custom extends Model {
this.data = options.data;
this.type = options.type;
this.init = options.init;
- this.destroy = options.destroy;
+ if (typeof options.destroy === "function") {
+ this.destroy = options.destroy;
+ }
this.beforeDestroy = options.beforeDestroy;
this.resize = options.resize;
this.update = options.update;