deno.com

Watching the filesystem

在 Github 上编辑

When creating frameworks or CLI tools, it is often necessary to watch the filesystem for changes.

The easiest way to watch a filesystem is to use the Deno builtin watchFs. Deno.watchFs returns an FsWatcher which is an async iterable.
let watcher = Deno.watchFs("./");
The easiest way to interact with async iterables is the javascript for await of syntax.
for await (const event of watcher) {
  console.log(">>>> event", event);
To stop the watcher we can simply call `watcher.close()`
  watcher.close();
}
In real applications, it is quite rare that an application needs to react to every change instantly. Events will be duplicated and multiple events will be dispatched for the same changes. To get around this, we can "debounce" our functions.
import { debounce } from "jsr:@std/async/debounce";
In this specific case, we use the standard library to do the work for us. This function will run at most once every two hundred milliseconds
const log = debounce((event: Deno.FsEvent) => {
  console.log("[%s] %s", event.kind, event.paths[0]);
}, 200);

watcher = Deno.watchFs("./");

for await (const event of watcher) {
  log(event);
}

使用 Deno CLI 在本地运行 此示例

deno run --allow-read https://docs.denohub.com/examples/scripts/watching_files.ts

你找到需要的内容了吗?

隐私政策