Skip to content

Commit b8727a9

Browse files
committed
Use watch factory in typings installer
1 parent 869abb1 commit b8727a9

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

src/jsTyping/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ declare namespace ts.server {
8383
useCaseSensitiveFileNames: boolean;
8484
writeFile(path: string, content: string): void;
8585
createDirectory(path: string): void;
86-
watchFile?(path: string, callback: FileWatcherCallback, pollingInterval?: number, options?: CompilerOptions): FileWatcher;
87-
watchDirectory?(path: string, callback: DirectoryWatcherCallback, recursive?: boolean, options?: CompilerOptions): FileWatcher;
86+
watchFile?(path: string, callback: FileWatcherCallback, pollingInterval?: number, options?: WatchOptions): FileWatcher;
87+
watchDirectory?(path: string, callback: DirectoryWatcherCallback, recursive?: boolean, options?: WatchOptions): FileWatcher;
8888
}
8989

9090
export interface SetTypings extends ProjectResponse {

src/typingsInstallerCore/typingsInstaller.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ namespace ts.server.typingsInstaller {
8686

8787
type ProjectWatchers = Map<FileWatcher> & { isInvoked?: boolean; };
8888

89+
function getDetailWatchInfo(projectName: string, watchers: ProjectWatchers) {
90+
return `Project: ${projectName} watcher already invoked: ${watchers.isInvoked}`;
91+
}
92+
8993
export abstract class TypingsInstaller {
9094
private readonly packageNameToTypingLocation: Map<JsTyping.CachedTyping> = createMap<JsTyping.CachedTyping>();
9195
private readonly missingTypingsSet: Map<true> = createMap<true>();
@@ -100,6 +104,8 @@ namespace ts.server.typingsInstaller {
100104
private inFlightRequestCount = 0;
101105

102106
abstract readonly typesRegistry: Map<MapLike<string>>;
107+
/*@internal*/
108+
private readonly watchFactory: WatchFactory<string, ProjectWatchers>;
103109

104110
constructor(
105111
protected readonly installTypingHost: InstallTypingHost,
@@ -110,9 +116,11 @@ namespace ts.server.typingsInstaller {
110116
protected readonly log = nullLog) {
111117
this.toCanonicalFileName = createGetCanonicalFileName(installTypingHost.useCaseSensitiveFileNames);
112118
this.globalCachePackageJsonPath = combinePaths(globalCachePath, "package.json");
113-
if (this.log.isEnabled()) {
119+
const isLoggingEnabled = this.log.isEnabled();
120+
if (isLoggingEnabled) {
114121
this.log.writeLine(`Global cache location '${globalCachePath}', safe file path '${safeListPath}', types map path ${typesMapLocation}`);
115122
}
123+
this.watchFactory = getWatchFactory(isLoggingEnabled ? WatchLogLevel.Verbose : WatchLogLevel.None, s => this.log.writeLine(s), getDetailWatchInfo);
116124
this.processCacheLocation(this.globalCachePath);
117125
}
118126

@@ -431,19 +439,13 @@ namespace ts.server.typingsInstaller {
431439
this.log.writeLine(`${projectWatcherType}:: Added:: WatchInfo: ${path}`);
432440
}
433441
const watcher = projectWatcherType === ProjectWatcherType.FileWatcher ?
434-
this.installTypingHost.watchFile!(path, (f, eventKind) => { // TODO: GH#18217
435-
if (isLoggingEnabled) {
436-
this.log.writeLine(`FileWatcher:: Triggered with ${f} eventKind: ${FileWatcherEventKind[eventKind]}:: WatchInfo: ${path}:: handler is already invoked '${watchers.isInvoked}'`);
437-
}
442+
this.watchFactory.watchFile(this.installTypingHost as WatchFileHost, path, () => {
438443
if (!watchers.isInvoked) {
439444
watchers.isInvoked = true;
440445
this.sendResponse({ projectName, kind: ActionInvalidate });
441446
}
442-
}, /*pollingInterval*/ 2000, options) :
443-
this.installTypingHost.watchDirectory!(path, f => { // TODO: GH#18217
444-
if (isLoggingEnabled) {
445-
this.log.writeLine(`DirectoryWatcher:: Triggered with ${f} :: WatchInfo: ${path} recursive :: handler is already invoked '${watchers.isInvoked}'`);
446-
}
447+
}, PollingInterval.High, options, projectName, watchers) :
448+
this.watchFactory.watchDirectory(this.installTypingHost as WatchDirectoryHost, path, f => {
447449
if (watchers.isInvoked || !fileExtensionIs(f, Extension.Json)) {
448450
return;
449451
}
@@ -453,7 +455,7 @@ namespace ts.server.typingsInstaller {
453455
watchers.isInvoked = true;
454456
this.sendResponse({ projectName, kind: ActionInvalidate });
455457
}
456-
}, /*recursive*/ true, options);
458+
}, WatchDirectoryFlags.Recursive, options, projectName, watchers);
457459

458460
watchers.set(canonicalPath, isLoggingEnabled ? {
459461
close: () => {

0 commit comments

Comments
 (0)