1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-15 16:43:44 -05:00
denoland-deno/cli/js/ops/fs_events.ts

40 lines
1.1 KiB
TypeScript
Raw Normal View History

2020-02-21 13:21:51 -05:00
// Copyright 2019 the Deno authors. All rights reserved. MIT license.
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[];
}
class FsWatcher implements AsyncIterableIterator<FsEvent> {
2020-02-21 13:21:51 -05:00
readonly rid: number;
constructor(paths: string[], options: { recursive: boolean }) {
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[],
options = { recursive: true }
): AsyncIterableIterator<FsEvent> {
return new FsWatcher(Array.isArray(paths) ? paths : [paths], options);
2020-02-21 13:21:51 -05:00
}