Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

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

Merged
merged 9 commits into from
Mar 16, 2022
2 changes: 1 addition & 1 deletion packages/bridge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.1",
"postcss": "^8",
"postcss-import": "^14.0.2",
"postcss-import-resolver": "^2.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/bridge/src/vite/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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, 300)
danielroe marked this conversation as resolved.
Show resolved Hide resolved

// Initial build
await _doBuild()
Expand Down
2 changes: 1 addition & 1 deletion packages/nitro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.1",
"pkg-types": "^0.3.2",
"pretty-bytes": "^6.0.0",
"rollup": "^2.70.1",
Expand Down
4 changes: 2 additions & 2 deletions packages/nitro/src/server/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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), 200, { leading: true })
danielroe marked this conversation as resolved.
Show resolved Hide resolved
watcher = chokidar.watch([
resolve(nitroContext.output.serverDir, pattern),
resolve(nitroContext._nuxt.buildDir, 'dist/server', pattern)
Expand Down
2 changes: 1 addition & 1 deletion packages/nuxi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.1",
"pkg-types": "^0.3.2",
"rimraf": "^3.0.2",
"scule": "^0.2.1",
Expand Down
4 changes: 2 additions & 2 deletions packages/nuxi/src/commands/dev.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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, 250)
danielroe marked this conversation as resolved.
Show resolved Hide resolved
const watcher = chokidar.watch([rootDir], { ignoreInitial: true, depth: 1 })
watcher.on('all', (event, file) => {
if (!currentNuxt) { return }
Expand Down
6 changes: 3 additions & 3 deletions packages/nuxt3/src/core/builder.ts
Original file line number Diff line number Diff line change
@@ -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), 25, { leading: true })
await generateApp()

if (nuxt.options.dev) {
Expand Down Expand Up @@ -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), 25, { leading: true })
danielroe marked this conversation as resolved.
Show resolved Hide resolved
watcher.on('all', watchHook)
nuxt.hook('close', () => watcher.close())
return watcher
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.1",
"postcss": "^8.4.8",
"postcss-import": "^14.0.2",
"postcss-url": "^10.1.3",
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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, 100)

// Initial build
await _doBuild()
Expand Down
15 changes: 11 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2736,8 +2736,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.1
postcss: ^8
postcss-import: ^14.0.2
postcss-import-resolver: ^2.0.0
Expand Down Expand Up @@ -3142,8 +3142,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.1
pkg-types: ^0.3.2
pretty-bytes: ^6.0.0
rollup: ^2.70.1
Expand Down Expand Up @@ -3442,8 +3442,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.1
postcss: ^8.4.8
postcss-import: ^14.0.2
postcss-url: ^10.1.3
Expand Down Expand Up @@ -15685,8 +15685,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.1
pkg-types: ^0.3.2
rimraf: ^3.0.2
scule: ^0.2.1
Expand Down Expand Up @@ -16549,6 +16549,13 @@ __metadata:
languageName: node
linkType: hard

"perfect-debounce@npm:^0.1.1":
version: 0.1.1
resolution: "perfect-debounce@npm:0.1.1"
checksum: ced6838702de7b89e27cacca737236754abdaf77f0852e33ec4a5a7e6af62996f512759c4a8785de958c9866c0345fd8d51e6e4814ac184c20012c4b4d90af5f
languageName: node
linkType: hard

"performance-now@npm:^2.1.0":
version: 2.1.0
resolution: "performance-now@npm:2.1.0"
Expand Down