Skip to content

Build your personal knowledge base with Trilium Notes

client/collections/board: fix some issues

Adorian Doranaea115713aa4
diff --git a/apps/client/src/widgets/collections/board/api.spec.ts b/apps/client/src/widgets/collections/board/api.spec.ts
index 757230fd85..2c9dcb9e74 100644
--- a/apps/client/src/widgets/collections/board/api.spec.ts
+++ b/apps/client/src/widgets/collections/board/api.spec.ts
@@ -12,7 +12,7 @@ import noteAttributeCache from "../../../services/note_attribute_cache";
import server from "../../../services/server";
import { buildNote } from "../../../test/easy-froca";
import { BoardViewData } from ".";
-import BoardApi from "./api";
+import BoardApi, { PendingColumnWrites } from "./api";
import { ColumnMap } from "./data";
import { BOARD_TEMPLATE_ID, getStatusDefinition } from "./columns";
@@ -57,7 +57,7 @@ function createApi(
const board = parentNote ?? buildNote({ title: "Board" });
const saved: BoardViewData[] = [];
const editing: (string | undefined)[] = [];
- const pendingRenames = new Map<string, string | undefined>();
+ const pending: PendingColumnWrites = { renames: new Map(), owners: new Map() };
const api = new BoardApi(
byColumn,
columns,
@@ -66,10 +66,10 @@ function createApi(
viewConfig,
(newConfig) => saved.push(newConfig),
(branchId) => editing.push(branchId),
- pendingRenames,
+ pending,
getStatusDefinition(board, statusAttribute)
);
- return { api, saved, editing, pendingRenames };
+ return { api, saved, editing, pendingRenames: pending.renames };
}
describe("BoardApi column mutations", () => {
@@ -324,6 +324,32 @@ describe("BoardApi column mutations", () => {
await running;
});
+ /**
+ * Two writes can ask for exactly the same thing, and what they leave behind is then the same
+ * record. Only the owner tells them apart, so the one that fails cannot take back the other's.
+ */
+ it("leaves an identical record made by another write alone when one fails", async () => {
+ const { api, pendingRenames } = createApi(
+ { columns: [ { value: "To Do" }, { value: "Done" } ] },
+ [ "To Do", "Done" ]
+ );
+
+ let releaseSecond = () => {};
+ vi.mocked(executeBulkActions).mockRejectedValueOnce(new Error("offline"));
+ vi.mocked(executeBulkActions).mockImplementationOnce(
+ () => new Promise<void>((resolve) => { releaseSecond = resolve; }));
+
+ // The same column, deleted twice over before either has answered.
+ const failing = api.removeColumn("Done");
+ const running = api.removeColumn("Done");
+
+ await expect(failing).rejects.toThrow("offline");
+ expect([ ...pendingRenames ]).toEqual([ [ "Done", undefined ] ]);
+
+ releaseSecond();
+ await running;
+ });
+
it("follows a rename through when the one before it has not landed yet", async () => {
const { api, pendingRenames } = createApi({ columns: [ { value: "Done" } ] }, [ "Done" ]);
diff --git a/apps/client/src/widgets/collections/board/api.ts b/apps/client/src/widgets/collections/board/api.ts
index b4969e7665..9486827293 100644
--- a/apps/client/src/widgets/collections/board/api.ts
+++ b/apps/client/src/widgets/collections/board/api.ts
@@ -15,6 +15,18 @@ import { BoardColumnData, BoardViewData } from ".";
import { type BoardStatusDefinition, canStoreColumnsInDefinition, DEFAULT_GROUP_BY } from "./columns";
import { ColumnMap } from "./data";
+/**
+ * The columns a board has in flight, and which write put each record there.
+ *
+ * The owner is what tells two writes apart, since what they leave behind need not: two readers of
+ * one board, or one reader twice over, can ask for the very same rename or deletion, and a record
+ * read only by name and value would then be given up by whichever of them failed first.
+ */
+export interface PendingColumnWrites {
+ renames: Map<string, string | undefined>;
+ owners: Map<string, object>;
+}
+
export default class BoardApi {
private isRelationMode: boolean;
@@ -30,7 +42,7 @@ export default class BoardApi {
private viewConfig: BoardViewData,
private saveConfig: (newConfig: BoardViewData) => void,
private setBranchIdToEdit: (branchId: string | undefined) => void,
- private pendingRenames: Map<string, string | undefined> = new Map(),
+ private pending: PendingColumnWrites = { renames: new Map(), owners: new Map() },
private statusDefinition?: BoardStatusDefinition
) {
this.isRelationMode = statusAttribute.startsWith("~");
@@ -110,7 +122,7 @@ export default class BoardApi {
// Add the new column to persisted data if it doesn't exist
if (columns.some(col => col.value === columnName)) return false;
- this.pendingRenames.delete(columnName);
+ this.pending.renames.delete(columnName);
this.storeColumns([ ...columns, { value: columnName } ]);
return true;
}
@@ -291,35 +303,39 @@ export default class BoardApi {
* flight, and put back those of one that has already failed.
*/
private retireColumn(oldValue: string, newValue?: string) {
- // A deletion is recorded as `undefined`, which is not the same as no record at all, so each
- // key is remembered as a value and whether it was there — before this call and after it.
+ const { renames, owners } = this.pending;
+ // Stands for this call alone, so that the undo can tell its own records from any other
+ // write's, however alike the two look from the outside.
+ const owner = {};
+
+ // A deletion is recorded as `undefined`, which is not the same as no record at all, so what
+ // was there before is remembered as a value and whether there was one.
const touched: {
key: string;
previous?: string;
wasRecorded: boolean;
- left?: string;
- leftRecorded: boolean;
+ previousOwner?: object;
}[] = [];
const write = (key: string, value: string | undefined, record: boolean) => {
touched.push({
key,
- previous: this.pendingRenames.get(key),
- wasRecorded: this.pendingRenames.has(key),
- left: record ? value : undefined,
- leftRecorded: record
+ previous: renames.get(key),
+ wasRecorded: renames.has(key),
+ previousOwner: owners.get(key)
});
if (record) {
- this.pendingRenames.set(key, value);
+ renames.set(key, value);
} else {
- this.pendingRenames.delete(key);
+ renames.delete(key);
}
+ owners.set(key, owner);
};
// A rename of a column whose own rename has not landed yet has to be followed through, or
// the old value the stale sources still carry would resolve to a name that is itself gone.
- for (const [ from, to ] of this.pendingRenames) {
+ for (const [ from, to ] of renames) {
if (to === oldValue) {
write(from, newValue, true);
}
@@ -334,16 +350,21 @@ export default class BoardApi {
}
return () => {
- for (const { key, previous, wasRecorded, left, leftRecorded } of touched.reverse()) {
- // Only what this call still owns is put back. A later rename may have taken the key
- // over on its way past, and that record belongs to a write of its own.
- if (this.pendingRenames.has(key) !== leftRecorded) continue;
- if (leftRecorded && this.pendingRenames.get(key) !== left) continue;
+ for (const { key, previous, wasRecorded, previousOwner } of touched.reverse()) {
+ // Another write has taken the key over since, and its record is not this one's to
+ // put back.
+ if (owners.get(key) !== owner) continue;
if (wasRecorded) {
- this.pendingRenames.set(key, previous);
+ renames.set(key, previous);
+ } else {
+ renames.delete(key);
+ }
+
+ if (previousOwner) {
+ owners.set(key, previousOwner);
} else {
- this.pendingRenames.delete(key);
+ owners.delete(key);
}
}
};
diff --git a/apps/client/src/widgets/collections/board/index.tsx b/apps/client/src/widgets/collections/board/index.tsx
index 655cab8e09..cd3b3b6e0e 100644
--- a/apps/client/src/widgets/collections/board/index.tsx
+++ b/apps/client/src/widgets/collections/board/index.tsx
@@ -16,7 +16,7 @@ import Icon from "../../react/Icon";
import NoteAutocomplete from "../../react/NoteAutocomplete";
import { onWheelHorizontalScroll } from "../../widget_utils";
import { ViewModeProps } from "../interface";
-import Api from "./api";
+import Api, { PendingColumnWrites } from "./api";
import BoardApi from "./api";
import { DEFAULT_GROUP_BY, getStatusDefinition } from "./columns";
import Column from "./column";
@@ -120,7 +120,10 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC
const [ definitionRevision, setDefinitionRevision ] = useState(0);
// A ref rather than state: `api` is rebuilt on every refresh, and the map has to outlive those
// instances to cover a rename (see BoardApi#retireColumn). Mutating it must not re-render.
- const pendingRenamesRef = useRef({ board: "", renames: new Map<string, string | undefined>() });
+ const pendingRenamesRef = useRef<{ board: string, writes: PendingColumnWrites }>({
+ board: "",
+ writes: { renames: new Map(), owners: new Map() }
+ });
/** Names each refresh, so one the board has moved on from is discarded rather than applied. */
const refreshSeqRef = useRef(0);
@@ -131,7 +134,10 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC
// Done while rendering, so the `api` built below is handed the map the refresh will read.
const boardIdentity = `${parentNote.noteId}|${statusAttributeWithPrefix}`;
if (pendingRenamesRef.current.board !== boardIdentity) {
- pendingRenamesRef.current = { board: boardIdentity, renames: new Map() };
+ pendingRenamesRef.current = {
+ board: boardIdentity,
+ writes: { renames: new Map(), owners: new Map() }
+ };
refreshSeqRef.current++;
}
const statusDefinition = useMemo(
@@ -140,7 +146,7 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC
const api = useMemo(() => {
return new Api(
byColumn, columns ?? [], parentNote, statusAttributeWithPrefix, viewConfig ?? {},
- saveConfig, setBranchIdToEdit, pendingRenamesRef.current.renames, statusDefinition);
+ saveConfig, setBranchIdToEdit, pendingRenamesRef.current.writes, statusDefinition);
}, [ byColumn, columns, parentNote, statusAttributeWithPrefix, viewConfig, saveConfig, setBranchIdToEdit, statusDefinition ]);
// 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.
@@ -188,12 +194,12 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC
getBoardData(
parentNote, statusAttributeWithPrefix, viewConfig ?? {}, includeArchived,
- statusDefinition?.options ?? [], pendingRenamesRef.current.renames)
+ statusDefinition?.options ?? [], pendingRenamesRef.current.writes.renames)
.then(({ byColumn, columns, newPersistedData, isInRelationMode, settledRenames }) => {
if (refreshId !== refreshSeqRef.current) return;
for (const settled of settledRenames) {
- pendingRenamesRef.current.renames.delete(settled);
+ pendingRenamesRef.current.writes.renames.delete(settled);
}
setByColumn(byColumn);