Skip to content

Commit

Permalink
Tweak loop in emitChange
Browse files Browse the repository at this point in the history
  • Loading branch information
james-pre committed Nov 20, 2024
1 parent aa4b6bf commit a375ff5
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/emulation/watchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,24 +171,24 @@ export function removeWatcher(path: string, watcher: FSWatcher) {
}

export function emitChange(eventType: fs.WatchEventType, filename: string) {
let normalizedFilename: string = normalizePath(filename);
filename = normalizePath(filename);
// Notify watchers on the specific file
if (watchers.has(normalizedFilename)) {
for (const watcher of watchers.get(normalizedFilename)!) {
if (watchers.has(filename)) {
for (const watcher of watchers.get(filename)!) {
watcher.emit('change', eventType, basename(filename));
}
}

// Notify watchers on parent directories if they are watching recursively
let parent = dirname(normalizedFilename);
do {
const offset = parent == '/' ? 0 : 1; // used to remove the leading slash
let parent = filename,
normalizedFilename;
while (parent !== normalizedFilename) {
normalizedFilename = parent;
parent = dirname(parent);
if (watchers.has(parent)) {
for (const watcher of watchers.get(parent)!) {
watcher.emit('change', eventType, filename.substring(parent.length + offset));
watcher.emit('change', eventType, filename.slice(parent.length + (parent == '/' ? 0 : 1)));
}
}
normalizedFilename = parent;
parent = dirname(parent);
} while (parent !== normalizedFilename);
}
}

0 comments on commit a375ff5

Please sign in to comment.