Skip to content

Build your personal knowledge base with Trilium Notes

client/collections/board: improve performance

Adorian Doran3493c357893c
diff --git a/apps/client/src/widgets/collections/board/index.spec.tsx b/apps/client/src/widgets/collections/board/index.spec.tsx
index 00d6d97acf..b7161bfe8d 100644
--- a/apps/client/src/widgets/collections/board/index.spec.tsx
+++ b/apps/client/src/widgets/collections/board/index.spec.tsx
@@ -400,6 +400,82 @@ describe("Collapsed board columns", () => {
});
});
+describe("A board in a tab the reader is not looking at", () => {
+ let container: HTMLElement | undefined;
+
+ afterEach(() => {
+ saved.length = 0;
+ if (container) {
+ render(null, container);
+ container.remove();
+ container = undefined;
+ }
+ });
+
+ /**
+ * Every mounted board hears every change, so a card renamed once would redraw the board in each
+ * tab it is open in. A board nobody is looking at remembers the change instead.
+ */
+ it("does not redraw for a change while its tab is in the background", async () => {
+ const note = buildNote({
+ title: "Board",
+ "#collection": "",
+ "#viewType": "board",
+ children: [
+ { id: "one", title: "First", "#status": "To Do" },
+ { id: "two", title: "Second", "#status": "Done" }
+ ]
+ });
+
+ // The context the board belongs to, reached the way `useNoteContext` reaches it: from the
+ // nearest ancestor carrying one.
+ let active = true;
+ const host = new Component();
+ Object.assign(host, { noteContext: { isActive: () => active } });
+
+ const mountPoint = document.createElement("div");
+ container = mountPoint;
+ document.body.appendChild(mountPoint);
+
+ const draw = async () => {
+ await act(async () => {
+ render(
+ <ParentComponent.Provider value={host}>
+ <Harness
+ note={note}
+ noteIds={[ ...note.getChildNoteIds() ]}
+ initialConfig={{ columns: [ { value: "To Do" }, { value: "Done" } ] }}
+ />
+ </ParentComponent.Provider>,
+ mountPoint
+ );
+ });
+ await act(async () => { await flush(); });
+ };
+
+ const columnOf = (title: string) => [ ...mountPoint.querySelectorAll(".board-column") ]
+ .find(column => [ ...column.querySelectorAll(".board-note") ]
+ .some(card => card.textContent?.includes(title)))
+ ?.getAttribute("data-column");
+
+ await draw();
+ expect(columnOf("First")).toBe("To Do");
+
+ // The card moves column while the tab is in the background.
+ active = false;
+ for (const attribute of froca.getNoteFromCache("one")?.getAttributes() ?? []) {
+ if (attribute.name === "status") attribute.value = "Done";
+ }
+ await draw();
+ expect(columnOf("First")).toBe("To Do");
+
+ // Looked at again, it catches up with what it missed.
+ active = true;
+ await draw();
+ expect(columnOf("First")).toBe("Done");
+ });
+});
+
describe("Board column creation", () => {
let container: HTMLElement | undefined;
diff --git a/apps/client/src/widgets/collections/board/index.tsx b/apps/client/src/widgets/collections/board/index.tsx
index 3d869ac2e1..c7948f7712 100644
--- a/apps/client/src/widgets/collections/board/index.tsx
+++ b/apps/client/src/widgets/collections/board/index.tsx
@@ -19,7 +19,8 @@ import CollectionProperties from "../../note_bars/CollectionProperties";
import FormTextArea from "../../react/FormTextArea";
import FormTextBox from "../../react/FormTextBox";
import {
- useContextualShortcutHints, useNoteLabelBoolean, useNoteLabelWithDefault, useTriliumEvent
+ useContextualShortcutHints, useNoteContext, useNoteLabelBoolean, useNoteLabelWithDefault,
+ useTrackedElement, useTriliumEvent
} from "../../react/hooks";
import Icon from "../../react/Icon";
import NoteAutocomplete from "../../react/NoteAutocomplete";
@@ -197,6 +198,7 @@ const BOARD_HINTS: ShortcutHintDefinition = [
];
export default function BoardView({ note: parentNote, noteIds, viewConfig, saveConfig }: ViewModeProps<BoardViewData>) {
+ const { noteContext } = useNoteContext();
const [ statusAttributeWithPrefix ] = useNoteLabelWithDefault(parentNote, "board:groupBy", DEFAULT_GROUP_BY);
const [ includeArchived ] = useNoteLabelBoolean(parentNote, "includeArchived");
const [ inboxEnabled ] = useNoteLabelBoolean(parentNote, "enableInboxColumn");
@@ -305,6 +307,17 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC
return;
}
+ // A board in a tab the reader is not looking at draws for nobody, and every mounted board
+ // hears every change: a card renamed once redraws each of them, whichever is on screen. The
+ // change is remembered instead, and drawn once the tab is looked at again. Asked of the
+ // context rather than of the box, which is empty for a board that has not drawn yet.
+ // Only once it has drawn: a board opened straight into a background tab has to draw at
+ // least once, or there is no container to notice the tab being shown and it stays empty.
+ if (byColumn && noteContext && !noteContext.isActive()) {
+ isStale.current = true;
+ return;
+ }
+
// `getBoardData` reads notes, so refreshes can resolve out of order and one issued for the
// board the user has left can arrive after the next board's. What it has to say is about
// sources no longer on screen, the pending renames it reports as settled included.
@@ -444,6 +457,28 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC
});
}, []);
+ // Whether a change arrived while the board was in a background tab.
+ const isStale = useRef(false);
+ const latestRefresh = useRef(refresh);
+ latestRefresh.current = refresh;
+
+ // Caught up when the board is given a size again, which is what showing its tab does. Keyed on
+ // that rather than on the context becoming active: the board is drawn from what the tab switch
+ // lays out, and a size is the one signal that is certainly in by then.
+ const boardElement = useTrackedElement(containerRef);
+ useEffect(() => {
+ if (!boardElement) return;
+
+ const observer = new ResizeObserver(() => {
+ if (isStale.current && boardElement.getBoundingClientRect().width > 0) {
+ isStale.current = false;
+ latestRefresh.current();
+ }
+ });
+ observer.observe(boardElement);
+ return () => observer.disconnect();
+ }, [ boardElement ]);
+
// The board is not drawn afresh for another note, so the column opened on one would otherwise
// still be open on the next, over whatever that board stores for a column of the same name.
useEffect(() => setActiveColumn(undefined), [ parentNote ]);