Skip to content

Commit 934ddf9

Browse files
committed
fix: remove debounce on beforeDestroy to not have a script that is triggered despite the element being removed from the dom
1 parent 0074d77 commit 934ddf9

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

_dev/src/ts/components/LogsViewer.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export default class LogsViewer extends ComponentAbstract implements DomLifecycl
4545
}
4646

4747
public mount = () => {
48-
this.#logsScroll.addEventListener('scroll', this.#debouncedRefreshView);
48+
this.#logsScroll.addEventListener('scroll', this.#debouncedRefreshView.debounced);
4949

5050
// delay needed because of side menu toggle on small screens
5151
// we set width to prevent the modification of the height of
@@ -57,8 +57,9 @@ export default class LogsViewer extends ComponentAbstract implements DomLifecycl
5757

5858
public beforeDestroy = () => {
5959
logStore.clearLogs();
60-
this.#logsScroll.removeEventListener('scroll', this.#debouncedRefreshView);
60+
this.#logsScroll.removeEventListener('scroll', this.#debouncedRefreshView.debounced);
6161
this.#logsSummary.removeEventListener('click', this.#handleLinkEvent);
62+
this.#debouncedRefreshView.cancel();
6263
};
6364

6465
/**

_dev/src/ts/utils/logsUtils.ts

+19-6
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,22 @@ export function parseLogWithSeverity(log: string): LogEntry {
5050

5151
/**
5252
* @public
53-
* @param {T} func
54-
* @param {number} wait
55-
* @return {(...args: Parameters<T>) => void}
56-
* @description
53+
* @template T
54+
* @param {T} func - The function to debounce.
55+
* @param {number} wait - The delay in milliseconds before the function is executed.
56+
* @return {(...args: Parameters<T>) => void & { clear: () => void }} - A debounced function
57+
* that delays the execution of `func` and provides a `clear` method to cancel any pending execution.
58+
* @description Creates a debounced version of the given function, ensuring it is executed
59+
* only after the specified delay has elapsed since the last invocation.
60+
* The returned function also includes a `clear` method to cancel any pending executions.
5761
*/
5862
export function debounce<T extends Procedure>(
5963
func: T,
6064
wait: number
61-
): (...args: Parameters<T>) => void {
65+
): { debounced: (...args: Parameters<T>) => void; cancel: () => void } {
6266
let timeoutId: ReturnType<typeof setTimeout> | undefined;
6367

64-
return (...args: Parameters<T>): void => {
68+
const debounced = (...args: Parameters<T>): void => {
6569
if (timeoutId) {
6670
clearTimeout(timeoutId);
6771
}
@@ -70,6 +74,15 @@ export function debounce<T extends Procedure>(
7074
func(...args);
7175
}, wait);
7276
};
77+
78+
const cancel = (): void => {
79+
if (timeoutId) {
80+
clearTimeout(timeoutId);
81+
timeoutId = undefined;
82+
}
83+
};
84+
85+
return { debounced, cancel };
7386
}
7487

7588
/**

0 commit comments

Comments
 (0)