1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-11 10:07:54 -05:00
denoland-deno/cli/js/ops/fs_events.ts

45 lines
1.1 KiB
TypeScript
Raw Normal View History

2020-07-06 21:45:39 -04:00
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
2020-02-21 13:21:51 -05:00
import { sendSync, sendAsync } from "./dispatch_json.ts";
import { close } from "./resources.ts";
2020-02-21 13:21:51 -05:00
export interface FsEvent {
kind: "any" | "access" | "create" | "modify" | "remove";
paths: string[];
}
2020-07-06 21:45:39 -04:00
interface FsWatcherOptions {
recursive: boolean;
}
class FsWatcher implements AsyncIterableIterator<FsEvent> {
2020-02-21 13:21:51 -05:00
readonly rid: number;
2020-07-06 21:45:39 -04:00
constructor(paths: string[], options: FsWatcherOptions) {
2020-02-21 13:21:51 -05:00
const { recursive } = options;
2020-02-25 09:14:27 -05:00
this.rid = sendSync("op_fs_events_open", { recursive, paths });
2020-02-21 13:21:51 -05:00
}
2020-03-20 09:38:34 -04:00
next(): Promise<IteratorResult<FsEvent>> {
return sendAsync("op_fs_events_poll", {
rid: this.rid,
2020-02-21 13:21:51 -05:00
});
}
2020-03-20 09:38:34 -04:00
return(value?: FsEvent): Promise<IteratorResult<FsEvent>> {
2020-02-21 13:21:51 -05:00
close(this.rid);
2020-03-20 09:38:34 -04:00
return Promise.resolve({ value, done: true });
2020-02-21 13:21:51 -05:00
}
[Symbol.asyncIterator](): AsyncIterableIterator<FsEvent> {
return this;
}
}
export function watchFs(
2020-02-21 13:21:51 -05:00
paths: string | string[],
2020-07-06 21:45:39 -04:00
options: FsWatcherOptions = { recursive: true }
2020-02-21 13:21:51 -05:00
): AsyncIterableIterator<FsEvent> {
return new FsWatcher(Array.isArray(paths) ? paths : [paths], options);
2020-02-21 13:21:51 -05:00
}