Skip to content

Commit 067573b

Browse files
committed
cr feedback: simplify the removeFile function
1 parent 02531af commit 067573b

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

src/compiler/sys.ts

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,15 @@ namespace ts {
311311
const fileWatcherCallbacks = createFileMap<FileWatcherCallback[]>();
312312
return { addFile, removeFile };
313313

314-
function reduceDirWatcherRefCount(dirPath: Path) {
315-
const watcher = dirWatchers.get(dirPath);
316-
watcher.referenceCount -= 1;
317-
if (watcher.referenceCount <= 0) {
318-
watcher.close();
319-
dirWatchers.remove(dirPath);
314+
function reduceDirWatcherRefCountForFile(filePath: Path) {
315+
const dirPath = getDirectoryPath(filePath);
316+
if (dirWatchers.contains(dirPath)) {
317+
const watcher = dirWatchers.get(dirPath);
318+
watcher.referenceCount -= 1;
319+
if (watcher.referenceCount <= 0) {
320+
watcher.close();
321+
dirWatchers.remove(dirPath);
322+
}
320323
}
321324
}
322325

@@ -346,14 +349,6 @@ namespace ts {
346349
}
347350
}
348351

349-
function findWatchedDirForFile(filePath: Path): Path {
350-
const dirPath = getDirectoryPath(filePath);
351-
if (dirWatchers.contains(dirPath)) {
352-
return dirPath;
353-
}
354-
return undefined;
355-
}
356-
357352
function addFile(filePath: Path, callback: FileWatcherCallback): WatchedFile {
358353
addFileWatcherCallback(filePath, callback);
359354
addDirWatcher(getDirectoryPath(filePath));
@@ -362,15 +357,15 @@ namespace ts {
362357
}
363358

364359
function removeFile(watchedFile: WatchedFile) {
365-
const filePath = watchedFile.filePath;
360+
removeFileWatcherCallback(watchedFile.filePath, watchedFile.callback);
361+
reduceDirWatcherRefCountForFile(watchedFile.filePath);
362+
}
363+
364+
function removeFileWatcherCallback(filePath: Path, callback: FileWatcherCallback) {
366365
if (fileWatcherCallbacks.contains(filePath)) {
367-
const newCallbacks = copyListRemovingItem(watchedFile.callback, fileWatcherCallbacks.get(filePath));
366+
const newCallbacks = copyListRemovingItem(callback, fileWatcherCallbacks.get(filePath));
368367
if (newCallbacks.length === 0) {
369368
fileWatcherCallbacks.remove(filePath);
370-
const watchedDir = findWatchedDirForFile(filePath);
371-
if (watchedDir) {
372-
reduceDirWatcherRefCount(watchedDir);
373-
}
374369
}
375370
else {
376371
fileWatcherCallbacks.set(filePath, newCallbacks);

0 commit comments

Comments
 (0)