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";
|
2020-03-08 08:09:22 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-04-24 17:40:29 -04:00
|
|
|
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>> {
|
2020-03-16 05:22:16 -04:00
|
|
|
return sendAsync("op_fs_events_poll", {
|
2020-03-28 13:03:49 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 17:40:29 -04:00
|
|
|
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> {
|
2020-04-24 17:40:29 -04:00
|
|
|
return new FsWatcher(Array.isArray(paths) ? paths : [paths], options);
|
2020-02-21 13:21:51 -05:00
|
|
|
}
|