diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2020-02-21 13:21:51 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-21 13:21:51 -0500 |
commit | bd640bc7e6a946dec4477afc64d8083e372660f6 (patch) | |
tree | cb08b999ad594dc0796fa1fefc588c0b86083066 /cli/js/fs_events.ts | |
parent | 754b8c65ad5adda2961c667a6b64ab59c130111d (diff) |
feat: Deno.fsEvents() (#3452)
Diffstat (limited to 'cli/js/fs_events.ts')
-rw-r--r-- | cli/js/fs_events.ts | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/cli/js/fs_events.ts b/cli/js/fs_events.ts new file mode 100644 index 000000000..a4deff48a --- /dev/null +++ b/cli/js/fs_events.ts @@ -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); +} |