Skip to content

Commit 9e3d6ef

Browse files
authored
reduce set of files being watched, increase polling interval (#12054) (#12092)
1 parent 2bf38ab commit 9e3d6ef

File tree

8 files changed

+27
-11
lines changed

8 files changed

+27
-11
lines changed

src/compiler/program.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@ namespace ts {
420420
getTypeCount: () => getDiagnosticsProducingTypeChecker().getTypeCount(),
421421
getFileProcessingDiagnostics: () => fileProcessingDiagnostics,
422422
getResolvedTypeReferenceDirectives: () => resolvedTypeReferenceDirectives,
423+
isSourceFileFromExternalLibrary,
423424
dropDiagnosticsProducingTypeChecker
424425
};
425426

@@ -722,13 +723,17 @@ namespace ts {
722723
getSourceFile: program.getSourceFile,
723724
getSourceFileByPath: program.getSourceFileByPath,
724725
getSourceFiles: program.getSourceFiles,
725-
isSourceFileFromExternalLibrary: (file: SourceFile) => !!sourceFilesFoundSearchingNodeModules[file.path],
726+
isSourceFileFromExternalLibrary,
726727
writeFile: writeFileCallback || (
727728
(fileName, data, writeByteOrderMark, onError, sourceFiles) => host.writeFile(fileName, data, writeByteOrderMark, onError, sourceFiles)),
728729
isEmitBlocked,
729730
};
730731
}
731732

733+
function isSourceFileFromExternalLibrary(file: SourceFile): boolean {
734+
return sourceFilesFoundSearchingNodeModules[file.path];
735+
}
736+
732737
function getDiagnosticsProducingTypeChecker() {
733738
return diagnosticsProducingTypeChecker || (diagnosticsProducingTypeChecker = createTypeChecker(program, /*produceDiagnostics:*/ true));
734739
}

src/compiler/sys.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ namespace ts {
1717
readFile(path: string, encoding?: string): string;
1818
getFileSize?(path: string): number;
1919
writeFile(path: string, data: string, writeByteOrderMark?: boolean): void;
20-
watchFile?(path: string, callback: FileWatcherCallback): FileWatcher;
20+
/**
21+
* @pollingInterval - this parameter is used in polling-based watchers and ignored in watchers that
22+
* use native OS file watching
23+
*/
24+
watchFile?(path: string, callback: FileWatcherCallback, pollingInterval?: number): FileWatcher;
2125
watchDirectory?(path: string, callback: DirectoryWatcherCallback, recursive?: boolean): FileWatcher;
2226
resolvePath(path: string): string;
2327
fileExists(path: string): boolean;
@@ -449,15 +453,15 @@ namespace ts {
449453
},
450454
readFile,
451455
writeFile,
452-
watchFile: (fileName, callback) => {
456+
watchFile: (fileName, callback, pollingInterval) => {
453457
if (useNonPollingWatchers) {
454458
const watchedFile = watchedFileSet.addFile(fileName, callback);
455459
return {
456460
close: () => watchedFileSet.removeFile(watchedFile)
457461
};
458462
}
459463
else {
460-
_fs.watchFile(fileName, { persistent: true, interval: 250 }, fileChanged);
464+
_fs.watchFile(fileName, { persistent: true, interval: pollingInterval || 250 }, fileChanged);
461465
return {
462466
close: () => _fs.unwatchFile(fileName, fileChanged)
463467
};

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2192,6 +2192,7 @@ namespace ts {
21922192

21932193
/* @internal */ getFileProcessingDiagnostics(): DiagnosticCollection;
21942194
/* @internal */ getResolvedTypeReferenceDirectives(): Map<ResolvedTypeReferenceDirective>;
2195+
/* @internal */ isSourceFileFromExternalLibrary(file: SourceFile): boolean;
21952196
// For testing purposes only.
21962197
/* @internal */ structureIsReused?: boolean;
21972198
}

src/server/project.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ namespace ts.server {
301301
return this.getLanguageService().getEmitOutput(info.fileName, emitOnlyDtsFiles);
302302
}
303303

304-
getFileNames() {
304+
getFileNames(excludeFilesFromExternalLibraries?: boolean) {
305305
if (!this.program) {
306306
return [];
307307
}
@@ -317,8 +317,14 @@ namespace ts.server {
317317
}
318318
return rootFiles;
319319
}
320-
const sourceFiles = this.program.getSourceFiles();
321-
return sourceFiles.map(sourceFile => asNormalizedPath(sourceFile.fileName));
320+
const result: NormalizedPath[] = [];
321+
for (const f of this.program.getSourceFiles()) {
322+
if (excludeFilesFromExternalLibraries && this.program.isSourceFileFromExternalLibrary(f)) {
323+
continue;
324+
}
325+
result.push(asNormalizedPath(f.fileName));
326+
}
327+
return result;
322328
}
323329

324330
getAllEmittableFiles() {

src/server/types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,6 @@ declare namespace ts.server {
7373
export interface InstallTypingHost extends JsTyping.TypingResolutionHost {
7474
writeFile(path: string, content: string): void;
7575
createDirectory(path: string): void;
76-
watchFile?(path: string, callback: FileWatcherCallback): FileWatcher;
76+
watchFile?(path: string, callback: FileWatcherCallback, pollingInterval?: number): FileWatcher;
7777
}
7878
}

src/server/typingsInstaller/typingsInstaller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ namespace ts.server.typingsInstaller {
367367
this.sendResponse({ projectName: projectName, kind: server.ActionInvalidate });
368368
isInvoked = true;
369369
}
370-
});
370+
}, /*pollingInterval*/ 2000);
371371
watchers.push(w);
372372
}
373373
this.projectWatchers[projectName] = watchers;

src/server/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ namespace ts.server {
5050
export function createInstallTypingsRequest(project: Project, typingOptions: TypingOptions, unresolvedImports: SortedReadonlyArray<string>, cachePath?: string): DiscoverTypings {
5151
return {
5252
projectName: project.getProjectName(),
53-
fileNames: project.getFileNames(),
53+
fileNames: project.getFileNames(/*excludeFilesFromExternalLibraries*/ true),
5454
compilerOptions: project.getCompilerOptions(),
5555
typingOptions,
5656
unresolvedImports,

src/services/jsTyping.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ namespace ts.JsTyping {
8888
exclude = typingOptions.exclude || [];
8989

9090
const possibleSearchDirs = map(fileNames, getDirectoryPath);
91-
if (projectRootPath !== undefined) {
91+
if (projectRootPath) {
9292
possibleSearchDirs.push(projectRootPath);
9393
}
9494
searchDirs = deduplicate(possibleSearchDirs);

0 commit comments

Comments
 (0)