Skip to content

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

:bug: Defer plugin API initialization to prevent mobile startup errors https://github.com/siyuan-note/siyuan/issues/19167

Daniel11f48fa6d819
diff --git a/app/src/plugin/API.test.ts b/app/src/plugin/API.test.ts
new file mode 100644
index 0000000000..d6783dac70
--- /dev/null
+++ b/app/src/plugin/API.test.ts
@@ -0,0 +1,27 @@
+import {it} from "node:test";
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {resolve} from "node:path";
+import {runInNewContext} from "node:vm";
+import * as ts from "typescript";
+
+it("defers plugin API dependency reads and shares the initialized API", () => {
+ const source = readFileSync(resolve(process.cwd(), "src/plugin/API.ts"), "utf8");
+ const compiled = ts.transpileModule(source, {
+ compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020},
+ }).outputText;
+ let dependenciesReady = false;
+ const adaptHotkey = (value: string) => value;
+ const dependency = new Proxy({}, {
+ get(_target, key) {
+ assert.ok(dependenciesReady, `Dependency ${String(key)} was read during module initialization`);
+ return key === "updateHotkeyTip" ? adaptHotkey : (): void => undefined;
+ },
+ });
+ const exports: {getAPI?: () => {adaptHotkey: typeof adaptHotkey}} = {};
+ runInNewContext(compiled, {exports, require: () => dependency});
+ dependenciesReady = true;
+ const api = exports.getAPI();
+ assert.equal(api.adaptHotkey, adaptHotkey);
+ assert.equal(exports.getAPI(), api);
+});
diff --git a/app/src/plugin/API.ts b/app/src/plugin/API.ts
index 133353e94e..49a5d59536 100644
--- a/app/src/plugin/API.ts
+++ b/app/src/plugin/API.ts
@@ -39,8 +39,8 @@ import {adjustEditorFontSize, setEditorFontSize} from "../util/editorFontSize";
import {isDockPanelVisible, toggleDockPanel} from "../layout/dock/panel";
/// #endif
-let openTab;
-let openWindow;
+let openTab: (options: any) => any;
+let openWindow: (options: any) => void;
/// #if MOBILE
openTab = () => {
// TODO: Mobile
@@ -393,7 +393,7 @@ const isBottomDockVisible = () => {
/// #endif
};
-export const API = {
+const createAPI = () => ({
adaptHotkey: updateHotkeyTip,
confirm: confirmDialog,
Constants,
@@ -439,4 +439,14 @@ export const API = {
isLeftDockVisible,
isRightDockVisible,
isBottomDockVisible,
+});
+
+let api: ReturnType<typeof createAPI>;
+
+export const getAPI = () => {
+ // 在插件首次请求接口时初始化,避免循环依赖读取尚未初始化的模块成员。
+ if (!api) {
+ api = createAPI();
+ }
+ return api;
};
diff --git a/app/src/plugin/loader.ts b/app/src/plugin/loader.ts
index 421ccef53a..e6b0d47dcd 100644
--- a/app/src/plugin/loader.ts
+++ b/app/src/plugin/loader.ts
@@ -10,7 +10,7 @@ import {
removeMobilePluginDock,
} from "../mobile/dock/pluginDockState";
/// #endif
-import {API} from "./API";
+import {getAPI} from "./API";
import {getFrontend, isMobile, isWindow} from "../util/functions";
import {Constants} from "../constants";
import {beginPluginTeardown, destroyPlugin} from "./uninstall";
@@ -30,12 +30,10 @@ import {
import {getHostCapabilities} from "../util/hostCapabilities";
const requireFunc = (key: string) => {
- const modules = {
- siyuan: API
- };
- // @ts-ignore
- return modules[key]
- ?? window.require?.(key);
+ if (key === "siyuan") {
+ return getAPI();
+ }
+ return window.require?.(key);
};
if (window.require instanceof Function) {
requireFunc.__proto__ = window.require;