feat(lib/utils): add callbacks
This commit is contained in:
parent
ccec83264b
commit
65875f055c
3 changed files with 15 additions and 13 deletions
|
|
@ -3,6 +3,9 @@
|
|||
"private": true,
|
||||
"type": "module",
|
||||
"exports": {
|
||||
"./callbacks": {
|
||||
"default": "./src/callbacks.ts"
|
||||
},
|
||||
"./errors": {
|
||||
"default": "./src/errors.ts"
|
||||
},
|
||||
|
|
|
|||
10
lib/utils/src/callbacks.ts
Normal file
10
lib/utils/src/callbacks.ts
Normal 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)
|
||||
})
|
||||
}
|
||||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue