refactor(core): CORE-01 migrate utility contracts to TypeScript

This commit is contained in:
Harvey Zhao committed 2026-09-10 23:50:47 +08:00
1 parent e005e5f249
commit d3c695024a
33 files changed
+1269 -305

No files matched your search

+32
View File
@@ -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)
}
}
}