Skip to content

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

:bug: Harden file tree initialization after loading code snippets https://github.com/siyuan-note/siyuan/issues/14154

Daniel95b463895924
diff --git a/app/src/boot/onGetConfig.ts b/app/src/boot/onGetConfig.ts
index 357e16bd4e..b14e7a30b6 100644
--- a/app/src/boot/onGetConfig.ts
+++ b/app/src/boot/onGetConfig.ts
@@ -57,7 +57,7 @@ export const onGetConfig = (isStart: boolean, app: App) => {
/// #endif
ensureUILayout();
initWindowEvent(app);
- const snippetReady = renderSnippet();
+ const snippetReady = renderSnippet(Constants.TIMEOUT_SNIPPET_LOAD);
fetchPost("/api/system/getEmojiConf", {}, response => {
window.siyuan.emojis = response.data as IEmoji[];
snippetReady.then(() => {
diff --git a/app/src/config/util/snippets.ts b/app/src/config/util/snippets.ts
index 0e0f7e7668..4fb03820cb 100644
--- a/app/src/config/util/snippets.ts
+++ b/app/src/config/util/snippets.ts
@@ -5,7 +5,11 @@ import {confirmDialog} from "../../dialog/confirmDialog";
import {Constants} from "../../constants";
import {refreshHeadingNumberMeasurements} from "../../util/assets";
-export const renderSnippet = () => {
+export const renderSnippet = (timeout = 0) => {
+ const abortController = timeout > 0 ? new AbortController() : undefined;
+ const timeoutId = abortController ? window.setTimeout(() => {
+ abortController.abort();
+ }, timeout) : 0;
return fetchPost("/api/snippet/getSnippet", {type: "all", enabled: 2}, (response) => {
let cssChanged = false;
response.data.snippets.forEach((item: ISnippet) => {
@@ -50,6 +54,8 @@ export const renderSnippet = () => {
if (cssChanged) {
refreshHeadingNumberMeasurements();
}
+ }, undefined, undefined, abortController?.signal).finally(() => {
+ window.clearTimeout(timeoutId);
});
};
diff --git a/app/src/constants.ts b/app/src/constants.ts
index 61693624dd..d36b8e3a27 100644
--- a/app/src/constants.ts
+++ b/app/src/constants.ts
@@ -318,6 +318,7 @@ export abstract class Constants {
public static readonly TIMEOUT_RESIZE = 200;
public static readonly TIMEOUT_INPUT = 256;
public static readonly TIMEOUT_LOAD = 300;
+ public static readonly TIMEOUT_SNIPPET_LOAD = 5000;
public static readonly TIMEOUT_LONGPRESS = 460;
public static readonly TIMEOUT_VIBRATION_DURATION = 20;
// 鼠标拖拽启动的短暂时间下限:平板鼠标合成 touch 拖拽文件树/画廊/列表操作等元素时,按下后需经过该时间才进拖拽,避免点击 + 号/箭头时抖动误触发 dragstart
diff --git a/app/src/mobile/util/initFramework.ts b/app/src/mobile/util/initFramework.ts
index fc606f5f8a..e9b32965b3 100644
--- a/app/src/mobile/util/initFramework.ts
+++ b/app/src/mobile/util/initFramework.ts
@@ -62,7 +62,7 @@ const openDockMenu = (app: App) => {
export const initFramework = async (app: App, isStart: boolean) => {
setInlineStyle();
- const snippetReady = renderSnippet();
+ const snippetReady = renderSnippet(Constants.TIMEOUT_SNIPPET_LOAD);
initKeyboardToolbar();
const sidebarElement = document.getElementById("sidebar");
// 不能使用 getEventName,否则点击返回会展开右侧栏
diff --git a/app/src/util/reloadSync.ts b/app/src/util/reloadSync.ts
index 1d24a234ca..35bd8981e3 100644
--- a/app/src/util/reloadSync.ts
+++ b/app/src/util/reloadSync.ts
@@ -54,7 +54,7 @@ export const reloadSync = (
}
}
setNoteBook(() => {
- window.siyuan.mobile.docks.file.init(false);
+ window.siyuan.mobile.docks.file?.init(false);
});
/// #else
const allModels = getAllModels();