Skip to content

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

:bug: Avoid delaying document content for background mind map migration https://github.com/siyuan-note/siyuan/issues/19759

Daniel8b7f041ffb30
diff --git a/app/src/protyle/render/listMindmap/migrate.test.ts b/app/src/protyle/render/listMindmap/migrate.test.ts
index c091c7ef5d..70033433c9 100644
--- a/app/src/protyle/render/listMindmap/migrate.test.ts
+++ b/app/src/protyle/render/listMindmap/migrate.test.ts
@@ -39,10 +39,11 @@ const setup = () => {
};
const tick = () => new Promise(resolve => setImmediate(resolve));
+const mindmap = (content: string) => `<div data-type="NodeCodeBlock" data-subtype="mindmap">${content}</div>`;
test("closing the editor cancels rendering while a migration request is pending", async () => {
const {run, cancel, protyle, completions} = setup();
- run(protyle, "source", [], () => assert.fail("closed editor rendered"));
+ run(protyle, mindmap("source"), [], () => assert.fail("closed editor rendered"));
await tick();
cancel(protyle);
completions[0]({code: 0, data: {blocks: []}});
@@ -52,17 +53,47 @@ test("closing the editor cancels rendering while a migration request is pending"
test("opening a document migrates once and resumes only the newest pending render", async () => {
const {run, protyle, calls, completions} = setup();
const rendered: string[] = [];
- assert.equal(run(protyle, "old render", [], (html: string) => rendered.push(html)), true);
- assert.equal(run(protyle, "latest render", [], (html: string) => rendered.push(html)), true);
+ assert.equal(run(protyle, mindmap("old render"), [], (html: string) => rendered.push(html)), true);
+ assert.equal(run(protyle, mindmap("latest render"), [], (html: string) => rendered.push(html)), true);
await tick();
assert.equal(calls.length, 1);
completions[0]({code: 0, data: {blocks: []}});
await tick();
- assert.deepEqual(rendered, ["latest render"]);
- assert.equal(run(protyle, "undo", [], () => assert.fail("undo retriggered migration")), false);
+ assert.deepEqual(rendered, [mindmap("latest render")]);
+ assert.equal(run(protyle, mindmap("undo"), [], () => assert.fail("undo retriggered migration")), false);
assert.equal(calls.length, 1);
});
+test("ordinary documents render while the full document migration runs", async () => {
+ const {run, protyle, calls, completions} = setup();
+ assert.equal(run(protyle, "<div data-type=\"NodeParagraph\">Content</div>", [], () => assert.fail("render deferred")), false);
+ await tick();
+ assert.equal(calls.length, 1);
+ completions[0]({code: 0, data: {blocks: []}});
+ await tick();
+});
+
+test("ordinary content supersedes a pending mind map migration", async () => {
+ const {run, protyle, completions} = setup();
+ run(protyle, mindmap("old"), [], () => assert.fail("stale content rendered"));
+ await tick();
+ assert.equal(run(protyle, "<div data-type=\"NodeParagraph\">New</div>", [], () => assert.fail("render deferred")), false);
+ completions[0]({code: 0, data: {blocks: []}});
+ await tick();
+});
+
+test("a visible mind map waits for an already running full document migration", async () => {
+ const {run, protyle, calls, completions} = setup();
+ const rendered: string[] = [];
+ assert.equal(run(protyle, "ordinary content", [], () => assert.fail("ordinary content deferred")), false);
+ await tick();
+ assert.equal(run(protyle, mindmap("visible"), [], (html: string) => rendered.push(html)), true);
+ assert.equal(calls.length, 1);
+ completions[0]({code: 0, data: {blocks: []}});
+ await tick();
+ assert.deepEqual(rendered, [mindmap("visible")]);
+});
+
test("read-only, history, backlink, paging and local editors never request migration", async () => {
for (const mode of ["readonly", "editorReadonly", "disabled", "lite", "history", "backlink", "append", "before", "preview"]) {
const {run, protyle, calls, window} = setup();
@@ -87,14 +118,14 @@ test("read-only, history, backlink, paging and local editors never request migra
test("migration failure still opens source and a stale document response cannot replace the current document", async () => {
const {run, protyle, completions} = setup();
const rendered: string[] = [];
- run(protyle, "first", [], (html: string) => rendered.push(html));
+ run(protyle, mindmap("first"), [], (html: string) => rendered.push(html));
await tick();
protyle.block.rootID = "second";
- run(protyle, "second", [], (html: string) => rendered.push(html));
+ run(protyle, mindmap("second"), [], (html: string) => rendered.push(html));
await tick();
completions[0]({code: 0, data: {blocks: []}});
completions[1]({code: -1});
await tick();
- assert.deepEqual(rendered, ["second"]);
- assert.equal(run(protyle, "source", [], () => assert.fail("retry")), false);
+ assert.deepEqual(rendered, [mindmap("second")]);
+ assert.equal(run(protyle, mindmap("source"), [], () => assert.fail("retry")), false);
});
diff --git a/app/src/protyle/render/listMindmap/migrate.ts b/app/src/protyle/render/listMindmap/migrate.ts
index 4ce0e107ef..b7be77dbc4 100644
--- a/app/src/protyle/render/listMindmap/migrate.ts
+++ b/app/src/protyle/render/listMindmap/migrate.ts
@@ -43,12 +43,16 @@ export const migrateLegacyMindmapsBeforeRender = (protyle: IProtyle, html: strin
return false;
}
const previous = migrations.get(protyle);
- const callback = (blocks: {id: string, dom: string}[]) => resume(replaceLegacyMindmapHTML(html, blocks));
+ // 当前可见内容没有旧脑图时先渲染正文,后台仍迁移整篇文档中尚未加载的块。
+ const waitForMigration = html.includes("data-subtype=\"mindmap\"");
+ const callback = waitForMigration ?
+ (blocks: {id: string, dom: string}[]) => resume(replaceLegacyMindmapHTML(html, blocks)) :
+ () => {};
if (previous?.rootID === rootID) {
if (previous.pending) {
previous.resume = callback;
}
- return previous.pending;
+ return waitForMigration && previous.pending;
}
const migration: Migration = {rootID, pending: true, resume: callback};
migrations.set(protyle, migration);
@@ -74,5 +78,5 @@ export const migrateLegacyMindmapsBeforeRender = (protyle: IProtyle, html: strin
}
};
void run();
- return true;
+ return waitForMigration;
};