mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-23 13:51:20 -08:00
refactor(core): CORE-01 migrate utility contracts to TypeScript
This commit is contained in:
1 parent
e005e5f249
commit
d3c695024a
33 files changed
+1269
-305
No files matched your search
@@ -0,0 +1,32 @@
|
||||
export function sleep(ms = 0): Promise<void> {
|
||||
return new Promise(resolve => setTimeout(resolve, ms))
|
||||
}
|
||||
|
||||
export function debounce<Receiver, Args extends unknown[]>(func: (this: Receiver, ...args: Args) => unknown, duration: number) {
|
||||
let timeout: ReturnType<typeof setTimeout> | undefined
|
||||
|
||||
return function (this: Receiver, ...args: Args): void {
|
||||
const effect = () => {
|
||||
timeout = undefined
|
||||
return func.apply(this, args)
|
||||
}
|
||||
|
||||
clearTimeout(timeout)
|
||||
timeout = setTimeout(effect, duration)
|
||||
}
|
||||
}
|
||||
|
||||
export function throttle<Receiver, Args extends unknown[]>(func: (this: Receiver, ...args: Args) => unknown, duration: number) {
|
||||
let shouldWait = false
|
||||
|
||||
return function (this: Receiver, ...args: Args): void {
|
||||
if (!shouldWait) {
|
||||
func.apply(this, args)
|
||||
shouldWait = true
|
||||
|
||||
setTimeout(() => {
|
||||
shouldWait = false
|
||||
}, duration)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user