Skip to content

Commit 1c7b828

Browse files
committed
vfs: fix lint errors in watch implementation
- Import setInterval/clearInterval from 'timers' module - Use SafeSet from primordials instead of global Set - Use object destructuring for Map iteration - Use optional chaining for timer methods - Use ??= operator for null coalescing assignments - Replace @returns with @throws in abstract methods - Fix test indentation and async IIFE patterns
1 parent b32d2fd commit 1c7b828

File tree

4 files changed

+28
-29
lines changed

4 files changed

+28
-29
lines changed

lib/internal/vfs/module_hooks.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -607,9 +607,8 @@ function installHooks() {
607607
if (typeof options === 'function') {
608608
listener = options;
609609
options = {};
610-
} else if (options == null) {
611-
options = {};
612610
}
611+
options ??= {};
613612

614613
if (typeof filename === 'string' || filename instanceof URL) {
615614
const pathStr = typeof filename === 'string' ? filename : filename.pathname;
@@ -628,9 +627,8 @@ function installHooks() {
628627
if (typeof options === 'function') {
629628
listener = options;
630629
options = {};
631-
} else if (options == null) {
632-
options = {};
633630
}
631+
options ??= {};
634632

635633
if (typeof filename === 'string' || filename instanceof URL) {
636634
const pathStr = typeof filename === 'string' ? filename : filename.pathname;

lib/internal/vfs/provider.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ class VirtualProvider {
510510
* @param {object} [options] Watch options
511511
* @param {number} [options.interval] Polling interval in ms (default: 100)
512512
* @param {boolean} [options.recursive] Watch subdirectories (default: false)
513-
* @returns {EventEmitter} A watcher that emits 'change' events
513+
* @throws {ERR_METHOD_NOT_IMPLEMENTED} When not overridden by subclass
514514
*/
515515
watch(path, options) {
516516
throw new ERR_METHOD_NOT_IMPLEMENTED('watch');
@@ -524,7 +524,7 @@ class VirtualProvider {
524524
* @param {number} [options.interval] Polling interval in ms (default: 100)
525525
* @param {boolean} [options.recursive] Watch subdirectories (default: false)
526526
* @param {AbortSignal} [options.signal] AbortSignal for cancellation
527-
* @returns {AsyncIterable} An async iterable that yields change events
527+
* @throws {ERR_METHOD_NOT_IMPLEMENTED} When not overridden by subclass
528528
*/
529529
watchAsync(path, options) {
530530
throw new ERR_METHOD_NOT_IMPLEMENTED('watchAsync');
@@ -537,7 +537,7 @@ class VirtualProvider {
537537
* @param {object} [options] Watch options
538538
* @param {number} [options.interval] Polling interval in ms (default: 5007)
539539
* @param {boolean} [options.persistent] Whether the watcher should prevent exit
540-
* @returns {EventEmitter} A stat watcher that emits 'change' events
540+
* @throws {ERR_METHOD_NOT_IMPLEMENTED} When not overridden by subclass
541541
*/
542542
watchFile(path, options) {
543543
throw new ERR_METHOD_NOT_IMPLEMENTED('watchFile');

lib/internal/vfs/watcher.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@ const {
55
Promise,
66
PromiseResolve,
77
SafeMap,
8+
SafeSet,
89
Symbol,
910
SymbolAsyncIterator,
1011
} = primordials;
1112

1213
const { EventEmitter } = require('events');
1314
const { basename, join } = require('path');
15+
const {
16+
setInterval,
17+
clearInterval,
18+
} = require('timers');
1419

1520
// Private symbols
1621
const kVfs = Symbol('kVfs');
@@ -114,7 +119,7 @@ class VFSWatcher extends EventEmitter {
114119

115120
// For recursive directory watching, check all tracked files
116121
if (this[kRecursive] && this[kTrackedFiles].size > 0) {
117-
for (const [filePath, info] of this[kTrackedFiles]) {
122+
for (const { 0: filePath, 1: info } of this[kTrackedFiles]) {
118123
const newStats = this._getStatsFor(filePath);
119124
if (this._statsChanged(info.stats, newStats)) {
120125
const eventType = this._determineEventType(info.stats, newStats);
@@ -254,9 +259,7 @@ class VFSWatcher extends EventEmitter {
254259
* @returns {this}
255260
*/
256261
unref() {
257-
if (this[kTimer] && this[kTimer].unref) {
258-
this[kTimer].unref();
259-
}
262+
this[kTimer]?.unref?.();
260263
return this;
261264
}
262265

@@ -265,9 +268,7 @@ class VFSWatcher extends EventEmitter {
265268
* @returns {this}
266269
*/
267270
ref() {
268-
if (this[kTimer] && this[kTimer].ref) {
269-
this[kTimer].ref();
270-
}
271+
this[kTimer]?.ref?.();
271272
return this;
272273
}
273274
}
@@ -294,7 +295,7 @@ class VFSStatWatcher extends EventEmitter {
294295
this[kPersistent] = options.persistent !== false;
295296
this[kClosed] = false;
296297
this[kTimer] = null;
297-
this[kListeners] = new Set();
298+
this[kListeners] = new SafeSet();
298299

299300
// Get initial stats
300301
this[kLastStats] = this._getStats();
@@ -434,9 +435,7 @@ class VFSStatWatcher extends EventEmitter {
434435
* @returns {this}
435436
*/
436437
unref() {
437-
if (this[kTimer] && this[kTimer].unref) {
438-
this[kTimer].unref();
439-
}
438+
this[kTimer]?.unref?.();
440439
return this;
441440
}
442441

@@ -445,9 +444,7 @@ class VFSStatWatcher extends EventEmitter {
445444
* @returns {this}
446445
*/
447446
ref() {
448-
if (this[kTimer] && this[kTimer].ref) {
449-
this[kTimer].ref();
450-
}
447+
this[kTimer]?.ref?.();
451448
return this;
452449
}
453450
}

test/parallel/test-vfs-watch.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ const vfs = require('node:vfs');
1515
assert.strictEqual(typeof watcher.on, 'function');
1616
assert.strictEqual(typeof watcher.close, 'function');
1717

18-
let changeCount = 0;
1918
watcher.on('change', common.mustCall((eventType, filename) => {
20-
changeCount++;
2119
assert.strictEqual(eventType, 'change');
2220
assert.strictEqual(filename, 'file.txt');
2321
watcher.close();
@@ -53,12 +51,15 @@ const vfs = require('node:vfs');
5351
const myVfs = vfs.create();
5452
myVfs.writeFileSync('/listener-test.txt', 'initial');
5553

56-
const watcher = myVfs.watch('/listener-test.txt', { interval: 50, persistent: false },
54+
const watcher = myVfs.watch(
55+
'/listener-test.txt',
56+
{ interval: 50, persistent: false },
5757
common.mustCall((eventType, filename) => {
5858
assert.strictEqual(eventType, 'change');
5959
assert.strictEqual(filename, 'listener-test.txt');
6060
watcher.close();
61-
}));
61+
}),
62+
);
6263

6364
setTimeout(() => {
6465
myVfs.writeFileSync('/listener-test.txt', 'updated');
@@ -70,15 +71,18 @@ const vfs = require('node:vfs');
7071
const myVfs = vfs.create();
7172
myVfs.writeFileSync('/watchfile.txt', 'initial');
7273

73-
const statWatcher = myVfs.watchFile('/watchfile.txt', { interval: 50, persistent: false },
74+
const statWatcher = myVfs.watchFile(
75+
'/watchfile.txt',
76+
{ interval: 50, persistent: false },
7477
common.mustCall((curr, prev) => {
7578
assert.ok(curr);
7679
assert.ok(prev);
7780
assert.strictEqual(curr.isFile(), true);
7881
// Stats should have changed
7982
assert.notStrictEqual(curr.mtimeMs, prev.mtimeMs);
8083
myVfs.unwatchFile('/watchfile.txt');
81-
}));
84+
}),
85+
);
8286

8387
assert.ok(statWatcher);
8488

@@ -291,7 +295,7 @@ const vfs = require('node:vfs');
291295
}
292296
assert.strictEqual(eventCount, 1);
293297
myVfs.unmount();
294-
})();
298+
})().then(common.mustCall());
295299
}
296300

297301
// Test VFS promises.watch directly
@@ -320,7 +324,7 @@ const vfs = require('node:vfs');
320324
ac.abort();
321325
}
322326
assert.strictEqual(eventCount, 1);
323-
})();
327+
})().then(common.mustCall());
324328
}
325329

326330
// Test recursive watching

0 commit comments

Comments
 (0)