mirror of
https://github.com/denoland/deno.git
synced 2024-12-24 16:19:12 -05:00
fix(ext/node): FsWatcher ref and unref (#22987)
Fixes https://github.com/denoland/deno/issues/22973 --------- Co-authored-by: Satya Rohith <me@satyarohith.com>
This commit is contained in:
parent
737adbe1b0
commit
5b2f689f08
3 changed files with 35 additions and 7 deletions
|
@ -146,7 +146,7 @@ export function watch(
|
||||||
}
|
}
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
});
|
}, () => iterator);
|
||||||
|
|
||||||
if (listener) {
|
if (listener) {
|
||||||
fsWatcher.on("change", listener.bind({ _handle: fsWatcher }));
|
fsWatcher.on("change", listener.bind({ _handle: fsWatcher }));
|
||||||
|
@ -252,6 +252,7 @@ class StatWatcher extends EventEmitter {
|
||||||
#bigint: boolean;
|
#bigint: boolean;
|
||||||
#refCount = 0;
|
#refCount = 0;
|
||||||
#abortController = new AbortController();
|
#abortController = new AbortController();
|
||||||
|
|
||||||
constructor(bigint: boolean) {
|
constructor(bigint: boolean) {
|
||||||
super();
|
super();
|
||||||
this.#bigint = bigint;
|
this.#bigint = bigint;
|
||||||
|
@ -306,19 +307,22 @@ class StatWatcher extends EventEmitter {
|
||||||
this.emit("stop");
|
this.emit("stop");
|
||||||
}
|
}
|
||||||
ref() {
|
ref() {
|
||||||
notImplemented("FSWatcher.ref() is not implemented");
|
notImplemented("StatWatcher.ref() is not implemented");
|
||||||
}
|
}
|
||||||
unref() {
|
unref() {
|
||||||
notImplemented("FSWatcher.unref() is not implemented");
|
notImplemented("StatWatcher.unref() is not implemented");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class FSWatcher extends EventEmitter {
|
class FSWatcher extends EventEmitter {
|
||||||
#closer: () => void;
|
#closer: () => void;
|
||||||
#closed = false;
|
#closed = false;
|
||||||
constructor(closer: () => void) {
|
#watcher: () => Deno.FsWatcher;
|
||||||
|
|
||||||
|
constructor(closer: () => void, getter: () => Deno.FsWatcher) {
|
||||||
super();
|
super();
|
||||||
this.#closer = closer;
|
this.#closer = closer;
|
||||||
|
this.#watcher = getter;
|
||||||
}
|
}
|
||||||
close() {
|
close() {
|
||||||
if (this.#closed) {
|
if (this.#closed) {
|
||||||
|
@ -329,10 +333,10 @@ class FSWatcher extends EventEmitter {
|
||||||
this.#closer();
|
this.#closer();
|
||||||
}
|
}
|
||||||
ref() {
|
ref() {
|
||||||
notImplemented("FSWatcher.ref() is not implemented");
|
this.#watcher().ref();
|
||||||
}
|
}
|
||||||
unref() {
|
unref() {
|
||||||
notImplemented("FSWatcher.unref() is not implemented");
|
this.#watcher().unref();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,7 @@ import { SymbolDispose } from "ext:deno_web/00_infra.js";
|
||||||
|
|
||||||
class FsWatcher {
|
class FsWatcher {
|
||||||
#rid = 0;
|
#rid = 0;
|
||||||
|
#promise;
|
||||||
|
|
||||||
constructor(paths, options) {
|
constructor(paths, options) {
|
||||||
const { recursive } = options;
|
const { recursive } = options;
|
||||||
|
@ -32,9 +33,18 @@ class FsWatcher {
|
||||||
return this.#rid;
|
return this.#rid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unref() {
|
||||||
|
core.unrefOpPromise(this.#promise);
|
||||||
|
}
|
||||||
|
|
||||||
|
ref() {
|
||||||
|
core.refOpPromise(this.#promise);
|
||||||
|
}
|
||||||
|
|
||||||
async next() {
|
async next() {
|
||||||
try {
|
try {
|
||||||
const value = await op_fs_events_poll(this.#rid);
|
this.#promise = op_fs_events_poll(this.#rid);
|
||||||
|
const value = await this.#promise;
|
||||||
return value ? { value, done: false } : { value: undefined, done: true };
|
return value ? { value, done: false } : { value: undefined, done: true };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (ObjectPrototypeIsPrototypeOf(BadResourcePrototype, error)) {
|
if (ObjectPrototypeIsPrototypeOf(BadResourcePrototype, error)) {
|
||||||
|
|
|
@ -38,3 +38,17 @@ Deno.test({
|
||||||
unwatchFile(file);
|
unwatchFile(file);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test({
|
||||||
|
name: "watch.unref() should work",
|
||||||
|
sanitizeOps: false,
|
||||||
|
sanitizeResources: false,
|
||||||
|
async fn() {
|
||||||
|
const file = Deno.makeTempFileSync();
|
||||||
|
const watcher = watch(file, () => {});
|
||||||
|
// Wait for the watcher to be initialized
|
||||||
|
await wait(10);
|
||||||
|
// @ts-ignore node types are outdated in deno.
|
||||||
|
watcher.unref();
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
Loading…
Reference in a new issue