2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-12-07 08:21:01 -05:00
|
|
|
import { core, primordials } from "ext:core/mod.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
const { BadResourcePrototype, InterruptedPrototype, ops } = core;
|
|
|
|
const {
|
|
|
|
ArrayIsArray,
|
|
|
|
ObjectPrototypeIsPrototypeOf,
|
|
|
|
PromiseResolve,
|
|
|
|
SymbolAsyncIterator,
|
|
|
|
} = primordials;
|
2023-11-01 15:26:12 -04:00
|
|
|
import { SymbolDispose } from "ext:deno_web/00_infra.js";
|
2023-12-26 20:30:26 -05:00
|
|
|
const {
|
|
|
|
op_fs_events_poll,
|
|
|
|
} = core.ensureFastOps();
|
2023-11-01 15:26:12 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
class FsWatcher {
|
|
|
|
#rid = 0;
|
|
|
|
|
|
|
|
constructor(paths, options) {
|
|
|
|
const { recursive } = options;
|
|
|
|
this.#rid = ops.op_fs_events_open({ recursive, paths });
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get rid() {
|
|
|
|
return this.#rid;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
async next() {
|
|
|
|
try {
|
2023-12-26 20:30:26 -05:00
|
|
|
const value = await op_fs_events_poll(this.rid);
|
2023-02-07 14:22:46 -05:00
|
|
|
return value ? { value, done: false } : { value: undefined, done: true };
|
|
|
|
} catch (error) {
|
|
|
|
if (ObjectPrototypeIsPrototypeOf(BadResourcePrototype, error)) {
|
|
|
|
return { value: undefined, done: true };
|
|
|
|
} else if (
|
|
|
|
ObjectPrototypeIsPrototypeOf(InterruptedPrototype, error)
|
|
|
|
) {
|
|
|
|
return { value: undefined, done: true };
|
2020-07-23 21:33:52 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
throw error;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// TODO(kt3k): This is deprecated. Will be removed in v2.0.
|
|
|
|
// See https://github.com/denoland/deno/issues/10577 for details
|
|
|
|
return(value) {
|
|
|
|
core.close(this.rid);
|
|
|
|
return PromiseResolve({ value, done: true });
|
|
|
|
}
|
2021-06-01 02:35:06 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
close() {
|
|
|
|
core.close(this.rid);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
[SymbolAsyncIterator]() {
|
|
|
|
return this;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-11-01 15:26:12 -04:00
|
|
|
|
|
|
|
[SymbolDispose]() {
|
|
|
|
core.tryClose(this.#rid);
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function watchFs(
|
|
|
|
paths,
|
|
|
|
options = { recursive: true },
|
|
|
|
) {
|
|
|
|
return new FsWatcher(ArrayIsArray(paths) ? paths : [paths], options);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
export { watchFs };
|