2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-02-04 17:18:32 -05:00
|
|
|
"use strict";
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
((window) => {
|
2020-09-16 16:22:43 -04:00
|
|
|
const core = window.Deno.core;
|
2022-01-27 10:27:22 -05:00
|
|
|
const { errors } = window.__bootstrap.errors;
|
2021-07-03 18:17:52 -04:00
|
|
|
const {
|
|
|
|
ArrayIsArray,
|
|
|
|
PromiseResolve,
|
|
|
|
SymbolAsyncIterator,
|
|
|
|
} = window.__bootstrap.primordials;
|
2020-07-19 13:49:44 -04:00
|
|
|
class FsWatcher {
|
|
|
|
#rid = 0;
|
|
|
|
|
|
|
|
constructor(paths, options) {
|
|
|
|
const { recursive } = options;
|
2021-04-12 15:55:05 -04:00
|
|
|
this.#rid = core.opSync("op_fs_events_open", { recursive, paths });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
get rid() {
|
|
|
|
return this.#rid;
|
|
|
|
}
|
|
|
|
|
2020-07-23 21:33:52 -04:00
|
|
|
async next() {
|
|
|
|
try {
|
2021-04-12 15:55:05 -04:00
|
|
|
const value = await core.opAsync("op_fs_events_poll", this.rid);
|
2021-04-05 12:40:24 -04:00
|
|
|
return value
|
|
|
|
? { value, done: false }
|
|
|
|
: { value: undefined, done: true };
|
2020-07-23 21:33:52 -04:00
|
|
|
} catch (error) {
|
2022-01-27 10:27:22 -05:00
|
|
|
if (error instanceof errors.BadResource) {
|
2020-07-23 21:33:52 -04:00
|
|
|
return { value: undefined, done: true };
|
2022-01-27 10:27:22 -05:00
|
|
|
} else if (error instanceof errors.Interrupted) {
|
2020-12-16 11:14:12 -05:00
|
|
|
return { value: undefined, done: true };
|
2020-07-23 21:33:52 -04:00
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-06-01 02:35:06 -04:00
|
|
|
// TODO(kt3k): This is deprecated. Will be removed in v2.0.
|
|
|
|
// See https://github.com/denoland/deno/issues/10577 for details
|
2020-07-19 13:49:44 -04:00
|
|
|
return(value) {
|
2020-09-17 12:09:50 -04:00
|
|
|
core.close(this.rid);
|
2021-07-03 18:17:52 -04:00
|
|
|
return PromiseResolve({ value, done: true });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-06-01 02:35:06 -04:00
|
|
|
close() {
|
|
|
|
core.close(this.rid);
|
|
|
|
}
|
|
|
|
|
2021-07-03 18:17:52 -04:00
|
|
|
[SymbolAsyncIterator]() {
|
2020-07-19 13:49:44 -04:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function watchFs(
|
|
|
|
paths,
|
|
|
|
options = { recursive: true },
|
|
|
|
) {
|
2021-07-03 18:17:52 -04:00
|
|
|
return new FsWatcher(ArrayIsArray(paths) ? paths : [paths], options);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
window.__bootstrap.fsEvents = {
|
|
|
|
watchFs,
|
|
|
|
};
|
|
|
|
})(this);
|