mirror of
https://github.com/denoland/deno.git
synced 2025-01-03 12:58:54 -05:00
feat: add FsWatcher interface (#10798)
This commit is contained in:
parent
f891368057
commit
595700c993
3 changed files with 49 additions and 6 deletions
27
cli/dts/lib.deno.ns.d.ts
vendored
27
cli/dts/lib.deno.ns.d.ts
vendored
|
@ -1949,6 +1949,25 @@ declare namespace Deno {
|
||||||
paths: string[];
|
paths: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* FsWatcher is returned by `Deno.watchFs` function when you start watching
|
||||||
|
* the file system. You can iterate over this interface to get the file
|
||||||
|
* system events, and also you can stop watching the file system by calling
|
||||||
|
* `.close()` method.
|
||||||
|
*/
|
||||||
|
export interface FsWatcher extends AsyncIterable<FsEvent> {
|
||||||
|
/** The resource id of the `FsWatcher`. */
|
||||||
|
readonly rid: number;
|
||||||
|
/** Stops watching the file system and closes the watcher resource. */
|
||||||
|
close(): void;
|
||||||
|
/** @deprecated
|
||||||
|
* Stops watching the file system and closes the watcher resource.
|
||||||
|
* Will be removed at 2.0.
|
||||||
|
*/
|
||||||
|
return?(value?: any): Promise<IteratorResult<FsEvent>>;
|
||||||
|
[Symbol.asyncIterator](): AsyncIterableIterator<FsEvent>;
|
||||||
|
}
|
||||||
|
|
||||||
/** Watch for file system events against one or more `paths`, which can be files
|
/** Watch for file system events against one or more `paths`, which can be files
|
||||||
* or directories. These paths must exist already. One user action (e.g.
|
* or directories. These paths must exist already. One user action (e.g.
|
||||||
* `touch test.file`) can generate multiple file system events. Likewise,
|
* `touch test.file`) can generate multiple file system events. Likewise,
|
||||||
|
@ -1967,15 +1986,13 @@ declare namespace Deno {
|
||||||
*
|
*
|
||||||
* Requires `allow-read` permission.
|
* Requires `allow-read` permission.
|
||||||
*
|
*
|
||||||
* Call `watcher.return()` to stop watching.
|
* Call `watcher.close()` to stop watching.
|
||||||
*
|
*
|
||||||
* ```ts
|
* ```ts
|
||||||
* const watcher = Deno.watchFs("/");
|
* const watcher = Deno.watchFs("/");
|
||||||
*
|
*
|
||||||
* setTimeout(() => {
|
* setTimeout(() => {
|
||||||
* if (watcher.return) {
|
* watcher.close();
|
||||||
* watcher.return();
|
|
||||||
* }
|
|
||||||
* }, 5000);
|
* }, 5000);
|
||||||
*
|
*
|
||||||
* for await (const event of watcher) {
|
* for await (const event of watcher) {
|
||||||
|
@ -1986,7 +2003,7 @@ declare namespace Deno {
|
||||||
export function watchFs(
|
export function watchFs(
|
||||||
paths: string | string[],
|
paths: string | string[],
|
||||||
options?: { recursive: boolean },
|
options?: { recursive: boolean },
|
||||||
): AsyncIterableIterator<FsEvent>;
|
): FsWatcher;
|
||||||
|
|
||||||
export class Process<T extends RunOptions = RunOptions> {
|
export class Process<T extends RunOptions = RunOptions> {
|
||||||
readonly rid: number;
|
readonly rid: number;
|
||||||
|
|
|
@ -26,7 +26,7 @@ unitTest({ perms: { read: true } }, function watchFsInvalidPath() {
|
||||||
});
|
});
|
||||||
|
|
||||||
async function getTwoEvents(
|
async function getTwoEvents(
|
||||||
iter: AsyncIterableIterator<Deno.FsEvent>,
|
iter: Deno.FsWatcher,
|
||||||
): Promise<Deno.FsEvent[]> {
|
): Promise<Deno.FsEvent[]> {
|
||||||
const events = [];
|
const events = [];
|
||||||
for await (const event of iter) {
|
for await (const event of iter) {
|
||||||
|
@ -61,6 +61,8 @@ unitTest(
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// TODO(kt3k): This test is for the backward compatibility of `.return` method.
|
||||||
|
// This should be removed at 2.0
|
||||||
unitTest(
|
unitTest(
|
||||||
{ perms: { read: true, write: true } },
|
{ perms: { read: true, write: true } },
|
||||||
async function watchFsReturn(): Promise<void> {
|
async function watchFsReturn(): Promise<void> {
|
||||||
|
@ -78,3 +80,21 @@ unitTest(
|
||||||
assertEquals(events, []);
|
assertEquals(events, []);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
unitTest(
|
||||||
|
{ perms: { read: true, write: true } },
|
||||||
|
async function watchFsClose(): Promise<void> {
|
||||||
|
const testDir = await Deno.makeTempDir();
|
||||||
|
const iter = Deno.watchFs(testDir);
|
||||||
|
|
||||||
|
// Asynchronously loop events.
|
||||||
|
const eventsPromise = getTwoEvents(iter);
|
||||||
|
|
||||||
|
// Close the watcher.
|
||||||
|
await iter.close();
|
||||||
|
|
||||||
|
// Expect zero events.
|
||||||
|
const events = await eventsPromise;
|
||||||
|
assertEquals(events, []);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
|
@ -33,11 +33,17 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO(kt3k): This is deprecated. Will be removed in v2.0.
|
||||||
|
// See https://github.com/denoland/deno/issues/10577 for details
|
||||||
return(value) {
|
return(value) {
|
||||||
core.close(this.rid);
|
core.close(this.rid);
|
||||||
return Promise.resolve({ value, done: true });
|
return Promise.resolve({ value, done: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
close() {
|
||||||
|
core.close(this.rid);
|
||||||
|
}
|
||||||
|
|
||||||
[Symbol.asyncIterator]() {
|
[Symbol.asyncIterator]() {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue