-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f536e4e
commit c647b90
Showing
5 changed files
with
24 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 16 additions & 13 deletions
29
...ts/atoms/DynamicPopover/utils/debounce.ts → components/src/utils/debounce.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,43 @@ | ||
export function debounceWithWait<T extends (...args: any[]) => any>( | ||
type DebounceOptions = { | ||
maxWait?: number | ||
} | ||
|
||
export function debounce<T extends (...args: any[]) => any>( | ||
func: T, | ||
wait: number, | ||
maxWait: number, | ||
options?: DebounceOptions, | ||
): (...args: Parameters<T>) => void { | ||
let timer: NodeJS.Timeout | null = null | ||
let maxTimer: NodeJS.Timeout | null = null | ||
const { maxWait } = options || {} | ||
|
||
return function (...args: Parameters<T>): void { | ||
// @ts-expect-error some magic | ||
// @ts-expect-error because this in a func | ||
// eslint-disable-next-line @typescript-eslint/no-this-alias | ||
const context = this | ||
|
||
if (timer) { | ||
clearTimeout(timer) | ||
} | ||
|
||
// Clear the maxWait timer if it exists | ||
if (maxTimer) { | ||
clearTimeout(maxTimer) | ||
} | ||
|
||
// Set the regular debounce timer | ||
timer = setTimeout(() => { | ||
if (maxTimer) { | ||
clearTimeout(maxTimer) | ||
} | ||
func.apply(context, args) | ||
}, wait) | ||
|
||
// Set the maxWait timer | ||
maxTimer = setTimeout(() => { | ||
func.apply(context, args) | ||
// Clear the regular timer since maxWait has fired | ||
if (timer) { | ||
clearTimeout(timer) | ||
} | ||
}, maxWait) | ||
if (maxWait) { | ||
maxTimer = setTimeout(() => { | ||
func.apply(context, args) | ||
if (timer) { | ||
clearTimeout(timer) | ||
} | ||
}, maxWait) | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.