Skip to content

Build your personal knowledge base with Trilium Notes

fix(client): stop a failed frontend update stalling for 30s

Elian Doran0fa8efc1fdec
diff --git a/apps/client/src/services/utils.spec.ts b/apps/client/src/services/utils.spec.ts
index 9289bee026..2b67ce5701 100644
--- a/apps/client/src/services/utils.spec.ts
+++ b/apps/client/src/services/utils.spec.ts
@@ -599,10 +599,10 @@ describe("timeLimit (default export)", () => {
expect(utils.timeLimit(42 as any, 1000)).toBe(42);
});
- it("resolves when the promise settles before the limit (timer sees it as resolved)", async () => {
+ it("resolves when the promise settles before the limit", async () => {
const result = utils.timeLimit(Promise.resolve("done"), 1000);
await expect(result).resolves.toBe("done");
- // Advance past the limit so the timeout callback runs and observes `resolved === true`.
+ // Advancing past the limit checks that the timeout does not turn the result into a rejection.
await vi.advanceTimersByTimeAsync(1000);
await expect(result).resolves.toBe("done");
});
@@ -615,6 +615,14 @@ describe("timeLimit (default export)", () => {
await assertion;
});
+ it("forwards a rejection immediately instead of waiting for the limit", async () => {
+ const failing = Promise.reject(new Error("boom"));
+ const limited = utils.timeLimit(failing, 30000);
+ const assertion = expect(limited).rejects.toThrow("boom");
+ await vi.advanceTimersByTimeAsync(0);
+ await assertion;
+ });
+
it("uses a default error message when none is supplied", async () => {
const never = new Promise(() => {});
const limited = utils.timeLimit(never, 50);
diff --git a/apps/client/src/services/utils.ts b/apps/client/src/services/utils.ts
index a83eb2421a..af805443f4 100644
--- a/apps/client/src/services/utils.ts
+++ b/apps/client/src/services/utils.ts
@@ -481,19 +481,18 @@ function timeLimit<T>(promise: Promise<T>, limitMs: number, errorMessage?: strin
const error = new Error(errorMessage || `Process exceeded time limit ${limitMs}`);
return new Promise<T>((res, rej) => {
- let resolved = false;
-
- promise.then((result) => {
- resolved = true;
-
- res(result);
- });
-
- setTimeout(() => {
- if (!resolved) {
- rej(error);
+ const timer = setTimeout(() => rej(error), limitMs);
+
+ promise.then(
+ (result) => {
+ clearTimeout(timer);
+ res(result);
+ },
+ (e) => {
+ clearTimeout(timer);
+ rej(e);
}
- }, limitMs);
+ );
});
}