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
fix: refresh froca instead of reloading the frontend after consistency repairs
Elian Doranb23a3ce3cbda
diff --git a/apps/client/src/services/froca.spec.ts b/apps/client/src/services/froca.spec.tsindex f86d9fdd3b..3d64ac14d6 100644--- a/apps/client/src/services/froca.spec.ts+++ b/apps/client/src/services/froca.spec.ts@@ -56,6 +56,24 @@ describe("loadInitialTree", () => {});});+describe("reloadAll", () => {+ it("reloads the initial tree, then every note froca held outside it", async () => {+ const held = buildNote({ title: "Held outside the tree" });+ const calls: string[] = [];+ vi.spyOn(froca, "loadInitialTree").mockImplementation(async () => {+ calls.push("loadInitialTree");+ });+ vi.spyOn(froca, "reloadNotes").mockImplementation(async (noteIds) => {+ calls.push("reloadNotes");+ expect(noteIds).toContain(held.noteId);+ });++ await froca.reloadAll();++ expect(calls).toEqual([ "loadInitialTree", "reloadNotes" ]);+ });+});+describe("loadSubTree", () => {it("fetches the subtree and returns the requested note", async () => {const noteId = "subtree-target";diff --git a/apps/client/src/services/froca.ts b/apps/client/src/services/froca.tsindex d39f524771..40d24f7a59 100644--- a/apps/client/src/services/froca.ts+++ b/apps/client/src/services/froca.ts@@ -64,6 +64,17 @@ class FrocaImpl implements Froca {this.addResp(resp);}+ /**+ * Replaces everything froca holds with the backend's current state: the initial tree, then+ * the notes froca held outside it, which open tabs and widgets can still refer to.+ */+ async reloadAll() {+ const allNoteIds = Object.keys(this.notes);++ await this.loadInitialTree();+ await this.reloadNotes(allNoteIds);+ }+#clear() {this.notes = {};this.branches = {};diff --git a/apps/client/src/services/protected_session.ts b/apps/client/src/services/protected_session.tsindex 54c75af576..ea68e443f8 100644--- a/apps/client/src/services/protected_session.ts+++ b/apps/client/src/services/protected_session.ts@@ -50,15 +50,6 @@ function enterProtectedSession() {return dfd.promise();}-async function reloadData() {- const allNoteIds = Object.keys(froca.notes);-- await froca.loadInitialTree();-- // make sure that all notes used in the application are loaded, including the ones not shown in the tree- await froca.reloadNotes(allNoteIds);-}-async function setupProtectedSession(password: string) {const response = await server.post<Response>("login/protected", { password: password });@@ -72,7 +63,7 @@ async function setupProtectedSession(password: string) {ws.subscribeToMessages(async (message) => {if (message.type === "protectedSessionLogin") {- await reloadData();+ await froca.reloadAll();await appContext.triggerEvent("frocaReloaded", {});diff --git a/apps/client/src/services/ws.spec.ts b/apps/client/src/services/ws.spec.tsindex e2d797218c..790efeaeb7 100644--- a/apps/client/src/services/ws.spec.ts+++ b/apps/client/src/services/ws.spec.ts@@ -85,6 +85,14 @@ describe("dispatchMessage", () => {await ws.dispatchMessage({ type: "reload-frontend", reason: "x" } as WebSocketMessage);expect(utilsCtrl.reloadFrontendApp).toHaveBeenCalledWith(expect.stringContaining("reload"));+ const { default: froca } = await import("./froca.js");+ const reloadAllSpy = vi.spyOn(froca, "reloadAll").mockImplementation(async () => {});+ utilsCtrl.reloadFrontendApp.mockClear();+ await ws.dispatchMessage({ type: "reload-froca", reason: "consistency checks" } as WebSocketMessage);+ expect(reloadAllSpy).toHaveBeenCalledTimes(1);+ expect(appCtx.triggerEvent).toHaveBeenCalledWith("frocaReloaded", {});+ expect(utilsCtrl.reloadFrontendApp).not.toHaveBeenCalled();+await ws.dispatchMessage({ type: "sync-hash-check-failed", sectors: ["blobs/9"] } as any);expect(toast.showErrorTitleAndMessage).toHaveBeenCalledTimes(1);@@ -189,7 +197,7 @@ describe("frontend-update / executeFrontendUpdate", () => {(window as any).glob.isDev = false;await ws.dispatchMessage({ type: "frontend-update", data: { entityChanges: [{ id: 101, entityName: "notes", entityId: "nA" }] } } as any);- expect(utilsCtrl.reloadFrontendApp).toHaveBeenCalled();+ expect(utilsCtrl.reloadFrontendApp).toHaveBeenCalledWith(expect.stringContaining("boom"));});it("shows a toast instead of reloading when debug mode is enabled", async () => {diff --git a/apps/client/src/services/ws.ts b/apps/client/src/services/ws.tsindex e10e496560..5253fde396 100644--- a/apps/client/src/services/ws.ts+++ b/apps/client/src/services/ws.ts@@ -95,6 +95,10 @@ export async function dispatchMessage(message: WebSocketMessage) {}} else if (messageType === "reload-frontend") {utils.reloadFrontendApp("received request from backend to reload frontend");+ } else if (messageType === "reload-froca") {+ logInfo(`Reloading froca: ${msg.reason}`);+ await froca.reloadAll();+ await appContext.triggerEvent("frocaReloaded", {});} else if (messageType === "frontend-update") {await executeFrontendUpdate(msg.data.entityChanges);} else if (messageType === "sync-hash-check-failed") {@@ -241,8 +245,7 @@ async function consumeFrontendUpdateData() {if (!glob.isDev && !options.is("debugModeEnabled")) {// if there's an error in updating the frontend, then the easy option to recover is to reload the frontend completely-- utils.reloadFrontendApp();+ utils.reloadFrontendApp(`processing entity changes failed: ${e.message}`);} else {console.log("nonProcessedEntityChanges causing the timeout", nonProcessedEntityChanges);diff --git a/packages/commons/src/lib/ws_api.ts b/packages/commons/src/lib/ws_api.tsindex 08cfe9aedf..1abf6ac07f 100644--- a/packages/commons/src/lib/ws_api.ts+++ b/packages/commons/src/lib/ws_api.ts@@ -169,7 +169,7 @@ export type WebSocketMessage = AllTaskDefinitions | {originEntityName: string;originEntityId?: string | null;} | {- type: "reload-frontend";+ type: "reload-frontend" | "reload-froca";reason: string;} | {type: "sync-push-in-progress" | "sync-finished" | "sync-failed";diff --git a/packages/trilium-core/src/becca/becca_loader.spec.ts b/packages/trilium-core/src/becca/becca_loader.spec.tsindex 81df92ae7c..ee3e276ab9 100644--- a/packages/trilium-core/src/becca/becca_loader.spec.ts+++ b/packages/trilium-core/src/becca/becca_loader.spec.ts@@ -227,21 +227,22 @@ describe("becca_loader", () => {});describe("reload()", () => {- it("reloads becca and notifies the frontend with the supplied reason", () => {- const spy = vi.spyOn(ws, "reloadFrontend").mockImplementation(() => {});+ it("reloads becca and has the clients refresh froca with the supplied reason", () => {+ const refresh = vi.spyOn(ws, "reloadFroca").mockImplementation(() => {});+ const fullReload = vi.spyOn(ws, "reloadFrontend").mockImplementation(() => {});getContext().init(() => reload("custom reason"));expect(becca.loaded).toBe(true);- expect(spy).toHaveBeenCalledWith("custom reason");+ expect(refresh).toHaveBeenCalledWith("custom reason");+ expect(fullReload).not.toHaveBeenCalled();});it("falls back to a default reason when none is given", () => {- const spy = vi.spyOn(ws, "reloadFrontend").mockImplementation(() => {});+ const spy = vi.spyOn(ws, "reloadFroca").mockImplementation(() => {});getContext().init(() => reload(""));- // The empty reason triggers the `reason || "becca reloaded"` fallback.expect(spy).toHaveBeenCalledWith("becca reloaded");});});diff --git a/packages/trilium-core/src/becca/becca_loader.ts b/packages/trilium-core/src/becca/becca_loader.tsindex f9b997d351..610c936579 100644--- a/packages/trilium-core/src/becca/becca_loader.ts+++ b/packages/trilium-core/src/becca/becca_loader.ts@@ -94,7 +94,7 @@ const ATTRIBUTE_COLUMNS = [function reload(reason: string) {load();- ws.reloadFrontend(reason || "becca reloaded");+ ws.reloadFroca(reason || "becca reloaded");}eventService.subscribeBeccaLoader([eventService.ENTITY_CHANGE_SYNCED], ({ entityName, entityRow }) => {diff --git a/packages/trilium-core/src/services/ws.spec.ts b/packages/trilium-core/src/services/ws.spec.tsindex e7fcddfddb..7184eb4a53 100644--- a/packages/trilium-core/src/services/ws.spec.ts+++ b/packages/trilium-core/src/services/ws.spec.ts@@ -160,6 +160,7 @@ describe("ws service (real DB)", () => {ws.syncFailed();ws.syncHashCheckFailed(["blobs/9"]);ws.reloadFrontend("because");+ ws.reloadFroca("repaired");const types = sentAll.map((m) => m.type);expect(types).toEqual(expect.arrayContaining([@@ -178,6 +179,7 @@ describe("ws service (real DB)", () => {type: "sync-pull-in-progress", lastSyncedPush: 42, progress: { pulled: 3, total: 10 }});expect(sentAll.find((m) => m.type === "sync-hash-check-failed")).toMatchObject({ sectors: ["blobs/9"] });+ expect(sentAll.find((m) => m.type === "reload-froca")).toEqual({ type: "reload-froca", reason: "repaired" });});it("sends a ping frontend-update when there are no pending entity changes", () => {diff --git a/packages/trilium-core/src/services/ws.ts b/packages/trilium-core/src/services/ws.tsindex ee09337063..41fa669801 100644--- a/packages/trilium-core/src/services/ws.ts+++ b/packages/trilium-core/src/services/ws.ts@@ -214,6 +214,14 @@ function reloadFrontend(reason: string) {sendMessageToAllClients({ type: "reload-frontend", reason });}+/**+ * Has the clients reload their froca cache in place, for a change made outside the entity layer+ * that emits no entity changes for the frontend to apply.+ */+function reloadFroca(reason: string) {+ sendMessageToAllClients({ type: "reload-froca", reason });+}+function setLastSyncedPush(entityChangeId: number) {lastSyncedPush = entityChangeId;}@@ -228,5 +236,6 @@ export default {syncHashCheckFailed,sendTransactionEntityChangesToAllClients,setLastSyncedPush,- reloadFrontend+ reloadFrontend,+ reloadFroca};