From 067573b0c3e387835452f1d703b877f055f4cbc5 Mon Sep 17 00:00:00 2001 From: Zhengbo Li Date: Wed, 13 Jan 2016 14:02:34 -0800 Subject: [PATCH] cr feedback: simplify the removeFile function --- src/compiler/sys.ts | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index 822ea5c192..073a662ce1 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -311,12 +311,15 @@ namespace ts { const fileWatcherCallbacks = createFileMap(); return { addFile, removeFile }; - function reduceDirWatcherRefCount(dirPath: Path) { - const watcher = dirWatchers.get(dirPath); - watcher.referenceCount -= 1; - if (watcher.referenceCount <= 0) { - watcher.close(); - dirWatchers.remove(dirPath); + function reduceDirWatcherRefCountForFile(filePath: Path) { + const dirPath = getDirectoryPath(filePath); + if (dirWatchers.contains(dirPath)) { + const watcher = dirWatchers.get(dirPath); + watcher.referenceCount -= 1; + if (watcher.referenceCount <= 0) { + watcher.close(); + dirWatchers.remove(dirPath); + } } } @@ -346,14 +349,6 @@ namespace ts { } } - function findWatchedDirForFile(filePath: Path): Path { - const dirPath = getDirectoryPath(filePath); - if (dirWatchers.contains(dirPath)) { - return dirPath; - } - return undefined; - } - function addFile(filePath: Path, callback: FileWatcherCallback): WatchedFile { addFileWatcherCallback(filePath, callback); addDirWatcher(getDirectoryPath(filePath)); @@ -362,15 +357,15 @@ namespace ts { } function removeFile(watchedFile: WatchedFile) { - const filePath = watchedFile.filePath; + removeFileWatcherCallback(watchedFile.filePath, watchedFile.callback); + reduceDirWatcherRefCountForFile(watchedFile.filePath); + } + + function removeFileWatcherCallback(filePath: Path, callback: FileWatcherCallback) { if (fileWatcherCallbacks.contains(filePath)) { - const newCallbacks = copyListRemovingItem(watchedFile.callback, fileWatcherCallbacks.get(filePath)); + const newCallbacks = copyListRemovingItem(callback, fileWatcherCallbacks.get(filePath)); if (newCallbacks.length === 0) { fileWatcherCallbacks.remove(filePath); - const watchedDir = findWatchedDirForFile(filePath); - if (watchedDir) { - reduceDirWatcherRefCount(watchedDir); - } } else { fileWatcherCallbacks.set(filePath, newCallbacks);