Skip to content

Commit

Permalink
fix(watcher): debounce event handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Feb 24, 2024
1 parent bbe3c12 commit 0ad9c81
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,23 @@
"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"
},
"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",
"jiti": "^1.21.0",
"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"
}
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions src/automd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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));
Expand All @@ -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,
Expand Down

0 comments on commit 0ad9c81

Please sign in to comment.