Simplified mutate map options

This commit is contained in:
Sheetal Nandi 2017-08-14 11:27:02 -07:00
parent d0a23bb876
commit 59d07dc488
4 changed files with 38 additions and 39 deletions

View File

@ -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); }
}
);
}

View File

@ -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));
}
}
/**

View File

@ -3491,27 +3491,25 @@ namespace ts {
/**
* clears already present map by calling onDeleteExistingValue callback before deleting that key/value
*/
export function clearMap<T>(map: Map<T>, onDeleteExistingValue: (key: string, existingValue: T) => void) {
export function clearMap<T>(map: Map<T>, onDeleteValue: (key: string, existingValue: T) => void) {
// Remove all
map.forEach((existingValue, key) => {
onDeleteExistingValue(key, existingValue);
onDeleteValue(key, existingValue);
});
map.clear();
}
export interface MutateMapOptions<T, U> {
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<T, U>(map: Map<T>, newMap: ReadonlyMap<U>, options: MutateMapOptions<T, U>) {
// 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);
}
}
}

View File

@ -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
)
}