From 34bb27d515212533f4ccf6b5c0c3721fe59355af Mon Sep 17 00:00:00 2001 From: priyanshu1976 Date: Sat, 29 Aug 2026 10:22:50 +0530 Subject: [PATCH] fix: clear timeout ids from array in clearTimeouts Empties the timeouts array after clearing so outOfFocusTimeouts no longer grows unboundedly across a session. Closes #8366 Co-Authored-By: Claude Fable 5 --- frontend/__tests__/utils/misc.spec.ts | 16 ++++++++++++++++ frontend/src/ts/utils/misc.ts | 6 ++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/frontend/__tests__/utils/misc.spec.ts b/frontend/__tests__/utils/misc.spec.ts index 84f6c10d258a..1db9ef4ab5a7 100644 --- a/frontend/__tests__/utils/misc.spec.ts +++ b/frontend/__tests__/utils/misc.spec.ts @@ -3,6 +3,7 @@ import { isObject, escapeHTML, promiseWithResolvers, + clearTimeouts, } from "../../src/ts/utils/misc"; import { getLanguageDisplayString, @@ -224,6 +225,21 @@ describe("misc.ts", () => { }); }); + describe("clearTimeouts", () => { + it("should clear timeouts and empty the array", () => { + //GIVEN + const spy = vi.fn(); + const timeouts = [setTimeout(spy, 1000), setTimeout(spy, 1000)]; + + //WHEN + clearTimeouts(timeouts); + + //THEN + expect(timeouts).toHaveLength(0); + expect(spy).not.toHaveBeenCalled(); + }); + }); + describe("promiseWithResolvers", () => { it("should resolve the promise from outside", async () => { //GIVEN diff --git a/frontend/src/ts/utils/misc.ts b/frontend/src/ts/utils/misc.ts index 510a82e1fe31..f58d88de94a8 100644 --- a/frontend/src/ts/utils/misc.ts +++ b/frontend/src/ts/utils/misc.ts @@ -146,10 +146,8 @@ export function escapeHTML(str: T): T { } export function clearTimeouts(timeouts: (number | NodeJS.Timeout)[]): void { - timeouts.forEach((to) => { - if (typeof to === "number") clearTimeout(to); - else clearTimeout(to); - }); + timeouts.forEach((to) => clearTimeout(to)); + timeouts.length = 0; } //https://stackoverflow.com/questions/273789/is-there-a-version-of-javascripts-string-indexof-that-allows-for-regular-expr