How to unwatch in Deno?

Viewed 147

There is a Deno.watch api to watch for file system events:

const watcher = Deno.watchFs("/");
for await (const event of watcher) {
   console.log(">>>> event", event);
   // { kind: "create", paths: [ "/foo.txt" ] }
}

But how to unwatch after Deno.watch called?

1 Answers

You can call watcher.return

const watcher = Deno.watchFs("/your-dir");

setTimeout(() => {
  watcher.return();
}, 2000)

for await (const event of watcher) {
   console.log(">>>> event", event);
   // { kind: "create", paths: [ "/foo.txt" ] }
}

It's not recommended to watch the whole filesystem / it'll take a lot of time to start. Deno.watchFs call synchronously to op_fs_events_open. After a watcher is acquired operations are async.

Related