1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-28 10:09:20 -05:00
denoland-deno/cli/js/ops/fs_events.ts
2020-03-20 09:38:34 -04:00

39 lines
1.1 KiB
TypeScript

// Copyright 2019 the Deno authors. All rights reserved. MIT license.
import { sendSync, sendAsync } from "./dispatch_json.ts";
import { close } from "./resources.ts";
export interface FsEvent {
kind: "any" | "access" | "create" | "modify" | "remove";
paths: string[];
}
class FsEvents implements AsyncIterableIterator<FsEvent> {
readonly rid: number;
constructor(paths: string[], options: { recursive: boolean }) {
const { recursive } = options;
this.rid = sendSync("op_fs_events_open", { recursive, paths });
}
next(): Promise<IteratorResult<FsEvent>> {
return sendAsync("op_fs_events_poll", {
rid: this.rid
});
}
return(value?: FsEvent): Promise<IteratorResult<FsEvent>> {
close(this.rid);
return Promise.resolve({ value, done: true });
}
[Symbol.asyncIterator](): AsyncIterableIterator<FsEvent> {
return this;
}
}
export function fsEvents(
paths: string | string[],
options = { recursive: true }
): AsyncIterableIterator<FsEvent> {
return new FsEvents(Array.isArray(paths) ? paths : [paths], options);
}