Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

perf(board): bound progress snapshot work during pagination

Tind96566c1ebf1
diff --git a/apps/web/src/fetchers/task/load-board-pages.test.ts b/apps/web/src/fetchers/task/load-board-pages.test.ts
index fd2b4c7a6..199ddc5dd 100644
--- a/apps/web/src/fetchers/task/load-board-pages.test.ts
+++ b/apps/web/src/fetchers/task/load-board-pages.test.ts
@@ -219,3 +219,43 @@ describe("complete board loading through bounded pages", () => {
expect(result.columns[1].tasks[0].id).toBe("late");
});
});
+
+it("keeps progress cloning linear across large task and related-page loads", async () => {
+ const snapshots: ProjectWithTasks[] = [];
+ const pages = 128;
+ const relatedPages = 8;
+ const result = await loadBoardPages(
+ async (number, related = 1) => {
+ const next = page(number, pages);
+ next.data.columns[0].tasks[0].labels = [
+ { id: `label-${number}-${related}`, name: "Label", color: "red" },
+ ];
+ return {
+ ...next,
+ pagination: { ...next.pagination, relatedTotalPages: relatedPages },
+ };
+ },
+ undefined,
+ (snapshot) => snapshots.push(snapshot),
+ );
+ const weight = (snapshot: ProjectWithTasks) =>
+ [
+ ...snapshot.columns.flatMap((column) => column.tasks),
+ ...snapshot.plannedTasks,
+ ...snapshot.archivedTasks,
+ ].reduce(
+ (count, task) =>
+ count +
+ 1 +
+ (task.labels?.length ?? 0) +
+ (task.externalLinks?.length ?? 0),
+ 0,
+ );
+ expect(snapshots.length).toBeLessThan(16);
+ expect(
+ snapshots.reduce((count, snapshot) => count + weight(snapshot), 0),
+ ).toBeLessThan(weight(result) * 4);
+ expect(snapshots[0].columns[0].tasks).toHaveLength(1);
+ expect(snapshots.at(-1)?.columns[0].tasks).toHaveLength(pages);
+ expect(result.columns[0].tasks.at(-1)?.labels).toHaveLength(relatedPages);
+});
diff --git a/apps/web/src/fetchers/task/load-board-pages.ts b/apps/web/src/fetchers/task/load-board-pages.ts
index ccfd4afc3..232b33804 100644
--- a/apps/web/src/fetchers/task/load-board-pages.ts
+++ b/apps/web/src/fetchers/task/load-board-pages.ts
@@ -20,14 +20,6 @@ export async function loadBoardPages<T extends ProjectWithTasks>(
signal?.throwIfAborted();
const first = await load(1);
const result = first.data;
- const reportProgress = () => {
- signal?.throwIfAborted();
- result.columns.sort(
- (left, right) => (left.position ?? 0) - (right.position ?? 0),
- );
- onProgress?.(structuredClone(result));
- };
- reportProgress();
const columns = new Map(result.columns.map((column) => [column.id, column]));
const seen = new Map(
[
@@ -36,13 +28,45 @@ export async function loadBoardPages<T extends ProjectWithTasks>(
...result.plannedTasks,
].map((task) => [task.id, task]),
);
+ let progressWeight =
+ result.columns.length +
+ seen.size +
+ [...seen.values()].reduce(
+ (count, task) =>
+ count + (task.labels?.length ?? 0) + (task.externalLinks?.length ?? 0),
+ 0,
+ );
+ let nextProgressWeight = 0;
+ const reportProgress = (force = false) => {
+ signal?.throwIfAborted();
+ if (!onProgress || (!force && progressWeight < nextProgressWeight)) return;
+ result.columns.sort(
+ (left, right) => (left.position ?? 0) - (right.position ?? 0),
+ );
+ onProgress(structuredClone(result));
+ nextProgressWeight = Math.max(1, progressWeight * 2);
+ };
+ reportProgress(true);
+ const relationIndexes = new WeakMap<object[], Map<string, number>>();
const mergeById = <U extends { id: string }>(
left: U[] = [],
right: U[] = [],
- ) =>
- Array.from(
- new Map([...left, ...right].map((value) => [value.id, value])).values(),
- );
+ ) => {
+ let indexes = relationIndexes.get(left);
+ if (!indexes) {
+ indexes = new Map(left.map((value, index) => [value.id, index]));
+ relationIndexes.set(left, indexes);
+ }
+ for (const value of right) {
+ const index = indexes.get(value.id);
+ if (index === undefined) {
+ indexes.set(value.id, left.length);
+ left.push(value);
+ progressWeight++;
+ } else left[index] = value;
+ }
+ return left;
+ };
const append = (
target: ProjectWithTasks["plannedTasks"],
tasks: ProjectWithTasks["plannedTasks"],
@@ -51,6 +75,8 @@ export async function loadBoardPages<T extends ProjectWithTasks>(
if (!seen.has(task.id)) {
seen.set(task.id, task);
target.push(task);
+ progressWeight +=
+ 1 + (task.labels?.length ?? 0) + (task.externalLinks?.length ?? 0);
} else {
const existing = seen.get(task.id);
if (existing) {
@@ -70,6 +96,7 @@ export async function loadBoardPages<T extends ProjectWithTasks>(
target = { ...column, tasks: [] };
columns.set(column.id, target);
result.columns.push(target);
+ progressWeight++;
}
append(target.tasks, column.tasks);
}
@@ -98,5 +125,6 @@ export async function loadBoardPages<T extends ProjectWithTasks>(
(left, right) => (left.position ?? 0) - (right.position ?? 0),
);
signal?.throwIfAborted();
+ reportProgress(true);
return result;
}