Skip to content

Build your personal knowledge base with Trilium Notes

perf(collections/table): load nested rows one level at a time

Elian Dorand9bc85224936
diff --git a/apps/client/src/widgets/collections/table/rows.spec.ts b/apps/client/src/widgets/collections/table/rows.spec.ts
index 388d84d90f..97413430b4 100644
--- a/apps/client/src/widgets/collections/table/rows.spec.ts
+++ b/apps/client/src/widgets/collections/table/rows.spec.ts
@@ -204,6 +204,42 @@ describe("buildRowDefinitions", () => {
]);
post.mockRestore();
});
+
+ it("loads the notes it is missing one level at a time, not one note at a time", async () => {
+ const parent = buildNote({
+ title: "Collection",
+ children: [ "A", "B", "C" ].map((title) => ({
+ title,
+ children: [ 1, 2 ].map((n) => ({ title: `${title}${n}`, children: [ { title: `${title}${n}.x` } ] }))
+ }))
+ });
+ // Everything below the direct children is known only by its branch, as after `GET /api/tree`.
+ const unloaded = new Map<string, FNote>();
+ const childrenOf = (note: FNote) => froca.getNotesFromCache(note.children);
+ for (const child of childrenOf(parent)) {
+ for (const grandchild of childrenOf(child)) {
+ for (const note of [ grandchild, ...childrenOf(grandchild) ]) {
+ unloaded.set(note.noteId, note);
+ delete froca.notes[note.noteId];
+ }
+ }
+ }
+ const post = vi.spyOn(server, "post").mockImplementation((async (url: string, data: { noteIds: string[] }) => {
+ if (url !== "tree/load") return {};
+ for (const noteId of data.noteIds) {
+ const note = unloaded.get(noteId);
+ if (note) froca.notes[noteId] = note;
+ }
+ return { notes: [], branches: [], attributes: [] };
+ }) as unknown as typeof server.post);
+
+ const { definitions } = await buildRowDefinitions(parent, [], false);
+
+ expect(post.mock.calls.filter(([ url ]) => url === "tree/load").map(([ , data ]) => (data as { noteIds: string[] }).noteIds.length))
+ .toEqual([ 6, 6 ]);
+ expect(definitions[2]._children?.[1]._children?.[0]).toMatchObject({ title: "C2.x" });
+ post.mockRestore();
+ });
});
/** Gives the note another relation under a name it already carries, which one literal cannot. */
diff --git a/apps/client/src/widgets/collections/table/rows.ts b/apps/client/src/widgets/collections/table/rows.ts
index 8bd180956a..8853b62b66 100644
--- a/apps/client/src/widgets/collections/table/rows.ts
+++ b/apps/client/src/widgets/collections/table/rows.ts
@@ -38,6 +38,10 @@ export async function buildRowDefinitions(parentNote: FNote, infos: AttributeDef
maxDepth = 0;
}
+ if (currentDepth === 0) {
+ await loadRowNotes(parentNote, includeArchived, maxDepth);
+ }
+
for (const branch of childBranches) {
const note = await branch.getNote();
if (!note || (!includeArchived && note.isArchived)) {
@@ -98,6 +102,20 @@ export async function buildRowDefinitions(parentNote: FNote, infos: AttributeDef
};
}
+/**
+ * Loads every note `buildRowDefinitions()` visits, one request per level of the tree. The initial
+ * tree holds only the expanded branches, so a note met one `branch.getNote()` at a time otherwise
+ * costs a `tree/load` request of its own.
+ */
+async function loadRowNotes(parentNote: FNote, includeArchived: boolean, maxDepth: number) {
+ let parents = [ parentNote ];
+ for (let depth = 0; parents.length > 0; depth++) {
+ const notes = await froca.getNotes(parents.flatMap((note) => note.children), true);
+ parents = notes.filter((note) => (includeArchived || !note.isArchived)
+ && note.hasChildren() && (maxDepth < 0 || depth < maxDepth));
+ }
+}
+
/**
* Fills each row's `relationTitles` with the titles its relation cells show, loading the targets
* as one batch. A relation can point at a note since deleted, or one the user may not see — such