From 59d07dc4888da2e7ba9c1d9e8529b9141d552473 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Mon, 14 Aug 2017 11:27:02 -0700 Subject: [PATCH] Simplified mutate map options --- src/compiler/builder.ts | 6 +++--- src/compiler/program.ts | 38 ++++++++++++++++++++++---------------- src/compiler/utilities.ts | 31 ++++++++++++------------------- src/server/project.ts | 2 +- 4 files changed, 38 insertions(+), 39 deletions(-) diff --git a/src/compiler/builder.ts b/src/compiler/builder.ts index 19a18f7b9c1..849732f0afb 100644 --- a/src/compiler/builder.ts +++ b/src/compiler/builder.ts @@ -104,9 +104,9 @@ namespace ts { // Add new file info createNewValue: (_path, sourceFile) => addNewFileInfo(program, sourceFile), // Remove existing file info - onDeleteExistingValue: removeExistingFileInfo, + onDeleteValue: removeExistingFileInfo, // We will update in place instead of deleting existing value and adding new one - onExistingValue: (existingInfo, sourceFile) => updateExistingFileInfo(program, existingInfo, sourceFile, hasInvalidatedResolution) + onExistingValue: (_key, existingInfo, sourceFile) => updateExistingFileInfo(program, existingInfo, sourceFile, hasInvalidatedResolution) } ); } @@ -396,7 +396,7 @@ namespace ts { createNewValue: (key): true => { referencedBy.add(key, path); return true; }, // Remove existing reference by entry: source file doesnt reference file 'key' any more // in other words source file (path) is not referenced by 'key' - onDeleteExistingValue: (key, _existingValue) => { referencedBy.remove(key, path); } + onDeleteValue: (key, _existingValue) => { referencedBy.remove(key, path); } } ); } diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 142d6351a46..c642d99ff9d 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -473,7 +473,7 @@ namespace ts { createNewValue: createMissingFileWatch, // Files that are no longer missing (e.g. because they are no longer required) // should no longer be watched. - onDeleteExistingValue: closeExistingMissingFilePathFileWatcher + onDeleteValue: closeExistingMissingFilePathFileWatcher } ); } @@ -497,26 +497,32 @@ namespace ts { wildcardDirectories, { // Create new watch and recursive info - createNewValue: (directory, flags) => { - return { - watcher: watchDirectory(directory, flags), - flags - }; - }, + createNewValue: createWildcardDirectoryWatcher, // Close existing watch thats not needed any more - onDeleteExistingValue: (directory, wildcardDirectoryWatcher) => + onDeleteValue: (directory, wildcardDirectoryWatcher) => closeDirectoryWatcher(directory, wildcardDirectoryWatcher, /*flagsChanged*/ false), // Close existing watch that doesnt match in the flags - shouldDeleteExistingValue: (directory, wildcardDirectoryWatcher, flags) => { - // Watcher is same if the recursive flags match - if (wildcardDirectoryWatcher.flags === flags) { - return false; - } - closeDirectoryWatcher(directory, wildcardDirectoryWatcher, /*flagsChanged*/ true); - return true; - } + onExistingValue: updateWildcardDirectoryWatcher } ); + + function createWildcardDirectoryWatcher(directory: string, flags: WatchDirectoryFlags): WildcardDirectoryWatchers { + // Create new watch and recursive info + return { + watcher: watchDirectory(directory, flags), + flags + }; + } + + function updateWildcardDirectoryWatcher(directory: string, wildcardDirectoryWatcher: WildcardDirectoryWatchers, flags: WatchDirectoryFlags) { + // Watcher needs to be updated if the recursive flags dont match + if (wildcardDirectoryWatcher.flags === flags) { + return; + } + + closeDirectoryWatcher(directory, wildcardDirectoryWatcher, /*flagsChanged*/ true); + existingWatchedForWildcards.set(directory, createWildcardDirectoryWatcher(directory, flags)); + } } /** diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 3f0fae850d6..6e26a287b83 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -3491,27 +3491,25 @@ namespace ts { /** * clears already present map by calling onDeleteExistingValue callback before deleting that key/value */ - export function clearMap(map: Map, onDeleteExistingValue: (key: string, existingValue: T) => void) { + export function clearMap(map: Map, onDeleteValue: (key: string, existingValue: T) => void) { // Remove all map.forEach((existingValue, key) => { - onDeleteExistingValue(key, existingValue); + onDeleteValue(key, existingValue); }); map.clear(); } export interface MutateMapOptions { createNewValue(key: string, valueInNewMap: U): T; - onDeleteExistingValue(key: string, existingValue: T, isNotSame?: boolean): void; + onDeleteValue(key: string, existingValue: T): void; /** - * If present this is called if there is value for the key in new map and existing map + * If present this is called with the key when there is value for that key both in new map as well as existing map provided + * Caller can then decide to update or remove this key. + * If the key is removed, caller will get callback of createNewValue for that key. + * If this callback is not provided, the value of such keys is not updated. */ - onExistingValue?(existingValue: T, valueInNewMap: U): void; - /** - * If there onExistingValue is not provided, this callback if present will be called to - * detemine if the value in the map needs to be deleted - */ - shouldDeleteExistingValue?(key: string, existingValue: T, valueInNewMap: U): boolean; + onExistingValue?(key: string, existingValue: T, valueInNewMap: U): void; } /** @@ -3520,23 +3518,18 @@ namespace ts { export function mutateMap(map: Map, newMap: ReadonlyMap, options: MutateMapOptions) { // If there are new values update them if (newMap) { - const { createNewValue, onDeleteExistingValue, onExistingValue, shouldDeleteExistingValue } = options; + const { createNewValue, onDeleteValue, onExistingValue } = options; // Needs update map.forEach((existingValue, key) => { const valueInNewMap = newMap.get(key); // Not present any more in new map, remove it if (valueInNewMap === undefined) { map.delete(key); - onDeleteExistingValue(key, existingValue); + onDeleteValue(key, existingValue); } // If present notify about existing values else if (onExistingValue) { - onExistingValue(existingValue, valueInNewMap); - } - // different value, delete it here if this value cant be kept around - // Note that if the value is deleted here, new value will be created in newMap.forEach loop for this key - else if (shouldDeleteExistingValue && !shouldDeleteExistingValue(key, existingValue, valueInNewMap)) { - map.delete(key); + onExistingValue(key, existingValue, valueInNewMap); } }); @@ -3549,7 +3542,7 @@ namespace ts { }); } else { - clearMap(map, options.onDeleteExistingValue); + clearMap(map, options.onDeleteValue); } } } diff --git a/src/server/project.ts b/src/server/project.ts index 9984788403b..2233ef1e941 100644 --- a/src/server/project.ts +++ b/src/server/project.ts @@ -1231,7 +1231,7 @@ namespace ts.server { path => this.projectService.onTypeRootFileChanged(this, path), WatchDirectoryFlags.None ), // Close existing watch thats not needed any more - onDeleteExistingValue: (directory, watcher) => this.projectService.closeDirectoryWatcher( + onDeleteValue: (directory, watcher) => this.projectService.closeDirectoryWatcher( WatchType.TypeRoot, this, directory, watcher, WatchDirectoryFlags.None, WatcherCloseReason.NotNeeded ) }