mirror of
https://github.com/denoland/deno.git
synced 2025-10-03 15:44:36 +00:00
feat: Deno.fsEvents() (#3452)
This commit is contained in:
parent
754b8c65ad
commit
bd640bc7e6
12 changed files with 1355 additions and 943 deletions
40
cli/js/fs_events.ts
Normal file
40
cli/js/fs_events.ts
Normal file
|
@ -0,0 +1,40 @@
|
|||
// Copyright 2019 the Deno authors. All rights reserved. MIT license.
|
||||
import { sendSync, sendAsync } from "./dispatch_json.ts";
|
||||
import * as dispatch from "./dispatch.ts";
|
||||
import { close } from "./files.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(dispatch.OP_FS_EVENTS_OPEN, { recursive, paths });
|
||||
}
|
||||
|
||||
async next(): Promise<IteratorResult<FsEvent>> {
|
||||
return await sendAsync(dispatch.OP_FS_EVENTS_POLL, {
|
||||
rid: this.rid
|
||||
});
|
||||
}
|
||||
|
||||
async return(value?: FsEvent): Promise<IteratorResult<FsEvent>> {
|
||||
close(this.rid);
|
||||
return { 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);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue