Skip to content

Add multiMapRemove helper #10684

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
1 commit merged into from
Sep 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,21 @@ namespace ts {
}
}

/**
* Removes a value from an array of values associated with the key.
* Does not preserve the order of those values.
* Does nothing if `key` is not in `map`, or `value` is not in `map[key]`.
*/
export function multiMapRemove<V>(map: Map<V[]>, key: string, value: V): void {
const values = map[key];
if (values) {
unorderedRemoveItem(values, value);
if (!values.length) {
delete map[key];
}
}
}

/**
* Tests whether a value is an array.
*/
Expand Down
8 changes: 1 addition & 7 deletions src/compiler/sys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,7 @@ namespace ts {
}

function removeFileWatcherCallback(filePath: string, callback: FileWatcherCallback) {
const callbacks = fileWatcherCallbacks[filePath];
if (callbacks) {
unorderedRemoveItem(callbacks, callback);
if (callbacks.length === 0) {
delete fileWatcherCallbacks[filePath];
}
}
multiMapRemove(fileWatcherCallbacks, filePath, callback);
}

function fileEventHandler(eventName: string, relativeFileName: string, baseDirPath: string) {
Expand Down
20 changes: 4 additions & 16 deletions src/harness/unittests/tsserverProjectSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,11 @@ namespace ts {
watchDirectory(directoryName: string, callback: DirectoryWatcherCallback, recursive: boolean): DirectoryWatcher {
const path = this.toPath(directoryName);
const cbWithRecursive = { cb: callback, recursive };
const callbacks = multiMapAdd(this.watchedDirectories, path, cbWithRecursive);
multiMapAdd(this.watchedDirectories, path, cbWithRecursive);
return {
referenceCount: 0,
directoryName,
close: () => {
unorderedRemoveItem(callbacks, cbWithRecursive);
if (!callbacks.length) {
delete this.watchedDirectories[path];
}
}
close: () => multiMapRemove(this.watchedDirectories, path, cbWithRecursive)
};
}

Expand All @@ -234,15 +229,8 @@ namespace ts {

watchFile(fileName: string, callback: FileWatcherCallback) {
const path = this.toPath(fileName);
const callbacks = multiMapAdd(this.watchedFiles, path, callback);
return {
close: () => {
unorderedRemoveItem(callbacks, callback);
if (!callbacks.length) {
delete this.watchedFiles[path];
}
}
};
multiMapAdd(this.watchedFiles, path, callback);
return { close: () => multiMapRemove(this.watchedFiles, path, callback) };
}

// TOOD: record and invoke callbacks to simulate timer events
Expand Down