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
|
|
|
|
2024-01-24 08:57:54 -05:00
|
|
|
import { core, internals, primordials } from "ext:core/mod.js";
|
2024-01-26 17:46:46 -05:00
|
|
|
import { op_fs_events_open, op_fs_events_poll } from "ext:core/ops";
|
2024-01-10 17:37:25 -05:00
|
|
|
const {
|
|
|
|
BadResourcePrototype,
|
|
|
|
InterruptedPrototype,
|
|
|
|
} = core;
|
2023-02-07 14:22:46 -05:00
|
|
|
const {
|
|
|
|
ArrayIsArray,
|
|
|
|
ObjectPrototypeIsPrototypeOf,
|
|
|
|
PromiseResolve,
|
|
|
|
SymbolAsyncIterator,
|
|
|
|
} = primordials;
|
2024-01-10 17:37:25 -05:00
|
|
|
|
2023-11-01 15:26:12 -04:00
|
|
|
import { SymbolDispose } from "ext:deno_web/00_infra.js";
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
class FsWatcher {
|
|
|
|
#rid = 0;
|
2024-03-20 01:49:53 -04:00
|
|
|
#promise;
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
constructor(paths, options) {
|
|
|
|
const { recursive } = options;
|
2024-01-10 17:37:25 -05:00
|
|
|
this.#rid = op_fs_events_open({ recursive, paths });
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2024-03-20 01:49:53 -04:00
|
|
|
unref() {
|
|
|
|
core.unrefOpPromise(this.#promise);
|
|
|
|
}
|
|
|
|
|
|
|
|
ref() {
|
|
|
|
core.refOpPromise(this.#promise);
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
async next() {
|
|
|
|
try {
|
2024-03-20 01:49:53 -04:00
|
|
|
this.#promise = op_fs_events_poll(this.#rid);
|
|
|
|
const value = await this.#promise;
|
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) {
|
2024-01-24 12:57:28 -05:00
|
|
|
internals.warnOnDeprecatedApi("Deno.FsWatcher.return()", new Error().stack);
|
|
|
|
core.close(this.#rid);
|
2023-02-07 14:22:46 -05:00
|
|
|
return PromiseResolve({ value, done: true });
|
|
|
|
}
|
2021-06-01 02:35:06 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
close() {
|
2024-01-24 18:05:22 -05:00
|
|
|
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,
|
2024-09-06 06:52:59 -04:00
|
|
|
options = { __proto__: null, recursive: true },
|
2023-02-07 14:22:46 -05:00
|
|
|
) {
|
|
|
|
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 };
|