Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: use createDeferredPromise() in promises.watch() #37386

Merged
merged 1 commit into from
Feb 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 6 additions & 16 deletions lib/internal/fs/watchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const {
FunctionPrototypeCall,
ObjectDefineProperty,
ObjectSetPrototypeOf,
Promise,
Symbol,
} = primordials;

Expand All @@ -15,6 +14,7 @@ const {
ERR_INVALID_ARG_VALUE,
},
} = require('internal/errors');
const { createDeferredPromise } = require('internal/util');

const {
kFsStatsFieldsNumber,
Expand Down Expand Up @@ -319,21 +319,14 @@ async function* watch(filename, options = {}) {
throw new AbortError();

const handle = new FSEvent();
let res;
let rej;
let { promise, resolve, reject } = createDeferredPromise();
const oncancel = () => {
handle.close();
rej(new AbortError());
reject(new AbortError());
};

try {
signal?.addEventListener('abort', oncancel, { once: true });

let promise = new Promise((resolve, reject) => {
res = resolve;
rej = reject;
});

handle.onchange = (status, eventType, filename) => {
if (status < 0) {
const error = uvException({
Expand All @@ -343,11 +336,11 @@ async function* watch(filename, options = {}) {
});
error.filename = filename;
handle.close();
rej(error);
reject(error);
return;
}

res({ eventType, filename });
resolve({ eventType, filename });
};

const err = handle.start(path, persistent, recursive, encoding);
Expand All @@ -366,10 +359,7 @@ async function* watch(filename, options = {}) {

while (!signal?.aborted) {
yield await promise;
promise = new Promise((resolve, reject) => {
res = resolve;
rej = reject;
});
({ promise, resolve, reject } = createDeferredPromise());
}
throw new AbortError();
} finally {
Expand Down