From 0ad9c8195c727e793f9b0f315523d793f4d010f3 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Sat, 24 Feb 2024 12:31:11 +0100 Subject: [PATCH] fix(watcher): debounce event handling --- package.json | 5 +++-- pnpm-lock.yaml | 3 +++ src/automd.ts | 5 +++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index cf948bc..8bda96a 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "ofetch": "^1.3.3", "omark": "^0.1.0", "pathe": "^1.1.2", + "perfect-debounce": "^1.0.0", "pkg-types": "^1.0.3", "scule": "^1.3.0", "untyped": "^1.4.2" @@ -53,6 +54,7 @@ "devDependencies": { "@types/node": "^20.11.20", "@vitest/coverage-v8": "^1.3.1", + "automd": "link:.", "changelogen": "^0.5.5", "eslint": "^8.57.0", "eslint-config-unjs": "^0.2.1", @@ -60,8 +62,7 @@ "prettier": "^3.2.5", "typescript": "^5.3.3", "unbuild": "^2.0.0", - "vitest": "^1.3.1", - "automd": "link:." + "vitest": "^1.3.1" }, "packageManager": "pnpm@8.15.3" } \ No newline at end of file diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d7898c2..48c5ef1 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -44,6 +44,9 @@ dependencies: pathe: specifier: ^1.1.2 version: 1.1.2 + perfect-debounce: + specifier: ^1.0.0 + version: 1.0.0 pkg-types: specifier: ^1.0.3 version: 1.0.3 diff --git a/src/automd.ts b/src/automd.ts index d3c9820..91a023c 100644 --- a/src/automd.ts +++ b/src/automd.ts @@ -2,6 +2,7 @@ import { existsSync, promises as fsp } from "node:fs"; import { resolve, relative } from "pathe"; import type { SubscribeCallback } from "@parcel/watcher"; import { pathToFileURL } from "mlly"; +import { debounce } from "perfect-debounce"; import type { Config, ResolvedConfig } from "./config"; import { type TransformResult, transform } from "./transform"; import { loadConfig } from "./config"; @@ -108,7 +109,7 @@ async function _watch( ) { const watcher = await import("@parcel/watcher"); - const watchCb: SubscribeCallback = async (_err, events) => { + const watchCb: SubscribeCallback = debounce(async (_err, events) => { const filesToUpdate = events .map((e) => relative(config.dir, e.path)) .filter((p) => inputFiles.includes(p)); @@ -120,7 +121,7 @@ async function _watch( if (config.onWatch) { config.onWatch({ results, time }); } - }; + }); const subscription = await watcher.subscribe(config.dir, watchCb, { ignore: config.ignore,