2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 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;
|
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 {
|
2021-04-05 12:40:24 -04:00
|
|
|
const value = await core.jsonOpAsync("op_fs_events_poll", this.rid);
|
|
|
|
return value
|
|
|
|
? { value, done: false }
|
|
|
|
: { value: undefined, done: true };
|
2020-07-23 21:33:52 -04:00
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof errors.BadResource) {
|
|
|
|
return { value: undefined, done: true };
|
2020-12-16 11:14:12 -05:00
|
|
|
} else if (error instanceof errors.Interrupted) {
|
|
|
|
return { value: undefined, done: true };
|
2020-07-23 21:33:52 -04:00
|
|
|
}
|
|
|
|
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);
|