summaryrefslogtreecommitdiff
path: root/std/node/_fs/_fs_watch.ts
diff options
context:
space:
mode:
authorCasper Beyer <caspervonb@pm.me>2021-02-02 19:05:46 +0800
committerGitHub <noreply@github.com>2021-02-02 12:05:46 +0100
commit6abf126c2a7a451cded8c6b5e6ddf1b69c84055d (patch)
treefd94c013a19fcb38954844085821ec1601c20e18 /std/node/_fs/_fs_watch.ts
parenta2b5d44f1aa9d64f448a2a3cc2001272e2f60b98 (diff)
chore: remove std directory (#9361)
This removes the std folder from the tree. Various parts of the tests are pretty tightly dependent on std (47 direct imports and 75 indirect imports, not counting the cli tests that use them as fixtures) so I've added std as a submodule for now.
Diffstat (limited to 'std/node/_fs/_fs_watch.ts')
-rw-r--r--std/node/_fs/_fs_watch.ts111
1 files changed, 0 insertions, 111 deletions
diff --git a/std/node/_fs/_fs_watch.ts b/std/node/_fs/_fs_watch.ts
deleted file mode 100644
index a5f3bb9c1..000000000
--- a/std/node/_fs/_fs_watch.ts
+++ /dev/null
@@ -1,111 +0,0 @@
-import { fromFileUrl } from "../path.ts";
-import { EventEmitter } from "../events.ts";
-import { notImplemented } from "../_utils.ts";
-
-export function asyncIterableIteratorToCallback<T>(
- iterator: AsyncIterableIterator<T>,
- callback: (val: T, done?: boolean) => void,
-) {
- function next() {
- iterator.next().then((obj) => {
- if (obj.done) {
- callback(obj.value, true);
- return;
- }
- callback(obj.value);
- next();
- });
- }
- next();
-}
-
-export function asyncIterableToCallback<T>(
- iter: AsyncIterable<T>,
- callback: (val: T, done?: boolean) => void,
-) {
- const iterator = iter[Symbol.asyncIterator]();
- function next() {
- iterator.next().then((obj) => {
- if (obj.done) {
- callback(obj.value, true);
- return;
- }
- callback(obj.value);
- next();
- });
- }
- next();
-}
-
-type watchOptions = {
- persistent?: boolean;
- recursive?: boolean;
- encoding?: string;
-};
-
-type watchListener = (eventType: string, filename: string) => void;
-
-export function watch(
- filename: string | URL,
- options: watchOptions,
- listener: watchListener,
-): FSWatcher;
-export function watch(
- filename: string | URL,
- listener: watchListener,
-): FSWatcher;
-export function watch(
- filename: string | URL,
- options: watchOptions,
-): FSWatcher;
-export function watch(filename: string | URL): FSWatcher;
-export function watch(
- filename: string | URL,
- optionsOrListener?: watchOptions | watchListener,
- optionsOrListener2?: watchOptions | watchListener,
-) {
- const listener = typeof optionsOrListener === "function"
- ? optionsOrListener
- : typeof optionsOrListener2 === "function"
- ? optionsOrListener2
- : undefined;
- const options = typeof optionsOrListener === "object"
- ? optionsOrListener
- : typeof optionsOrListener2 === "object"
- ? optionsOrListener2
- : undefined;
- filename = filename instanceof URL ? fromFileUrl(filename) : filename;
-
- const iterator = Deno.watchFs(filename, {
- recursive: options?.recursive || false,
- });
-
- if (!listener) throw new Error("No callback function supplied");
-
- const fsWatcher = new FSWatcher(() => {
- if (iterator.return) iterator.return();
- });
-
- fsWatcher.on("change", listener);
-
- asyncIterableIteratorToCallback<Deno.FsEvent>(iterator, (val, done) => {
- if (done) return;
- fsWatcher.emit("change", val.kind, val.paths[0]);
- });
-
- return fsWatcher;
-}
-
-class FSWatcher extends EventEmitter {
- close: () => void;
- constructor(closer: () => void) {
- super();
- this.close = closer;
- }
- ref() {
- notImplemented("FSWatcher.ref() is not implemented");
- }
- unref() {
- notImplemented("FSWatcher.unref() is not implemented");
- }
-}