Skip to content

Commit d4cad48

Browse files
author
Andy
authored
Merge pull request #10684 from Microsoft/multi_map_remove
Add `multiMapRemove` helper
2 parents 2379000 + 74df444 commit d4cad48

File tree

3 files changed

+20
-23
lines changed

3 files changed

+20
-23
lines changed

src/compiler/core.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,21 @@ namespace ts {
581581
}
582582
}
583583

584+
/**
585+
* Removes a value from an array of values associated with the key.
586+
* Does not preserve the order of those values.
587+
* Does nothing if `key` is not in `map`, or `value` is not in `map[key]`.
588+
*/
589+
export function multiMapRemove<V>(map: Map<V[]>, key: string, value: V): void {
590+
const values = map[key];
591+
if (values) {
592+
unorderedRemoveItem(values, value);
593+
if (!values.length) {
594+
delete map[key];
595+
}
596+
}
597+
}
598+
584599
/**
585600
* Tests whether a value is an array.
586601
*/

src/compiler/sys.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -283,13 +283,7 @@ namespace ts {
283283
}
284284

285285
function removeFileWatcherCallback(filePath: string, callback: FileWatcherCallback) {
286-
const callbacks = fileWatcherCallbacks[filePath];
287-
if (callbacks) {
288-
unorderedRemoveItem(callbacks, callback);
289-
if (callbacks.length === 0) {
290-
delete fileWatcherCallbacks[filePath];
291-
}
292-
}
286+
multiMapRemove(fileWatcherCallbacks, filePath, callback);
293287
}
294288

295289
function fileEventHandler(eventName: string, relativeFileName: string, baseDirPath: string) {

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -199,16 +199,11 @@ namespace ts {
199199
watchDirectory(directoryName: string, callback: DirectoryWatcherCallback, recursive: boolean): DirectoryWatcher {
200200
const path = this.toPath(directoryName);
201201
const cbWithRecursive = { cb: callback, recursive };
202-
const callbacks = multiMapAdd(this.watchedDirectories, path, cbWithRecursive);
202+
multiMapAdd(this.watchedDirectories, path, cbWithRecursive);
203203
return {
204204
referenceCount: 0,
205205
directoryName,
206-
close: () => {
207-
unorderedRemoveItem(callbacks, cbWithRecursive);
208-
if (!callbacks.length) {
209-
delete this.watchedDirectories[path];
210-
}
211-
}
206+
close: () => multiMapRemove(this.watchedDirectories, path, cbWithRecursive)
212207
};
213208
}
214209

@@ -234,15 +229,8 @@ namespace ts {
234229

235230
watchFile(fileName: string, callback: FileWatcherCallback) {
236231
const path = this.toPath(fileName);
237-
const callbacks = multiMapAdd(this.watchedFiles, path, callback);
238-
return {
239-
close: () => {
240-
unorderedRemoveItem(callbacks, callback);
241-
if (!callbacks.length) {
242-
delete this.watchedFiles[path];
243-
}
244-
}
245-
};
232+
multiMapAdd(this.watchedFiles, path, callback);
233+
return { close: () => multiMapRemove(this.watchedFiles, path, callback) };
246234
}
247235

248236
// TOOD: record and invoke callbacks to simulate timer events

0 commit comments

Comments
 (0)