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: fix some issues
Adorian Doran40ec3a7e8963
diff --git a/apps/client/src/widgets/collections/board/api.spec.ts b/apps/client/src/widgets/collections/board/api.spec.tsindex 1d5e56ef97..c9164df5c1 100644--- a/apps/client/src/widgets/collections/board/api.spec.ts+++ b/apps/client/src/widgets/collections/board/api.spec.ts@@ -288,6 +288,29 @@ describe("BoardApi column mutations", () => {await running;});+ /**+ * The later rename re-points the record the earlier one left, taking the key over. An undo that+ * reverted it anyway would strip a rename still running of the record it needs.+ */+ it("leaves a key a later rename has taken over alone when the earlier one fails", async () => {+ const { api, pendingRenames } = createApi({ columns: [ { value: "Done" } ] }, [ "Done" ]);++ let releaseSecond = () => {};+ vi.mocked(executeBulkActions).mockRejectedValueOnce(new Error("offline"));+ vi.mocked(executeBulkActions).mockImplementationOnce(+ () => new Promise<void>((resolve) => { releaseSecond = resolve; }));++ const failing = api.renameColumn("Done", "Shipped");+ const running = api.renameColumn("Shipped", "Delivered");++ await expect(failing).rejects.toThrow("offline");+ expect([ ...pendingRenames ])+ .toEqual([ [ "Done", "Delivered" ], [ "Shipped", "Delivered" ] ]);++ 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.tsindex a9a9cf4bf9..ef6bc26d2c 100644--- a/apps/client/src/widgets/collections/board/api.ts+++ b/apps/client/src/widgets/collections/board/api.ts@@ -289,35 +289,55 @@ export default class BoardApi {* flight, and put back those of one that has already failed.*/private retireColumn(oldValue: string, newValue?: string) {- const touched: { key: string, previous?: string, wasRecorded: boolean }[] = [];- const remember = (key: string) => touched.push({- key,- previous: this.pendingRenames.get(key),- // A deletion is recorded as `undefined`, which is not the same as no record at all.- wasRecorded: this.pendingRenames.has(key)- });+ // 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 touched: {+ key: string;+ previous?: string;+ wasRecorded: boolean;+ left?: string;+ leftRecorded: boolean;+ }[] = [];++ 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+ });++ if (record) {+ this.pendingRenames.set(key, value);+ } else {+ this.pendingRenames.delete(key);+ }+ };// 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) {if (to === oldValue) {- remember(from);- this.pendingRenames.set(from, newValue);+ write(from, newValue, true);}}- remember(oldValue);- this.pendingRenames.set(oldValue, newValue);+ write(oldValue, newValue, true);if (newValue) {// Covers a rename back to a name still pending, whose record the loop above just turned// into one mapping the name to itself.- remember(newValue);- this.pendingRenames.delete(newValue);+ write(newValue, undefined, false);}return () => {- for (const { key, previous, wasRecorded } of touched.reverse()) {+ 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;+if (wasRecorded) {this.pendingRenames.set(key, previous);} else {