From 74df44480b57c6668e145fd1e3eb7852e4a4eb33 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Fri, 2 Sep 2016 07:16:57 -0700 Subject: [PATCH] Add `multiMapRemove` helper --- src/compiler/core.ts | 15 ++++++++++++++ src/compiler/sys.ts | 8 +------- .../unittests/tsserverProjectSystem.ts | 20 ++++--------------- 3 files changed, 20 insertions(+), 23 deletions(-) diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 092fa597e327d..3a8c78fa29d8c 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -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(map: Map, 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. */ diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index f380401c3b83b..0f066f5dccf2a 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -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) { diff --git a/src/harness/unittests/tsserverProjectSystem.ts b/src/harness/unittests/tsserverProjectSystem.ts index a5648d999704f..37a62f033e0e0 100644 --- a/src/harness/unittests/tsserverProjectSystem.ts +++ b/src/harness/unittests/tsserverProjectSystem.ts @@ -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) }; } @@ -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