Skip to content

Commit cabb211

Browse files
committed
Create polling interval as non optional parameter
1 parent c8e2b9c commit cabb211

4 files changed

Lines changed: 92 additions & 82 deletions

File tree

src/compiler/watch.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -252,18 +252,19 @@ namespace ts {
252252
let hasChangedCompilerOptions = false; // True if the compiler options have changed between compilations
253253
let hasChangedAutomaticTypeDirectiveNames = false; // True if the automatic type directives have changed
254254

255-
const loggingEnabled = compilerOptions.diagnostics || compilerOptions.extendedDiagnostics;
256-
const writeLog: (s: string) => void = loggingEnabled ? s => { system.write(s); system.write(system.newLine); } : noop;
257-
const watchFile = compilerOptions.extendedDiagnostics ? ts.addFileWatcherWithLogging : loggingEnabled ? ts.addFileWatcherWithOnlyTriggerLogging : ts.addFileWatcher;
258-
const watchFilePath = compilerOptions.extendedDiagnostics ? ts.addFilePathWatcherWithLogging : ts.addFilePathWatcher;
259-
const watchDirectoryWorker = compilerOptions.extendedDiagnostics ? ts.addDirectoryWatcherWithLogging : ts.addDirectoryWatcher;
255+
const watchLogLevel = compilerOptions.extendedDiagnostics ? WatchLogLevel.Verbose :
256+
compilerOptions.diagnostics ? WatchLogLevel.TriggerOnly : WatchLogLevel.None;
257+
const writeLog: (s: string) => void = watchLogLevel !== WatchLogLevel.None ? s => { system.write(s); system.write(system.newLine); } : noop;
258+
const watchFile = createWatchFile(watchLogLevel, writeLog);
259+
const watchFilePath = createWatchFilePath(watchLogLevel, writeLog);
260+
const watchDirectoryWorker = createWatchDirectory(watchLogLevel, writeLog);
260261

261262
watchingHost = watchingHost || createWatchingSystemHost(compilerOptions.pretty);
262263
const { system, parseConfigFile, reportDiagnostic, reportWatchDiagnostic, beforeCompile, afterCompile } = watchingHost;
263264

264265
const directoryStructureHost = configFileName ? createCachedDirectoryStructureHost(system) : system;
265266
if (configFileName) {
266-
watchFile(system, configFileName, scheduleProgramReload, writeLog);
267+
watchFile(system, configFileName, scheduleProgramReload, /*pollingInterval*/ undefined);
267268
}
268269

269270
const getCurrentDirectory = memoize(() => directoryStructureHost.getCurrentDirectory());
@@ -416,7 +417,7 @@ namespace ts {
416417
hostSourceFile.sourceFile = sourceFile;
417418
sourceFile.version = hostSourceFile.version.toString();
418419
if (!hostSourceFile.fileWatcher) {
419-
hostSourceFile.fileWatcher = watchFilePath(system, fileName, onSourceFileChange, path, writeLog);
420+
hostSourceFile.fileWatcher = watchFilePath(system, fileName, onSourceFileChange, /*pollingInterval*/ undefined, path);
420421
}
421422
}
422423
else {
@@ -429,7 +430,7 @@ namespace ts {
429430
let fileWatcher: FileWatcher;
430431
if (sourceFile) {
431432
sourceFile.version = "0";
432-
fileWatcher = watchFilePath(system, fileName, onSourceFileChange, path, writeLog);
433+
fileWatcher = watchFilePath(system, fileName, onSourceFileChange, /*pollingInterval*/ undefined, path);
433434
sourceFilesCache.set(path, { sourceFile, version: 0, fileWatcher });
434435
}
435436
else {
@@ -599,11 +600,11 @@ namespace ts {
599600
}
600601

601602
function watchDirectory(directory: string, cb: DirectoryWatcherCallback, flags: WatchDirectoryFlags) {
602-
return watchDirectoryWorker(system, directory, cb, flags, writeLog);
603+
return watchDirectoryWorker(system, directory, cb, flags);
603604
}
604605

605606
function watchMissingFilePath(missingFilePath: Path) {
606-
return watchFilePath(system, missingFilePath, onMissingFileChange, missingFilePath, writeLog);
607+
return watchFilePath(system, missingFilePath, onMissingFileChange, /*pollingInterval*/ undefined, missingFilePath);
607608
}
608609

609610
function onMissingFileChange(fileName: string, eventKind: FileWatcherEventKind, missingFilePath: Path) {

src/compiler/watchUtilities.ts

Lines changed: 59 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -90,75 +90,89 @@ namespace ts {
9090
return program.isEmittedFile(file);
9191
}
9292

93-
export function addFileWatcher(host: System, file: string, cb: FileWatcherCallback): FileWatcher {
94-
return host.watchFile(file, cb);
93+
export enum WatchLogLevel {
94+
None,
95+
TriggerOnly,
96+
Verbose
9597
}
9698

97-
export function addFileWatcherWithLogging(host: System, file: string, cb: FileWatcherCallback, log: (s: string) => void): FileWatcher {
98-
const watcherCaption = `FileWatcher:: `;
99-
return createWatcherWithLogging(addFileWatcher, watcherCaption, log, /*logOnlyTrigger*/ false, host, file, cb);
100-
}
99+
export type WatchFile<X, Y> = (host: System, file: string, callback: FileWatcherCallback, pollingInterval?: number, detailInfo1?: X, detailInfo2?: Y) => FileWatcher;
100+
export type FilePathWatcherCallback = (fileName: string, eventKind: FileWatcherEventKind, filePath: Path) => void;
101+
export type WatchFilePath<X, Y> = (host: System, file: string, callback: FilePathWatcherCallback, pollingInterval: number | undefined, path: Path, detailInfo1?: X, detailInfo2?: Y) => FileWatcher;
102+
export type WatchDirectory<X, Y> = (host: System, directory: string, callback: DirectoryWatcherCallback, flags: WatchDirectoryFlags, detailInfo1?: X, detailInfo2?: Y) => FileWatcher;
101103

102-
export function addFileWatcherWithOnlyTriggerLogging(host: System, file: string, cb: FileWatcherCallback, log: (s: string) => void): FileWatcher {
103-
const watcherCaption = `FileWatcher:: `;
104-
return createWatcherWithLogging(addFileWatcher, watcherCaption, log, /*logOnlyTrigger*/ true, host, file, cb);
104+
export function createWatchFile<X = undefined, Y = undefined>(watchLogLevel: WatchLogLevel, log: (s: string) => void, getDetailWatchInfo?: GetDetailWatchInfo<X, Y>): WatchFile<X, Y> {
105+
const createFileWatcher: CreateFileWatcher<number | undefined, FileWatcherEventKind, undefined, X, Y> = getCreateFileWatcher(watchLogLevel, watchFile);
106+
return (host, file, callback, pollingInterval, detailInfo1, detailInfo2) =>
107+
createFileWatcher(host, file, callback, pollingInterval, /*passThrough*/ undefined, detailInfo1, detailInfo2, watchFile, log, "FileWatcher", getDetailWatchInfo);
105108
}
106109

107-
export type FilePathWatcherCallback = (fileName: string, eventKind: FileWatcherEventKind, filePath: Path) => void;
108-
export function addFilePathWatcher(host: System, file: string, cb: FilePathWatcherCallback, path: Path): FileWatcher {
109-
return host.watchFile(file, (fileName, eventKind) => cb(fileName, eventKind, path));
110+
export function createWatchFilePath<X = undefined, Y = undefined>(watchLogLevel: WatchLogLevel, log: (s: string) => void, getDetailWatchInfo?: GetDetailWatchInfo<X, Y>): WatchFilePath<X, Y> {
111+
const createFileWatcher: CreateFileWatcher<number | undefined, FileWatcherEventKind, Path, X, Y> = getCreateFileWatcher(watchLogLevel, watchFilePath);
112+
return (host, file, callback, pollingInterval, path, detailInfo1, detailInfo2) =>
113+
createFileWatcher(host, file, callback, pollingInterval, path, detailInfo1, detailInfo2, watchFile, log, "FileWatcher", getDetailWatchInfo);
110114
}
111115

112-
export function addFilePathWatcherWithLogging(host: System, file: string, cb: FilePathWatcherCallback, path: Path, log: (s: string) => void): FileWatcher {
113-
const watcherCaption = `FileWatcher:: `;
114-
return createWatcherWithLogging(addFileWatcher, watcherCaption, log, /*logOnlyTrigger*/ false, host, file, cb, path);
116+
export function createWatchDirectory<X = undefined, Y = undefined>(watchLogLevel: WatchLogLevel, log: (s: string) => void, getDetailWatchInfo?: GetDetailWatchInfo<X, Y>): WatchDirectory<X, Y> {
117+
const createFileWatcher: CreateFileWatcher<WatchDirectoryFlags, undefined, undefined, X, Y> = getCreateFileWatcher(watchLogLevel, watchDirectory);
118+
return (host, directory, callback, flags, detailInfo1, detailInfo2) =>
119+
createFileWatcher(host, directory, callback, flags, /*passThrough*/ undefined, detailInfo1, detailInfo2, watchDirectory, log, "DirectoryWatcher", getDetailWatchInfo);
115120
}
116121

117-
export function addFilePathWatcherWithOnlyTriggerLogging(host: System, file: string, cb: FilePathWatcherCallback, path: Path, log: (s: string) => void): FileWatcher {
118-
const watcherCaption = `FileWatcher:: `;
119-
return createWatcherWithLogging(addFileWatcher, watcherCaption, log, /*logOnlyTrigger*/ true, host, file, cb, path);
122+
function watchFile(host: System, file: string, callback: FileWatcherCallback, pollingInterval?: number): FileWatcher {
123+
return host.watchFile(file, callback, pollingInterval);
120124
}
121125

122-
export function addDirectoryWatcher(host: System, directory: string, cb: DirectoryWatcherCallback, flags: WatchDirectoryFlags): FileWatcher {
123-
const recursive = (flags & WatchDirectoryFlags.Recursive) !== 0;
124-
return host.watchDirectory(directory, cb, recursive);
126+
function watchFilePath(host: System, file: string, callback: FilePathWatcherCallback, pollingInterval: number | undefined, path: Path): FileWatcher {
127+
return host.watchFile(file, (fileName, eventKind) => callback(fileName, eventKind, path), pollingInterval);
125128
}
126129

127-
export function addDirectoryWatcherWithLogging(host: System, directory: string, cb: DirectoryWatcherCallback, flags: WatchDirectoryFlags, log: (s: string) => void): FileWatcher {
128-
const watcherCaption = `DirectoryWatcher ${(flags & WatchDirectoryFlags.Recursive) !== 0 ? "recursive" : ""}:: `;
129-
return createWatcherWithLogging(addDirectoryWatcher, watcherCaption, log, /*logOnlyTrigger*/ false, host, directory, cb, flags);
130+
function watchDirectory(host: System, directory: string, callback: DirectoryWatcherCallback, flags: WatchDirectoryFlags): FileWatcher {
131+
return host.watchDirectory(directory, callback, (flags & WatchDirectoryFlags.Recursive) !== 0);
130132
}
131133

132-
export function addDirectoryWatcherWithOnlyTriggerLogging(host: System, directory: string, cb: DirectoryWatcherCallback, flags: WatchDirectoryFlags, log: (s: string) => void): FileWatcher {
133-
const watcherCaption = `DirectoryWatcher ${(flags & WatchDirectoryFlags.Recursive) !== 0 ? "recursive" : ""}:: `;
134-
return createWatcherWithLogging(addDirectoryWatcher, watcherCaption, log, /*logOnlyTrigger*/ true, host, directory, cb, flags);
135-
}
134+
export type WatchCallback<T, U> = (fileName: string, cbOptional?: T, passThrough?: U) => void;
135+
export type AddWatch<T, U, V> = (host: System, file: string, cb: WatchCallback<U, V>, flags: T, passThrough?: V, detailInfo1?: undefined, detailInfo2?: undefined) => FileWatcher;
136+
export type GetDetailWatchInfo<X, Y> = (detailInfo1: X, detailInfo2: Y) => string;
136137

137-
type WatchCallback<T, U> = (fileName: string, cbOptional1?: T, optional?: U) => void;
138-
type AddWatch<T, U> = (host: System, file: string, cb: WatchCallback<T, U>, optional?: U) => FileWatcher;
139-
function createWatcherWithLogging<T, U>(addWatch: AddWatch<T, U>, watcherCaption: string, log: (s: string) => void, logOnlyTrigger: boolean, host: System, file: string, cb: WatchCallback<T, U>, optional?: U): FileWatcher {
140-
const info = `PathInfo: ${file}`;
141-
if (!logOnlyTrigger) {
142-
log(`${watcherCaption}Added: ${info}`);
138+
type CreateFileWatcher<T, U, V, X, Y> = (host: System, file: string, cb: WatchCallback<U, V>, flags: T, passThrough: V | undefined, detailInfo1: X | undefined, detailInfo2: Y | undefined, addWatch: AddWatch<T, U, V>, log: (s: string) => void, watchCaption: string, getDetailWatchInfo: GetDetailWatchInfo<X, Y> | undefined) => FileWatcher;
139+
function getCreateFileWatcher<T, U, V, X, Y>(watchLogLevel: WatchLogLevel, addWatch: AddWatch<T, U, V>): CreateFileWatcher<T, U, V, X, Y> {
140+
switch (watchLogLevel) {
141+
case WatchLogLevel.None:
142+
return addWatch;
143+
case WatchLogLevel.TriggerOnly:
144+
return createFileWatcherWithLogging;
145+
case WatchLogLevel.Verbose:
146+
return createFileWatcherWithTriggerLogging;
143147
}
144-
const watcher = addWatch(host, file, (fileName, cbOptional1?) => {
145-
const optionalInfo = cbOptional1 !== undefined ? ` ${cbOptional1}` : "";
146-
log(`${watcherCaption}Trigger: ${fileName}${optionalInfo} ${info}`);
147-
const start = timestamp();
148-
cb(fileName, cbOptional1, optional);
149-
const elapsed = timestamp() - start;
150-
log(`${watcherCaption}Elapsed: ${elapsed}ms Trigger: ${fileName}${optionalInfo} ${info}`);
151-
}, optional);
148+
}
149+
150+
function createFileWatcherWithLogging<T, U, V, X, Y>(host: System, file: string, cb: WatchCallback<U, V>, flags: T, passThrough: V | undefined, detailInfo1: X | undefined, detailInfo2: Y | undefined, addWatch: AddWatch<T, U, undefined>, log: (s: string) => void, watchCaption: string, getDetailWatchInfo: GetDetailWatchInfo<X, Y> | undefined): FileWatcher {
151+
log(`${watchCaption}:: Added:: ${getWatchInfo(file, flags, detailInfo1, detailInfo2, getDetailWatchInfo)}`);
152+
const watcher = createFileWatcherWithTriggerLogging(host, file, cb, flags, passThrough, detailInfo1, detailInfo2, addWatch, log, watchCaption, getDetailWatchInfo);
152153
return {
153154
close: () => {
154-
if (!logOnlyTrigger) {
155-
log(`${watcherCaption}Close: ${info}`);
156-
}
155+
log(`${watchCaption}:: Close:: ${getWatchInfo(file, flags, detailInfo1, detailInfo2, getDetailWatchInfo)}`);
157156
watcher.close();
158157
}
159158
};
160159
}
161160

161+
function createFileWatcherWithTriggerLogging<T, U, V, X, Y>(host: System, file: string, cb: WatchCallback<U, V>, flags: T, passThrough: V | undefined, detailInfo1: X | undefined, detailInfo2: Y | undefined, addWatch: AddWatch<T, U, undefined>, log: (s: string) => void, watchCaption: string, getDetailWatchInfo: GetDetailWatchInfo<X, Y> | undefined): FileWatcher {
162+
return addWatch(host, file, (fileName, cbOptional) => {
163+
const triggerredInfo = `${watchCaption}:: Triggered with ${fileName}${cbOptional !== undefined ? cbOptional : ""}:: ${getWatchInfo(file, flags, detailInfo1, detailInfo2, getDetailWatchInfo)}`;
164+
log(triggerredInfo);
165+
const start = timestamp();
166+
cb(fileName, cbOptional, passThrough);
167+
const elapsed = timestamp() - start;
168+
log(`Elapsed:: ${elapsed}ms ${triggerredInfo}`);
169+
}, flags);
170+
}
171+
172+
function getWatchInfo<T, X, Y>(file: string, flags: T, detailInfo1: X | undefined, detailInfo2: Y | undefined, getDetailWatchInfo: GetDetailWatchInfo<X, Y> | undefined) {
173+
return `WatchInfo: ${file} ${flags} ${getDetailWatchInfo ? getDetailWatchInfo(detailInfo1, detailInfo2) : ""}`
174+
}
175+
162176
export function closeFileWatcher(watcher: FileWatcher) {
163177
watcher.close();
164178
}

0 commit comments

Comments
 (0)