Skip to content

Commit 0ad9c81

Browse files
committed
fix(watcher): debounce event handling
1 parent bbe3c12 commit 0ad9c81

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,23 @@
4646
"ofetch": "^1.3.3",
4747
"omark": "^0.1.0",
4848
"pathe": "^1.1.2",
49+
"perfect-debounce": "^1.0.0",
4950
"pkg-types": "^1.0.3",
5051
"scule": "^1.3.0",
5152
"untyped": "^1.4.2"
5253
},
5354
"devDependencies": {
5455
"@types/node": "^20.11.20",
5556
"@vitest/coverage-v8": "^1.3.1",
57+
"automd": "link:.",
5658
"changelogen": "^0.5.5",
5759
"eslint": "^8.57.0",
5860
"eslint-config-unjs": "^0.2.1",
5961
"jiti": "^1.21.0",
6062
"prettier": "^3.2.5",
6163
"typescript": "^5.3.3",
6264
"unbuild": "^2.0.0",
63-
"vitest": "^1.3.1",
64-
"automd": "link:."
65+
"vitest": "^1.3.1"
6566
},
6667
"packageManager": "pnpm@8.15.3"
6768
}

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/automd.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { existsSync, promises as fsp } from "node:fs";
22
import { resolve, relative } from "pathe";
33
import type { SubscribeCallback } from "@parcel/watcher";
44
import { pathToFileURL } from "mlly";
5+
import { debounce } from "perfect-debounce";
56
import type { Config, ResolvedConfig } from "./config";
67
import { type TransformResult, transform } from "./transform";
78
import { loadConfig } from "./config";
@@ -108,7 +109,7 @@ async function _watch(
108109
) {
109110
const watcher = await import("@parcel/watcher");
110111

111-
const watchCb: SubscribeCallback = async (_err, events) => {
112+
const watchCb: SubscribeCallback = debounce(async (_err, events) => {
112113
const filesToUpdate = events
113114
.map((e) => relative(config.dir, e.path))
114115
.filter((p) => inputFiles.includes(p));
@@ -120,7 +121,7 @@ async function _watch(
120121
if (config.onWatch) {
121122
config.onWatch({ results, time });
122123
}
123-
};
124+
});
124125

125126
const subscription = await watcher.subscribe(config.dir, watchCb, {
126127
ignore: config.ignore,

0 commit comments

Comments
 (0)