Skip to content

Commit

Permalink
fix: fixed debounce arguments due to compiler bug
Browse files Browse the repository at this point in the history
  • Loading branch information
KiraLT committed Jul 30, 2024
1 parent 6cba175 commit def5e73
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
1 change: 0 additions & 1 deletion spec/async.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ describe('debounce', () => {
cb(3)

await delay(110)

expect(mock.mock.calls.length).toBe(1)
expect(mock.mock.calls[0][0]).toBe(3)
})
Expand Down
12 changes: 7 additions & 5 deletions src/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ export function debounce<A extends unknown[]>(
* The number of milliseconds to delay.
*/
timeInMs: number,
): (...args: A) => void {
): (this: unknown, ...args: A) => void {
let timer: ReturnType<typeof setTimeout>
return function (this: unknown, ...args: A) {
return function () {
const args = arguments as unknown as A
clearTimeout(timer)
timer = setTimeout(async () => {
timer = setTimeout(() => {
func.apply(this, args)
}, timeInMs)
}
Expand Down Expand Up @@ -87,16 +88,17 @@ export function throttle<A extends unknown[]>(
*/
trailing?: boolean
},
): (...args: A) => void {
): (this: unknown, ...args: A) => void {
let lastCallTime: number | undefined
let timer: ReturnType<typeof setTimeout> | undefined
let lastArgs: A
let lastThis: unknown

const { leading = true, trailing = true } = options ?? {}

return function (this: unknown, ...args: A) {
return function () {
const now = Date.now()
const args = arguments as unknown as A
lastArgs = args
lastThis = this

Expand Down

0 comments on commit def5e73

Please sign in to comment.