Skip to content

Build your personal knowledge base with Trilium Notes

fix(client): treat an erased entity froca holds as a deletion instead of reloading

Elian Doran4a968cdd6c84
diff --git a/apps/client/src/services/froca_updater.spec.ts b/apps/client/src/services/froca_updater.spec.ts
index caed058416..c6ac4a0b4a 100644
--- a/apps/client/src/services/froca_updater.spec.ts
+++ b/apps/client/src/services/froca_updater.spec.ts
@@ -69,13 +69,13 @@ describe("froca_updater - note changes", () => {
expect(triggerSpy).not.toHaveBeenCalled();
});
- it("triggers a full reload when an in-froca note is erased", async () => {
+ it("drops an erased in-froca note without reloading the frontend", async () => {
const note = buildNote({ title: "Erasable" });
await process([ec({ entityName: "notes", entityId: note.noteId, isErased: true })]);
- expect(reloadSpy).toHaveBeenCalledTimes(1);
- // event still fires because addNote populated the load results
- expect(triggerSpy).toHaveBeenCalledTimes(1);
- expect(note.noteId in froca.notes).toBe(true); // not deleted, early return
+ expect(reloadSpy).not.toHaveBeenCalled();
+ expect(note.noteId in froca.notes).toBe(false);
+ const loadResults = triggerSpy.mock.calls[0][1].loadResults as LoadResults;
+ expect(loadResults.getNoteIds()).toContain(note.noteId);
});
it("deletes the note from froca when marked deleted", async () => {
@@ -158,11 +158,20 @@ describe("froca_updater - note changes", () => {
});
describe("froca_updater - branch changes", () => {
- it("triggers a full reload when an in-froca branch is erased", async () => {
+ it("unlinks an erased in-froca branch and reports it as deleted", async () => {
const parent = buildNote({ title: "P", children: [{ title: "C" }] });
- const branchId = `${parent.noteId}_${parent.children[0]}`;
- await process([ec({ entityName: "branches", entityId: branchId, isErased: true })]);
- expect(reloadSpy).toHaveBeenCalledTimes(1);
+ const childId = parent.children[0];
+ const branchId = `${parent.noteId}_${childId}`;
+ await process([ec({ entityName: "branches", entityId: branchId, componentId: "comp-e", isErased: true })]);
+
+ expect(reloadSpy).not.toHaveBeenCalled();
+ expect(branchId in froca.branches).toBe(false);
+ expect(parent.children.includes(childId)).toBe(false);
+ expect(froca.notes[childId]?.parents.includes(parent.noteId)).toBe(false);
+ const loadResults = triggerSpy.mock.calls[0][1].loadResults as LoadResults;
+ expect(loadResults.getBranchRows()).toEqual([
+ expect.objectContaining({ branchId, noteId: childId, parentNoteId: parent.noteId, isDeleted: true })
+ ]);
});
it("removes a deleted branch and unlinks parent/child", async () => {
@@ -374,11 +383,18 @@ describe("froca_updater - revisions / options / etapi_tokens", () => {
});
describe("froca_updater - attribute changes", () => {
- it("triggers a full reload when an in-froca attribute is erased", async () => {
+ it("unlinks an erased in-froca attribute and reports it as deleted", async () => {
const note = buildNote({ title: "AttrNote", "#color": "red" });
const attrId = note.attributes[0];
- await process([ec({ entityName: "attributes", entityId: attrId, isErased: true })]);
- expect(reloadSpy).toHaveBeenCalledTimes(1);
+ await process([ec({ entityName: "attributes", entityId: attrId, componentId: "comp-e", isErased: true })]);
+
+ expect(reloadSpy).not.toHaveBeenCalled();
+ expect(attrId in froca.attributes).toBe(false);
+ expect(note.attributes.includes(attrId)).toBe(false);
+ const loadResults = triggerSpy.mock.calls[0][1].loadResults as LoadResults;
+ expect(loadResults.getAttributeRows()).toEqual([
+ expect.objectContaining({ attributeId: attrId, noteId: note.noteId, name: "color", isDeleted: true })
+ ]);
});
it("removes a deleted relation attribute and unlinks source + target", async () => {
@@ -514,10 +530,8 @@ describe("froca_updater - attribute changes", () => {
const note = buildNote({ title: "ErasedAttrHost", "~inherit": "target-loaded" });
const attrId = note.attributes[0];
await process([ec({ entityName: "attributes", entityId: attrId, isErased: true, entity: undefined })]);
- // The attribute IS in froca via buildNote and is erased => the early reloadFrontendApp()
- // path is taken (this is the code path actually hit by this fixture).
- expect(reloadSpy).toHaveBeenCalledTimes(1);
- // And because the ancestor scan sees entity === undefined it must NOT reload the target note.
+ expect(reloadSpy).not.toHaveBeenCalled();
+ // A removed relation needs no target, so the scan must not load "target-loaded".
expect(reloadNotesSpy).not.toHaveBeenCalled();
});
@@ -535,7 +549,7 @@ describe("froca_updater - attribute changes", () => {
});
describe("froca_updater - attachment changes", () => {
- it("triggers a full reload when an in-froca attachment is erased", async () => {
+ it("removes an erased in-froca attachment and reports it as deleted", async () => {
const note = buildNote({ title: "AttHost" });
const att = new FAttachment(froca, {
attachmentId: "att-erase",
@@ -549,9 +563,17 @@ describe("froca_updater - attachment changes", () => {
contentLength: 0
});
froca.attachments[att.attachmentId] = att;
+ note.attachments = [att];
await process([ec({ entityName: "attachments", entityId: att.attachmentId, isErased: true })]);
- expect(reloadSpy).toHaveBeenCalledTimes(1);
+
+ expect(reloadSpy).not.toHaveBeenCalled();
+ expect(att.attachmentId in froca.attachments).toBe(false);
+ expect(note.attachments).toEqual([]);
+ const loadResults = triggerSpy.mock.calls[0][1].loadResults as LoadResults;
+ expect(loadResults.getAttachmentRows()).toEqual([
+ expect.objectContaining({ attachmentId: "att-erase", ownerId: note.noteId, isDeleted: true })
+ ]);
});
it("removes a deleted attachment from its owner note", async () => {
diff --git a/apps/client/src/services/froca_updater.ts b/apps/client/src/services/froca_updater.ts
index 5fe279a64d..484c46728f 100644
--- a/apps/client/src/services/froca_updater.ts
+++ b/apps/client/src/services/froca_updater.ts
@@ -1,4 +1,4 @@
-import type { OptionNames } from "@triliumnext/commons";
+import type { AttachmentRow, OptionNames } from "@triliumnext/commons";
import appContext from "../components/app_context.js";
import FAttachment, { type FAttachmentRow } from "../entities/fattachment.js";
@@ -11,9 +11,9 @@ import { invalidateNoteImages } from "./image_urls.js";
import LoadResults from "./load_results.js";
import noteAttributeCache from "./note_attribute_cache.js";
import options from "./options.js";
-import utils from "./utils.js";
-async function processEntityChanges(entityChanges: EntityChange[]) {
+async function processEntityChanges(changes: EntityChange[]) {
+ const entityChanges = changes.map(asDeletionIfErased);
const loadResults = new LoadResults(entityChanges);
for (const ec of entityChanges) {
@@ -58,8 +58,7 @@ async function processEntityChanges(entityChanges: EntityChange[]) {
const missingNoteIds: string[] = [];
for (const { entityName, entity } of entityChanges) {
- if (!entity) {
- // if erased
+ if (!entity || entity.isDeleted) {
continue;
}
@@ -101,11 +100,6 @@ function processNoteChange(loadResults: LoadResults, ec: EntityChange) {
loadResults.addNote(ec.entityId, ec.componentId);
- if (ec.isErased && ec.entityId in froca.notes) {
- utils.reloadFrontendApp(`${ec.entityName} '${ec.entityId}' is erased, need to do complete reload.`);
- return;
- }
-
if (ec.isErased || ec.entity?.isDeleted) {
delete froca.notes[ec.entityId];
} else {
@@ -131,11 +125,6 @@ function processNoteChange(loadResults: LoadResults, ec: EntityChange) {
}
async function processBranchChange(loadResults: LoadResults, ec: EntityChange) {
- if (ec.isErased && ec.entityId in froca.branches) {
- utils.reloadFrontendApp(`${ec.entityName} '${ec.entityId}' is erased, need to do complete reload.`);
- return;
- }
-
let branch = froca.branches[ec.entityId];
if (ec.isErased || ec.entity?.isDeleted) {
@@ -232,11 +221,6 @@ function processNoteReordering(loadResults: LoadResults, ec: EntityChange) {
function processAttributeChange(loadResults: LoadResults, ec: EntityChange) {
let attribute = froca.attributes[ec.entityId];
- if (ec.isErased && ec.entityId in froca.attributes) {
- utils.reloadFrontendApp(`${ec.entityName} '${ec.entityId}' is erased, need to do complete reload.`);
- return;
- }
-
if (ec.isErased || ec.entity?.isDeleted) {
if (attribute) {
const sourceNote = froca.notes[attribute.noteId];
@@ -286,11 +270,6 @@ function processAttributeChange(loadResults: LoadResults, ec: EntityChange) {
}
function processAttachment(loadResults: LoadResults, ec: EntityChange) {
- if (ec.isErased && ec.entityId in froca.attachments) {
- utils.reloadFrontendApp(`${ec.entityName} '${ec.entityId}' is erased, need to do complete reload.`);
- return;
- }
-
const attachment = froca.attachments[ec.entityId];
const attachmentEntity = ec.entity as FAttachmentRow;
@@ -327,6 +306,53 @@ function processAttachment(loadResults: LoadResults, ec: EntityChange) {
loadResults.addAttachmentRow(attachmentEntity, ec.componentId);
}
+/**
+ * Turns the erasure of an entity froca holds into a deletion of froca's copy of its row. An erase
+ * carries no row, and `LoadResults` and the widgets reading it need one to know what was removed.
+ */
+function asDeletionIfErased(ec: EntityChange): EntityChange {
+ if (!ec.isErased) {
+ return ec;
+ }
+
+ const row = getFrocaRow(ec);
+ return row ? { ...ec, entity: { ...row, isDeleted: true } } : ec;
+}
+
+function getFrocaRow({ entityName, entityId }: EntityChange) {
+ if (entityName === "notes") {
+ const note = froca.notes[entityId];
+ return note && {
+ noteId: note.noteId, title: note.title, isProtected: note.isProtected,
+ type: note.type, mime: note.mime, blobId: note.blobId
+ } satisfies FNoteRow;
+ } else if (entityName === "branches") {
+ const branch = froca.branches[entityId];
+ return branch && {
+ branchId: branch.branchId, noteId: branch.noteId, parentNoteId: branch.parentNoteId,
+ notePosition: branch.notePosition, prefix: branch.prefix,
+ isExpanded: branch.isExpanded, fromSearchNote: branch.fromSearchNote
+ } satisfies FBranchRow;
+ } else if (entityName === "attributes") {
+ const attribute = froca.attributes[entityId];
+ return attribute && {
+ attributeId: attribute.attributeId, noteId: attribute.noteId, type: attribute.type,
+ name: attribute.name, value: attribute.value, position: attribute.position,
+ isInheritable: attribute.isInheritable
+ } satisfies FAttributeRow;
+ } else if (entityName === "attachments") {
+ const attachment = froca.attachments[entityId];
+ return attachment && {
+ attachmentId: attachment.attachmentId, ownerId: attachment.ownerId,
+ role: attachment.role, mime: attachment.mime, title: attachment.title,
+ utcDateModified: attachment.utcDateModified,
+ utcDateScheduledForErasureSince: attachment.utcDateScheduledForErasureSince,
+ contentLength: attachment.contentLength
+ } satisfies AttachmentRow;
+ }
+ return undefined;
+}
+
/**
* Returns the notes whose image might have changed: a note's image is its content or one of its
* attachments, such as `canvas-export.svg`. An erased attachment arrives without its row.