fix: use perfect-debounce to handle trailing run of promise (#3679)

Co-authored-by: pooya parsa <pyapar@gmail.com>
This commit is contained in:
Daniel Roe 2022-03-16 11:11:30 +00:00 committed by GitHub
parent 3a8bef609d
commit ff88d91bae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 26 additions and 19 deletions

View File

@ -46,8 +46,8 @@
"murmurhash-es": "^0.1.1",
"node-fetch": "^3.2.3",
"nuxi": "3.0.0",
"p-debounce": "^4.0.0",
"pathe": "^0.2.0",
"perfect-debounce": "^0.1.3",
"postcss": "^8",
"postcss-import": "^14.0.2",
"postcss-import-resolver": "^2.0.0",

View File

@ -3,7 +3,7 @@ import * as vite from 'vite'
import { createVuePlugin } from 'vite-plugin-vue2'
import { logger } from '@nuxt/kit'
import fse from 'fs-extra'
import pDebounce from 'p-debounce'
import { debounce } from 'perfect-debounce'
import { bundleRequest } from '../../../vite/src/dev-bundler'
import { isCSS } from '../../../vite/src/utils'
import { wpfs } from './utils/wpfs'
@ -118,7 +118,7 @@ export async function buildServer (ctx: ViteBuildContext) {
consola.info(`Server built in ${time}ms`)
await onBuild()
}
const doBuild = pDebounce(pDebounce.promise(_doBuild), 300)
const doBuild = debounce(_doBuild)
// Initial build
await _doBuild()

View File

@ -55,8 +55,8 @@
"node-fetch": "^3.2.3",
"ohmyfetch": "^0.4.15",
"ora": "^6.1.0",
"p-debounce": "^4.0.0",
"pathe": "^0.2.0",
"perfect-debounce": "^0.1.3",
"pkg-types": "^0.3.2",
"pretty-bytes": "^6.0.0",
"rollup": "^2.70.1",

View File

@ -4,7 +4,7 @@ import { IncomingMessage, ServerResponse } from 'http'
import { existsSync, promises as fsp } from 'fs'
import { loading as loadingTemplate } from '@nuxt/ui-templates'
import chokidar, { FSWatcher } from 'chokidar'
import debounce from 'p-debounce'
import { debounce } from 'perfect-debounce'
import { promisifyHandle, createApp, Middleware, useBase } from 'h3'
import httpProxy from 'http-proxy'
import { listen, Listener, ListenOptions } from 'listhen'
@ -128,7 +128,7 @@ export function createDevServer (nitroContext: NitroContext) {
let watcher: FSWatcher
function watch () {
if (watcher) { return }
const dReload = debounce(debounce.promise(() => reload().catch(console.warn)), 200, { before: true })
const dReload = debounce(() => reload().catch(console.warn))
watcher = chokidar.watch([
resolve(nitroContext.output.serverDir, pattern),
resolve(nitroContext._nuxt.buildDir, 'dist/server', pattern)

View File

@ -37,8 +37,8 @@
"listhen": "^0.2.6",
"mlly": "^0.4.3",
"mri": "^1.2.0",
"p-debounce": "^4.0.0",
"pathe": "^0.2.0",
"perfect-debounce": "^0.1.3",
"pkg-types": "^0.3.2",
"rimraf": "^3.0.2",
"scule": "^0.2.1",

View File

@ -1,6 +1,6 @@
import { resolve, relative } from 'pathe'
import chokidar from 'chokidar'
import debounce from 'p-debounce'
import { debounce } from 'perfect-debounce'
import type { Nuxt } from '@nuxt/schema'
import consola from 'consola'
import { withTrailingSlash } from 'ufo'
@ -69,7 +69,7 @@ export default defineNuxtCommand({
// Watch for config changes
// TODO: Watcher service, modules, and requireTree
const dLoad = debounce(debounce.promise(load), 250)
const dLoad = debounce(load)
const watcher = chokidar.watch([rootDir], { ignoreInitial: true, depth: 1 })
watcher.on('all', (event, file) => {
if (!currentNuxt) { return }

View File

@ -1,12 +1,12 @@
import chokidar from 'chokidar'
import type { Nuxt } from '@nuxt/schema'
import { isIgnored, tryImportModule } from '@nuxt/kit'
import debounce from 'p-debounce'
import { debounce } from 'perfect-debounce'
import { createApp, generateApp as _generateApp } from './app'
export async function build (nuxt: Nuxt) {
const app = createApp(nuxt)
const generateApp = debounce(debounce.promise(() => _generateApp(nuxt, app)), 1)
const generateApp = debounce(() => _generateApp(nuxt, app), undefined, { leading: true })
await generateApp()
if (nuxt.options.dev) {
@ -48,7 +48,7 @@ function watch (nuxt: Nuxt) {
]
})
const watchHook = debounce(debounce.promise((event: 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir', path: string) => nuxt.callHook('builder:watch', event, path)), 1)
const watchHook = debounce((event: 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir', path: string) => nuxt.callHook('builder:watch', event, path))
watcher.on('all', watchHook)
nuxt.hook('close', () => watcher.close())
return watcher

View File

@ -34,8 +34,8 @@
"knitwork": "^0.1.1",
"magic-string": "^0.26.1",
"mlly": "^0.4.3",
"p-debounce": "^4.0.0",
"pathe": "^0.2.0",
"perfect-debounce": "^0.1.3",
"postcss": "^8.4.12",
"postcss-import": "^14.0.2",
"postcss-url": "^10.1.3",

View File

@ -4,7 +4,7 @@ import vuePlugin from '@vitejs/plugin-vue'
import viteJsxPlugin from '@vitejs/plugin-vue-jsx'
import { logger, resolveModule } from '@nuxt/kit'
import fse from 'fs-extra'
import pDebounce from 'p-debounce'
import { debounce } from 'perfect-debounce'
import { withoutTrailingSlash } from 'ufo'
import { ViteBuildContext, ViteOptions } from './vite'
import { wpfs } from './utils/wpfs'
@ -154,7 +154,7 @@ export async function buildServer (ctx: ViteBuildContext) {
logger.success(`Vite server built in ${time}ms`)
await onBuild()
}
const doBuild = pDebounce(pDebounce.promise(_doBuild), 100)
const doBuild = debounce(_doBuild)
// Initial build
await _doBuild()

View File

@ -2620,8 +2620,8 @@ __metadata:
node-fetch: ^3.2.3
nuxi: 3.0.0
nuxt: ^2
p-debounce: ^4.0.0
pathe: ^0.2.0
perfect-debounce: ^0.1.3
postcss: ^8
postcss-import: ^14.0.2
postcss-import-resolver: ^2.0.0
@ -3027,8 +3027,8 @@ __metadata:
node-fetch: ^3.2.3
ohmyfetch: ^0.4.15
ora: ^6.1.0
p-debounce: ^4.0.0
pathe: ^0.2.0
perfect-debounce: ^0.1.3
pkg-types: ^0.3.2
pretty-bytes: ^6.0.0
rollup: ^2.70.1
@ -3328,8 +3328,8 @@ __metadata:
knitwork: ^0.1.1
magic-string: ^0.26.1
mlly: ^0.4.3
p-debounce: ^4.0.0
pathe: ^0.2.0
perfect-debounce: ^0.1.3
postcss: ^8.4.12
postcss-import: ^14.0.2
postcss-url: ^10.1.3
@ -15323,8 +15323,8 @@ __metadata:
listhen: ^0.2.6
mlly: ^0.4.3
mri: ^1.2.0
p-debounce: ^4.0.0
pathe: ^0.2.0
perfect-debounce: ^0.1.3
pkg-types: ^0.3.2
rimraf: ^3.0.2
scule: ^0.2.1
@ -16180,6 +16180,13 @@ __metadata:
languageName: node
linkType: hard
"perfect-debounce@npm:^0.1.3":
version: 0.1.3
resolution: "perfect-debounce@npm:0.1.3"
checksum: c77465eb23d87669e5ee646d12331507c45278e0f2e6fc20a587104b956c1a53f9afe723915d882f73fa588e42770a2edd2bbd93eb0b958c9ff1eb7b915ccdc1
languageName: node
linkType: hard
"performance-now@npm:^2.1.0":
version: 2.1.0
resolution: "performance-now@npm:2.1.0"