2020-07-19 13:49:44 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
((window) => {
|
2020-09-16 16:22:43 -04:00
|
|
|
const core = window.Deno.core;
|
2020-07-23 21:33:52 -04:00
|
|
|
const { errors } = window.__bootstrap.errors;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
class FsWatcher {
|
|
|
|
#rid = 0;
|
|
|
|
|
|
|
|
constructor(paths, options) {
|
|
|
|
const { recursive } = options;
|
2020-09-16 16:22:43 -04:00
|
|
|
this.#rid = core.jsonOpSync("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 {
|
2020-09-16 16:22:43 -04:00
|
|
|
return await core.jsonOpAsync("op_fs_events_poll", {
|
2020-07-23 21:33:52 -04:00
|
|
|
rid: this.rid,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof errors.BadResource) {
|
|
|
|
return { value: undefined, done: true };
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return(value) {
|
2020-09-17 12:09:50 -04:00
|
|
|
core.close(this.rid);
|
2020-07-19 13:49:44 -04:00
|
|
|
return Promise.resolve({ value, done: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
[Symbol.asyncIterator]() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function watchFs(
|
|
|
|
paths,
|
|
|
|
options = { recursive: true },
|
|
|
|
) {
|
|
|
|
return new FsWatcher(Array.isArray(paths) ? paths : [paths], options);
|
|
|
|
}
|
|
|
|
|
|
|
|
window.__bootstrap.fsEvents = {
|
|
|
|
watchFs,
|
|
|
|
};
|
|
|
|
})(this);
|