File tree 2 files changed +22
-8
lines changed
2 files changed +22
-8
lines changed Original file line number Diff line number Diff line change @@ -45,7 +45,7 @@ export default class LogsViewer extends ComponentAbstract implements DomLifecycl
45
45
}
46
46
47
47
public mount = ( ) => {
48
- this . #logsScroll. addEventListener ( 'scroll' , this . #debouncedRefreshView) ;
48
+ this . #logsScroll. addEventListener ( 'scroll' , this . #debouncedRefreshView. debounced ) ;
49
49
50
50
// delay needed because of side menu toggle on small screens
51
51
// we set width to prevent the modification of the height of
@@ -57,8 +57,9 @@ export default class LogsViewer extends ComponentAbstract implements DomLifecycl
57
57
58
58
public beforeDestroy = ( ) => {
59
59
logStore . clearLogs ( ) ;
60
- this . #logsScroll. removeEventListener ( 'scroll' , this . #debouncedRefreshView) ;
60
+ this . #logsScroll. removeEventListener ( 'scroll' , this . #debouncedRefreshView. debounced ) ;
61
61
this . #logsSummary. removeEventListener ( 'click' , this . #handleLinkEvent) ;
62
+ this . #debouncedRefreshView. cancel ( ) ;
62
63
} ;
63
64
64
65
/**
Original file line number Diff line number Diff line change @@ -50,18 +50,22 @@ export function parseLogWithSeverity(log: string): LogEntry {
50
50
51
51
/**
52
52
* @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.
57
61
*/
58
62
export function debounce < T extends Procedure > (
59
63
func : T ,
60
64
wait : number
61
- ) : ( ...args : Parameters < T > ) => void {
65
+ ) : { debounced : ( ...args : Parameters < T > ) => void ; cancel : ( ) => void } {
62
66
let timeoutId : ReturnType < typeof setTimeout > | undefined ;
63
67
64
- return ( ...args : Parameters < T > ) : void => {
68
+ const debounced = ( ...args : Parameters < T > ) : void => {
65
69
if ( timeoutId ) {
66
70
clearTimeout ( timeoutId ) ;
67
71
}
@@ -70,6 +74,15 @@ export function debounce<T extends Procedure>(
70
74
func ( ...args ) ;
71
75
} , wait ) ;
72
76
} ;
77
+
78
+ const cancel = ( ) : void => {
79
+ if ( timeoutId ) {
80
+ clearTimeout ( timeoutId ) ;
81
+ timeoutId = undefined ;
82
+ }
83
+ } ;
84
+
85
+ return { debounced, cancel } ;
73
86
}
74
87
75
88
/**
You can’t perform that action at this time.
0 commit comments