Fix throttle to use correct pending arguments
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@welshman/lib",
|
"name": "@welshman/lib",
|
||||||
"version": "0.0.31",
|
"version": "0.0.32",
|
||||||
"author": "hodlbod",
|
"author": "hodlbod",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"description": "A collection of utilities.",
|
"description": "A collection of utilities.",
|
||||||
|
|||||||
@@ -463,14 +463,13 @@ export const throttle = <F extends (...args: any[]) => any>(ms: number, f: F) =>
|
|||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
|
|
||||||
let args: Parameters<F>
|
|
||||||
let paused = false
|
let paused = false
|
||||||
let trailing = false
|
let nextArgs: Maybe<Parameters<F>>
|
||||||
|
|
||||||
const unpause = () => {
|
const unpause = () => {
|
||||||
if (trailing) {
|
if (nextArgs) {
|
||||||
f(...args)
|
f(...nextArgs)
|
||||||
trailing = false
|
nextArgs = undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
paused = false
|
paused = false
|
||||||
@@ -480,10 +479,9 @@ export const throttle = <F extends (...args: any[]) => any>(ms: number, f: F) =>
|
|||||||
if (!paused) {
|
if (!paused) {
|
||||||
f(...thisArgs)
|
f(...thisArgs)
|
||||||
paused = true
|
paused = true
|
||||||
args = thisArgs
|
|
||||||
setTimeout(unpause, ms)
|
setTimeout(unpause, ms)
|
||||||
} else {
|
} else {
|
||||||
trailing = true
|
nextArgs = thisArgs
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user