Use removeItem instead of copyListRemovingItem

This commit is contained in:
Andy Hanson 2016-08-15 11:00:08 -07:00
parent 75cb595f10
commit 5ad7729357
4 changed files with 20 additions and 29 deletions

View file

@ -1394,14 +1394,18 @@ namespace ts {
}
}
export function copyListRemovingItem<T>(item: T, list: T[]) {
const copiedList: T[] = [];
for (const e of list) {
if (e !== item) {
copiedList.push(e);
export function removeItem<T>(item: T, array: T[]): void {
for (let i = 0; i < array.length; i++) {
if (array[i] === item) {
// Move everything over to the left.
// This seems to be faster than either `array.splice(i, 1)` or `array.copyWithin(i, i+ 1)`.
for (; i < array.length - 1; i++) {
array[i] = array[i + 1];
}
array.pop();
break;
}
}
return copiedList;
}
export function createGetCanonicalFileName(useCaseSensitivefileNames: boolean): (fileName: string) => string {

View file

@ -285,13 +285,10 @@ namespace ts {
function removeFileWatcherCallback(filePath: string, callback: FileWatcherCallback) {
const callbacks = fileWatcherCallbacks[filePath];
if (callbacks) {
const newCallbacks = copyListRemovingItem(callback, callbacks);
if (newCallbacks.length === 0) {
removeItem(callback, callbacks);
if (callbacks.length === 0) {
delete fileWatcherCallbacks[filePath];
}
else {
fileWatcherCallbacks[filePath] = newCallbacks;
}
}
}

View file

@ -275,7 +275,7 @@ namespace ts.server {
removeRoot(info: ScriptInfo) {
if (this.filenameToScript.contains(info.path)) {
this.filenameToScript.remove(info.path);
this.roots = copyListRemovingItem(info, this.roots);
removeItem(info, this.roots);
this.resolvedModuleNames.remove(info.path);
this.resolvedTypeReferenceDirectives.remove(info.path);
}
@ -585,16 +585,6 @@ namespace ts.server {
project?: Project;
}
function copyListRemovingItem<T>(item: T, list: T[]) {
const copiedList: T[] = [];
for (let i = 0, len = list.length; i < len; i++) {
if (list[i] != item) {
copiedList.push(list[i]);
}
}
return copiedList;
}
/**
* This helper funciton processes a list of projects and return the concatenated, sortd and deduplicated output of processing each project.
*/
@ -859,7 +849,7 @@ namespace ts.server {
project.directoryWatcher.close();
forEachValue(project.directoriesWatchedForWildcards, watcher => { watcher.close(); });
delete project.directoriesWatchedForWildcards;
this.configuredProjects = copyListRemovingItem(project, this.configuredProjects);
removeItem(project, this.configuredProjects);
}
else {
for (const directory of project.directoriesWatchedForTsconfig) {
@ -871,7 +861,7 @@ namespace ts.server {
delete project.projectService.directoryWatchersForTsconfig[directory];
}
}
this.inferredProjects = copyListRemovingItem(project, this.inferredProjects);
removeItem(project, this.inferredProjects);
}
const fileNames = project.getFileNames();
@ -996,7 +986,7 @@ namespace ts.server {
}
}
else {
this.openFilesReferenced = copyListRemovingItem(info, this.openFilesReferenced);
removeItem(info, this.openFilesReferenced);
}
info.close();
}
@ -1506,13 +1496,13 @@ namespace ts.server {
// openFileRoots or openFileReferenced.
if (info.isOpen) {
if (this.openFileRoots.indexOf(info) >= 0) {
this.openFileRoots = copyListRemovingItem(info, this.openFileRoots);
removeItem(info, this.openFileRoots);
if (info.defaultProject && !info.defaultProject.isConfiguredProject()) {
this.removeProject(info.defaultProject);
}
}
if (this.openFilesReferenced.indexOf(info) >= 0) {
this.openFilesReferenced = copyListRemovingItem(info, this.openFilesReferenced);
removeItem(info, this.openFilesReferenced);
}
this.openFileRootsConfigured.push(info);
info.defaultProject = project;

View file

@ -204,7 +204,7 @@ namespace ts.server {
// average async stat takes about 30 microseconds
// set chunk size to do 30 files in < 1 millisecond
function createPollingWatchedFileSet(interval = 2500, chunkSize = 30) {
let watchedFiles: WatchedFile[] = [];
const watchedFiles: WatchedFile[] = [];
let nextFileToCheck = 0;
let watchTimer: any;
@ -267,7 +267,7 @@ namespace ts.server {
}
function removeFile(file: WatchedFile) {
watchedFiles = copyListRemovingItem(file, watchedFiles);
removeItem(file, watchedFiles);
}
return {