Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/collections/board: improve performance
Adorian Doran9af7a59ce208
diff --git a/apps/client/src/widgets/collections/board/api.spec.ts b/apps/client/src/widgets/collections/board/api.spec.tsindex 8f9041ecc0..56d866e1bd 100644--- a/apps/client/src/widgets/collections/board/api.spec.ts+++ b/apps/client/src/widgets/collections/board/api.spec.ts@@ -631,11 +631,18 @@ describe("BoardApi card operations", () => {note: created, branch: { branchId: "createdBranch" }} as never);+ // The column goes in with the note. A write of its own would refresh the whole board a+ // second time, which is what adding a card costs on a board of any size.await api.createNewItem("Done", "Created");- expect(put).toHaveBeenCalledWith(- "notes/" + created.noteId + "/set-attribute",- expect.objectContaining({ name: "status", value: "Done" }),- undefined);+ expect(note_create.createNote).toHaveBeenCalledWith(+ expect.any(String),+ expect.objectContaining({+ title: "Created",+ attributes: [ expect.objectContaining({+ type: "label", name: "status", value: "Done"+ }) ]+ }));+ expect(put).not.toHaveBeenCalled();// The board has already drawn the card, so a failure here is logged rather than thrown.const logged = vi.spyOn(console, "error").mockImplementation(() => {});diff --git a/apps/client/src/widgets/collections/board/api.ts b/apps/client/src/widgets/collections/board/api.tsindex df349e72ed..03df7e6808 100644--- a/apps/client/src/widgets/collections/board/api.ts+++ b/apps/client/src/widgets/collections/board/api.ts@@ -95,18 +95,24 @@ export default class BoardApi {private sentToColumnEnd = new Map<string, string>();statusAttribute: string;+ /** The config as the board last handed it over, against which a fresh one is recognised. */+ private viewConfigSource: BoardViewData | undefined;+ private viewConfig: BoardViewData;+constructor(private byColumn: ColumnMap | undefined,public columns: string[],private parentNote: FNote,statusAttribute: string,- private viewConfig: BoardViewData,+ viewConfig: BoardViewData | undefined,private saveConfig: (newConfig: BoardViewData) => void,private setBranchIdToEdit: (branchId: string | undefined) => void,private pending: PendingColumnWrites ={ renames: new Map(), claims: new Map(), inFlight: 0 },private statusDefinition?: BoardStatusDefinition) {+ this.viewConfigSource = viewConfig;+ this.viewConfig = viewConfig ?? {};this.isRelationMode = statusAttribute.startsWith("~");if (statusAttribute.startsWith("~") || statusAttribute.startsWith("#")) {@@ -115,23 +121,72 @@ export default class BoardApi {this.statusAttribute = statusAttribute;};+ /**+ * Points the api at the board as it now stands.+ *+ * A refresh calls this instead of building a new api, so that the object every card holds keeps+ * its identity and a move redraws only the cards whose position changed. What the api works out+ * for itself, such as {@link sentToColumnEnd}, is kept.+ */+ update(+ byColumn: ColumnMap | undefined,+ columns: string[],+ parentNote: FNote,+ statusAttribute: string,+ viewConfig: BoardViewData | undefined,+ saveConfig: (newConfig: BoardViewData) => void,+ setBranchIdToEdit: (branchId: string | undefined) => void,+ statusDefinition?: BoardStatusDefinition+ ) {+ this.byColumn = byColumn;+ this.columns = columns;+ this.parentNote = parentNote;+ // Only a config the board has actually replaced, since `storeColumns` moves this one ahead+ // of what the board holds and an unrelated render must not take that back.+ if (viewConfig !== this.viewConfigSource) {+ this.viewConfigSource = viewConfig;+ this.viewConfig = viewConfig ?? {};+ }+ this.saveConfig = saveConfig;+ this.setBranchIdToEdit = setBranchIdToEdit;+ this.statusDefinition = statusDefinition;+ this.isRelationMode = statusAttribute.startsWith("~");+ this.statusAttribute = statusAttribute.replace(/^[~#]/, "");+ }+async createNewItem(column: string, title: string) {try {- // Create a new note as a child of the parent note- const { note: newNote, branch: newBranch } = await note_create.createNote(this.parentNote.noteId, {+ await note_create.createNote(this.parentNote.noteId, {activate: false,title,- isProtected: this.parentNote.isProtected+ isProtected: this.parentNote.isProtected,+ attributes: this.groupingFor(column)});-- if (newNote && newBranch) {- await this.changeColumn(newNote.noteId, column);- }} catch (error) {console.error("Failed to create new item:", error);}}+ /**+ * What a new card carries so that it lands in the column it was added to.+ *+ * Written with the note rather than after it: a write of its own is a second round trip and a+ * second refresh, which on a large board is most of what adding a card costs. The inbox is the+ * column held by having no value at all, so it carries nothing.+ */+ private groupingFor(column: string) {+ if (column === INBOX_COLUMN) {+ return [];+ }++ return [ {+ type: this.isRelationMode ? "relation" as const : "label" as const,+ name: this.statusAttribute,+ value: column,+ isInheritable: false+ } ];+ }+/*** Puts a note that already exists into a column, cloning it onto the board unless it is already* somewhere beneath it.@@ -682,15 +737,14 @@ export default class BoardApi {activate: false,targetBranchId: relativeToBranchId,target: direction,- title: t("board_view.new-item")+ title: t("board_view.new-item"),+ attributes: this.groupingFor(column)});if (!note || !branch) {throw new Error("Failed to create note");}- const { noteId } = note;- await this.changeColumn(noteId, column);this.startEditing(branch.branchId);return note;diff --git a/apps/client/src/widgets/collections/board/card.spec.tsx b/apps/client/src/widgets/collections/board/card.spec.tsxindex 0fe61e5e1c..52a2f7cc09 100644--- a/apps/client/src/widgets/collections/board/card.spec.tsx+++ b/apps/client/src/widgets/collections/board/card.spec.tsx@@ -25,12 +25,24 @@ vi.mock("../../../menus/link_context_menu", () => ({default: { getItems: () => [], handleLinkContextMenuItem: () => {} }}));+/** Counts how often each card has drawn, which is what the memo boundary around it decides. */+const draws = vi.hoisted(() => new Map<string, number>());+vi.mock("../../attribute_widgets/UserAttributesList", () => ({+ default: ({ note }: { note: { noteId: string } }) => {+ draws.set(note.noteId, (draws.get(note.noteId) ?? 0) + 1);+ return null;+ }+}));+describe("Board card", () => {let container: HTMLElement | undefined;/** froca is module-level, so ids are kept distinct rather than reset between tests. */let idSeed = 0;- beforeEach(() => vi.restoreAllMocks());+ beforeEach(() => {+ vi.restoreAllMocks();+ draws.clear();+ });afterEach(() => {if (container) {@@ -54,6 +66,25 @@ describe("Board card", () => {expect(cardClasses(second)).not.toContain("archived");});+ /**+ * A board holds hundreds of cards, so a refresh that redrew all of them would make one card's+ * move cost the whole board. The api is what this rests on: rebuilt per refresh, as it once was,+ * it is a new prop on every card and no card can be skipped.+ */+ it("leaves the cards a refresh did not change undrawn", async () => {+ const { component, first, second } = await renderBoard();+ const before = draws.get(first);+ expect(before).toBeGreaterThan(0);++ // The last card takes a column of its own, so the first one keeps both its column and its+ // place in it while the board rebuilds its data.+ await changeStatus(component, second, "Done");++ expect(columnsOf(container)).toContain("Done");+ expect(draws.get(first)).toBe(before);+ expect(draws.get(second)).toBeGreaterThan(before ?? 0);+ });+it("follows its note's icon, which the quick edit popup sets as a label", async () => {const { component, first } = await renderBoard();@@ -197,6 +228,35 @@ describe("Board card", () => {return { note, component, first, second };}+ /** Moves a card to another column the way an edit made anywhere else reaches the board. */+ async function changeStatus(component: Component, noteId: string, value: string) {+ const attribute = (noteAttributeCache.attributes[noteId] ?? [])+ .find(candidate => candidate.name === "status");+ if (!attribute) throw new Error(`no status label on ${noteId}`);+ attribute.value = value;++ const entity = {+ attributeId: attribute.attributeId, noteId, type: "label", name: "status", value,+ isDeleted: false+ };+ const loadResults = new LoadResults([ {+ entityName: "attributes", entityId: attribute.attributeId, entity, hash: "",+ isSynced: true, isErased: false+ } ]);+ loadResults.addAttribute(attribute.attributeId, "someOtherComponent");++ await act(async () => {+ await component.handleEvent("entitiesReloaded", { loadResults });+ });+ await settle();+ }++ /** The columns the board is showing, in the order it draws them. */+ function columnsOf(root: HTMLElement | undefined) {+ return [ ...root?.querySelectorAll(".board-column") ?? [] ]+ .map(column => column.getAttribute("data-column"));+ }+/** Adds a label to a note already in froca and announces it the way a websocket message would. */async function addLabel(component: Component, noteId: string, name: string, value = "") {const attributeId = utils.randomString(12);diff --git a/apps/client/src/widgets/collections/board/card.tsx b/apps/client/src/widgets/collections/board/card.tsxindex 899aee52ba..da3881b590 100644--- a/apps/client/src/widgets/collections/board/card.tsx+++ b/apps/client/src/widgets/collections/board/card.tsx@@ -16,6 +16,7 @@ function Card({branch,column,index,+ statusAttribute,isDragging,isEditing,onFocusCard@@ -25,6 +26,11 @@ function Card({branch: FBranch,column: string,index: number,+ /**+ * The label the board groups by, so that a card is not left reading it off `api`. The api keeps+ * one identity for the life of the board, which a `memo` comparison cannot see through.+ */+ statusAttribute: string,isDragging: boolean,/*** Passed down rather than derived here from the drag state's `branchIdToEdit`, so that a card@@ -107,7 +113,7 @@ function Card({title={t("board_view.edit-note-title")}onClick={handleEdit}/>- <UserAttributesDisplay note={note} ignoredAttributes={[api.statusAttribute]} />+ <UserAttributesDisplay note={note} ignoredAttributes={[statusAttribute]} /></>) : (<TitleEditor@@ -132,7 +138,8 @@ function Card({* re-renders a context consumer whatever its memo boundary says, so subscribing there would make* the comparison below unreachable. `isEditing` and `isDragging` arrive as props for that reason.*- * Note that `api` is rebuilt whenever the board's data is, so a refresh still re-renders every card- * regardless. This bails out on the drag and edit redraws, not on those.+ * `api` keeps one identity for as long as the board is mounted, so a refresh reaches only the cards+ * whose own props changed. Anything a card reads off the api while rendering has to arrive as a prop+ * instead, which is why `statusAttribute` is one.*/export default memo(Card);diff --git a/apps/client/src/widgets/collections/board/column.tsx b/apps/client/src/widgets/collections/board/column.tsxindex 7d3376f71d..43b1c87b3f 100644--- a/apps/client/src/widgets/collections/board/column.tsx+++ b/apps/client/src/widgets/collections/board/column.tsx@@ -303,6 +303,7 @@ export default function Column({branch={branch}column={column}index={index}+ statusAttribute={api.statusAttribute}isDragging={draggedCard?.noteId === note.noteId}isEditing={branch.branchId === branchIdToEdit}onFocusCard={onFocusCard}diff --git a/apps/client/src/widgets/collections/board/index.tsx b/apps/client/src/widgets/collections/board/index.tsxindex c7948f7712..b4e584e8bc 100644--- a/apps/client/src/widgets/collections/board/index.tsx+++ b/apps/client/src/widgets/collections/board/index.tsx@@ -251,14 +251,24 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveCconst statusDefinition = useMemo(() => getStatusDefinition(parentNote, statusAttributeWithPrefix),[ parentNote, statusAttributeWithPrefix, definitionRevision ]);- const api = useMemo(() => {- return new Api(- byColumn, usableColumns, parentNote, statusAttributeWithPrefix, viewConfig ?? {},- saveConfig, setBranchIdToEdit, pendingRenamesRef.current.writes, statusDefinition);- }, [- byColumn, usableColumns, parentNote, statusAttributeWithPrefix, viewConfig,- saveConfig, setBranchIdToEdit, statusDefinition- ]);+ // One api for as long as the board is shown, pointed at each refresh's data rather than built+ // again: a new object would be a new prop on every card, and `memo` would then redraw all of+ // them for a move that touched one. Another board takes a new one, since this instance is+ // reused across boards and the api holds that board's record of the writes in flight.+ const apiRef = useRef<{ board: string, api: Api }>();+ if (!apiRef.current || apiRef.current.board !== boardIdentity) {+ apiRef.current = {+ board: boardIdentity,+ api: new Api(+ byColumn, usableColumns, parentNote, statusAttributeWithPrefix, viewConfig,+ saveConfig, setBranchIdToEdit, pendingRenamesRef.current.writes, statusDefinition)+ };+ } else {+ apiRef.current.api.update(+ byColumn, usableColumns, parentNote, statusAttributeWithPrefix, viewConfig,+ saveConfig, setBranchIdToEdit, statusDefinition);+ }+ const api = apiRef.current.api;// Every member is one of useState's own setters, so this value is built once and never changes// identity -- a drag cannot reach anything that reads only this.const boardActions = useMemo<BoardActions>(() => ({diff --git a/apps/client/vite.config.mts b/apps/client/vite.config.mtsindex 52ecf45853..bf7b237c7a 100644--- a/apps/client/vite.config.mts+++ b/apps/client/vite.config.mts@@ -13,9 +13,11 @@ const isDev = process.env.NODE_ENV === "development";let plugins: any = [];if (isDev) {- // Add Prefresh for Preact HMR in developmentplugins = [- prefresh(),+ // Prefresh keeps a growing list of vnodes per component type and scans it on every diff, so+ // a view with thousands of instances of one component slows to a stop. Set TRILIUM_NO_HMR to+ // work on such a view; components then reload with the page instead of in place.+ ...(process.env.TRILIUM_NO_HMR ? [] : [ prefresh() ]),stripUniverHyphenation()];} else {