Fix throttle to use correct pending arguments

This commit is contained in:
Jon Staab
2024-12-12 15:08:44 -08:00
parent de8934dbde
commit 627eb1e36d
2 changed files with 6 additions and 8 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@welshman/lib",
"version": "0.0.31",
"version": "0.0.32",
"author": "hodlbod",
"license": "MIT",
"description": "A collection of utilities.",
+5 -7
View File
@@ -463,14 +463,13 @@ export const throttle = <F extends (...args: any[]) => any>(ms: number, f: F) =>
return f
}
let args: Parameters<F>
let paused = false
let trailing = false
let nextArgs: Maybe<Parameters<F>>
const unpause = () => {
if (trailing) {
f(...args)
trailing = false
if (nextArgs) {
f(...nextArgs)
nextArgs = undefined
}
paused = false
@@ -480,10 +479,9 @@ export const throttle = <F extends (...args: any[]) => any>(ms: number, f: F) =>
if (!paused) {
f(...thisArgs)
paused = true
args = thisArgs
setTimeout(unpause, ms)
} else {
trailing = true
nextArgs = thisArgs
}
}
}