feat(lib/utils): add callbacks

This commit is contained in:
PalmDevs 2025-04-15 14:22:41 +07:00
parent ccec83264b
commit 65875f055c
No known key found for this signature in database
GPG key ID: 6FAE8F8677ACA8BA
3 changed files with 15 additions and 13 deletions

View file

@ -3,6 +3,9 @@
"private": true,
"type": "module",
"exports": {
"./callbacks": {
"default": "./src/callbacks.ts"
},
"./errors": {
"default": "./src/errors.ts"
},

View file

@ -0,0 +1,10 @@
export function debounce<F extends (...args: any[]) => any>(func: F, timeout: number) {
let timer: Parameters<typeof clearTimeout>[0]
return (...args: Parameters<F>) =>
new Promise(rs => {
clearTimeout(timer)
timer = setTimeout(() => {
rs(func(...args))
}, timeout)
})
}

View file

@ -1,24 +1,13 @@
import { join } from 'path'
import watcher from '@parcel/watcher'
import { debounce } from '@revenge-mod/utils/callbacks'
import chalk from 'chalk'
import { join } from 'path'
import build from './build'
const cwdify = (path: string) => join(process.cwd(), path)
const Sources = ['src', 'lib', 'plugins'].map(cwdify)
const ExitTriggers = ['scripts', 'package.json', 'bun.lock'].map(cwdify)
function debounce<F extends (...args: any[]) => any>(func: F, timeout: number) {
let timer: NodeJS.Timer
return (...args: Parameters<F>) =>
new Promise(rs => {
clearTimeout(timer)
timer = setTimeout(() => {
rs(func(...args))
}, timeout)
})
}
const buildDevWithDebounceAndCatchErrors = debounce(async () => {
needRebuild = false