Skip to content

Build your personal knowledge base with Trilium Notes

fix(client): fall back to a full reload when a froca rebuild fails

Elian Doran76c21d5d68c9
diff --git a/apps/client/src/services/ws.spec.ts b/apps/client/src/services/ws.spec.ts
index 8aeb50d465..8a2342c983 100644
--- a/apps/client/src/services/ws.spec.ts
+++ b/apps/client/src/services/ws.spec.ts
@@ -268,6 +268,28 @@ describe("frontend-update / executeFrontendUpdate", () => {
await Promise.all([ heldUpdate, rebuildDuringUpdate ]);
expect(reloadAll).toHaveBeenCalledTimes(2);
});
+
+ it("falls back to a full reload when a rebuild fails, and still applies the changes queued behind it", async () => {
+ const ws = await loadWs();
+ const { default: froca } = await import("./froca.js");
+ let failRebuild = (_error: Error) => {};
+ vi.spyOn(froca, "reloadAll")
+ .mockImplementationOnce(() => new Promise<void>((_res, rej) => { failRebuild = rej; }));
+
+ const failingRebuild = ws.dispatchMessage({ type: "reload-froca", reason: "test" } as WebSocketMessage);
+ await new Promise((res) => setTimeout(res, 0));
+ const queuedUpdate = ws.dispatchMessage({
+ type: "frontend-update", data: { entityChanges: [{ id: 401, entityName: "notes", entityId: "f1" }] }
+ } as any);
+ failRebuild(new Error("tree request failed"));
+
+ await expect(queuedUpdate).resolves.toBeUndefined();
+ expect(frocaUpdater.processEntityChanges).toHaveBeenCalledWith([
+ expect.objectContaining({ id: 401 })
+ ]);
+ await failingRebuild;
+ expect(utilsCtrl.reloadFrontendApp).toHaveBeenCalledWith(expect.stringContaining("tree request failed"));
+ });
});
describe("waitForMaxKnownEntityChangeId", () => {
diff --git a/apps/client/src/services/ws.ts b/apps/client/src/services/ws.ts
index ea855dc534..6c44147829 100644
--- a/apps/client/src/services/ws.ts
+++ b/apps/client/src/services/ws.ts
@@ -9,7 +9,7 @@ import { t } from "./i18n.js";
import options from "./options.js";
import server from "./server.js";
import toastService, { showUnhandledError } from "./toast.js";
-import utils, { isPreAuthScreen } from "./utils.js";
+import utils, { getErrorMessage, isPreAuthScreen } from "./utils.js";
type MessageHandler = (message: WebSocketMessage) => void;
let messageHandlers: MessageHandler[] = [];
@@ -174,11 +174,20 @@ async function executeFrontendUpdate(entityChanges: EntityChange[]) {
*/
export async function reloadFroca() {
await runInUpdateQueue(async () => {
- await froca.reloadAll();
- await appContext.triggerEvent("frocaReloaded", {});
+ try {
+ await froca.reloadAll();
+ await appContext.triggerEvent("frocaReloaded", {});
+ } catch (e: unknown) {
+ // A partly rebuilt froca is not safe to keep using.
+ utils.reloadFrontendApp(`rebuilding froca failed: ${getErrorMessage(e)}`);
+ }
});
}
+/**
+ * Runs `task` once the tasks queued before it finish. `task` must handle its own errors: a
+ * rejection would also reject every task waiting behind it, leaving their changes unapplied.
+ */
async function runInUpdateQueue(task: () => Promise<void>) {
// first wait for all the preceding consumers to finish
while (consumeQueuePromise) {