Skip to content

Commit ac325de

Browse files
committed
Some refactoring for watchFactory
1 parent b8727a9 commit ac325de

File tree

7 files changed

+126
-163
lines changed

7 files changed

+126
-163
lines changed

src/compiler/tsbuildPublic.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ namespace ts {
224224
originalGetSourceFile: CompilerHost["getSourceFile"];
225225
}
226226

227-
interface SolutionBuilderState<T extends BuilderProgram = BuilderProgram> {
227+
interface SolutionBuilderState<T extends BuilderProgram = BuilderProgram> extends WatchFactory<WatchType, ResolvedConfigFileName> {
228228
readonly host: SolutionBuilderHost<T>;
229229
readonly hostWithWatch: SolutionBuilderWithWatchHost<T>;
230230
readonly currentDirectory: string;
@@ -271,9 +271,6 @@ namespace ts {
271271

272272
timerToBuildInvalidatedProject: any;
273273
reportFileChangeDetected: boolean;
274-
watchFile: WatchFile<WatchType, ResolvedConfigFileName>;
275-
watchFilePath: WatchFilePath<WatchType, ResolvedConfigFileName>;
276-
watchDirectory: WatchDirectory<WatchType, ResolvedConfigFileName>;
277274
writeLog: (s: string) => void;
278275
}
279276

@@ -297,7 +294,7 @@ namespace ts {
297294
loadWithLocalCache<ResolvedModuleFull>(Debug.checkEachDefined(moduleNames), containingFile, redirectedReference, loader);
298295
}
299296

300-
const { watchFile, watchFilePath, watchDirectory, writeLog } = createWatchFactory<ResolvedConfigFileName>(hostWithWatch, options);
297+
const { watchFile, watchDirectory, writeLog } = createWatchFactory<ResolvedConfigFileName>(hostWithWatch, options);
301298

302299
const state: SolutionBuilderState<T> = {
303300
host,
@@ -346,7 +343,6 @@ namespace ts {
346343
timerToBuildInvalidatedProject: undefined,
347344
reportFileChangeDetected: false,
348345
watchFile,
349-
watchFilePath,
350346
watchDirectory,
351347
writeLog,
352348
};
@@ -1796,7 +1792,6 @@ namespace ts {
17961792
function watchConfigFile(state: SolutionBuilderState, resolved: ResolvedConfigFileName, resolvedPath: ResolvedConfigFilePath, parsed: ParsedCommandLine | undefined) {
17971793
if (!state.watch || state.allWatchedConfigFiles.has(resolvedPath)) return;
17981794
state.allWatchedConfigFiles.set(resolvedPath, state.watchFile(
1799-
state.hostWithWatch,
18001795
resolved,
18011796
() => {
18021797
invalidateProjectAndScheduleBuilds(state, resolvedPath, ConfigFileProgramReloadLevel.Full);
@@ -1814,7 +1809,6 @@ namespace ts {
18141809
getOrCreateValueMapFromConfigFileMap(state.allWatchedWildcardDirectories, resolvedPath),
18151810
createMapFromTemplate(parsed.configFileSpecs!.wildcardDirectories),
18161811
(dir, flags) => state.watchDirectory(
1817-
state.hostWithWatch,
18181812
dir,
18191813
fileOrDirectory => {
18201814
if (isIgnoredFileFromWildCardWatching({
@@ -1846,13 +1840,11 @@ namespace ts {
18461840
getOrCreateValueMapFromConfigFileMap(state.allWatchedInputFiles, resolvedPath),
18471841
arrayToMap(parsed.fileNames, fileName => toPath(state, fileName)),
18481842
{
1849-
createNewValue: (path, input) => state.watchFilePath(
1850-
state.hostWithWatch,
1843+
createNewValue: (_path, input) => state.watchFile(
18511844
input,
18521845
() => invalidateProjectAndScheduleBuilds(state, resolvedPath, ConfigFileProgramReloadLevel.None),
18531846
PollingInterval.Low,
18541847
parsed?.watchOptions,
1855-
path as Path,
18561848
WatchType.SourceFile,
18571849
resolved
18581850
),
@@ -1927,9 +1919,7 @@ namespace ts {
19271919
}
19281920

19291921
function reportWatchStatus(state: SolutionBuilderState, message: DiagnosticMessage, ...args: (string | number | undefined)[]) {
1930-
if (state.hostWithWatch.onWatchStatusChange) {
1931-
state.hostWithWatch.onWatchStatusChange(createCompilerDiagnostic(message, ...args), state.host.getNewLine(), state.baseCompilerOptions);
1932-
}
1922+
state.hostWithWatch.onWatchStatusChange?.(createCompilerDiagnostic(message, ...args), state.host.getNewLine(), state.baseCompilerOptions);
19331923
}
19341924

19351925
function reportErrors({ host }: SolutionBuilderState, errors: readonly Diagnostic[]) {

src/compiler/watch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,10 @@ namespace ts {
270270
writeLog: (s: string) => void;
271271
}
272272

273-
export function createWatchFactory<Y = undefined>(host: { trace?(s: string): void; }, options: { extendedDiagnostics?: boolean; diagnostics?: boolean; }) {
273+
export function createWatchFactory<Y = undefined>(host: WatchFactoryHost & { trace?(s: string): void; }, options: { extendedDiagnostics?: boolean; diagnostics?: boolean; }) {
274274
const watchLogLevel = host.trace ? options.extendedDiagnostics ? WatchLogLevel.Verbose : options.diagnostics ? WatchLogLevel.TriggerOnly : WatchLogLevel.None : WatchLogLevel.None;
275275
const writeLog: (s: string) => void = watchLogLevel !== WatchLogLevel.None ? (s => host.trace!(s)) : noop;
276-
const result = getWatchFactory<WatchType, Y>(watchLogLevel, writeLog) as WatchFactory<WatchType, Y>;
276+
const result = getWatchFactory<WatchType, Y>(host, watchLogLevel, writeLog) as WatchFactory<WatchType, Y>;
277277
result.writeLog = writeLog;
278278
return result;
279279
}

src/compiler/watchPublic.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -283,13 +283,13 @@ namespace ts {
283283
newLine = updateNewLine();
284284
}
285285

286-
const { watchFile, watchFilePath, watchDirectory, writeLog } = createWatchFactory<string>(host, compilerOptions);
286+
const { watchFile, watchDirectory, writeLog } = createWatchFactory(host, compilerOptions);
287287
const getCanonicalFileName = createGetCanonicalFileName(useCaseSensitiveFileNames);
288288

289289
writeLog(`Current directory: ${currentDirectory} CaseSensitiveFileNames: ${useCaseSensitiveFileNames}`);
290290
let configFileWatcher: FileWatcher | undefined;
291291
if (configFileName) {
292-
configFileWatcher = watchFile(host, configFileName, scheduleProgramReload, PollingInterval.High, watchOptions, WatchType.ConfigFile);
292+
configFileWatcher = watchFile(configFileName, scheduleProgramReload, PollingInterval.High, watchOptions, WatchType.ConfigFile);
293293
}
294294

295295
const compilerHost = createCompilerHostFromProgramHost(host, () => compilerOptions, directoryStructureHost) as CompilerHost & ResolutionCacheHost;
@@ -305,8 +305,8 @@ namespace ts {
305305
compilerHost.toPath = toPath;
306306
compilerHost.getCompilationSettings = () => compilerOptions;
307307
compilerHost.useSourceOfProjectReferenceRedirect = maybeBind(host, host.useSourceOfProjectReferenceRedirect);
308-
compilerHost.watchDirectoryOfFailedLookupLocation = (dir, cb, flags) => watchDirectory(host, dir, cb, flags, watchOptions, WatchType.FailedLookupLocations);
309-
compilerHost.watchTypeRootsDirectory = (dir, cb, flags) => watchDirectory(host, dir, cb, flags, watchOptions, WatchType.TypeRoots);
308+
compilerHost.watchDirectoryOfFailedLookupLocation = (dir, cb, flags) => watchDirectory(dir, cb, flags, watchOptions, WatchType.FailedLookupLocations);
309+
compilerHost.watchTypeRootsDirectory = (dir, cb, flags) => watchDirectory(dir, cb, flags, watchOptions, WatchType.TypeRoots);
310310
compilerHost.getCachedDirectoryStructureHost = () => cachedDirectoryStructureHost;
311311
compilerHost.scheduleInvalidateResolutionsOfFailedLookupLocations = scheduleInvalidateResolutionsOfFailedLookupLocations;
312312
compilerHost.onInvalidatedResolution = scheduleProgramUpdate;
@@ -488,7 +488,7 @@ namespace ts {
488488
(hostSourceFile as FilePresentOnHost).sourceFile = sourceFile;
489489
hostSourceFile.version = sourceFile.version;
490490
if (!hostSourceFile.fileWatcher) {
491-
hostSourceFile.fileWatcher = watchFilePath(host, fileName, onSourceFileChange, PollingInterval.Low, watchOptions, path, WatchType.SourceFile);
491+
hostSourceFile.fileWatcher = watchFilePath(path, fileName, onSourceFileChange, PollingInterval.Low, watchOptions, WatchType.SourceFile);
492492
}
493493
}
494494
else {
@@ -501,7 +501,7 @@ namespace ts {
501501
}
502502
else {
503503
if (sourceFile) {
504-
const fileWatcher = watchFilePath(host, fileName, onSourceFileChange, PollingInterval.Low, watchOptions, path, WatchType.SourceFile);
504+
const fileWatcher = watchFilePath(path, fileName, onSourceFileChange, PollingInterval.Low, watchOptions, WatchType.SourceFile);
505505
sourceFilesCache.set(path, { sourceFile, version: sourceFile.version, fileWatcher });
506506
}
507507
else {
@@ -675,6 +675,17 @@ namespace ts {
675675
hasChangedConfigFileParsingErrors = true;
676676
}
677677

678+
function watchFilePath(
679+
path: Path,
680+
file: string,
681+
callback: (fileName: string, eventKind: FileWatcherEventKind, filePath: Path) => void,
682+
pollingInterval: PollingInterval,
683+
options: WatchOptions | undefined,
684+
watchType: WatchType
685+
): FileWatcher {
686+
return watchFile(file, (fileName, eventKind) => callback(fileName, eventKind, path), pollingInterval, options, watchType);
687+
}
688+
678689
function onSourceFileChange(fileName: string, eventKind: FileWatcherEventKind, path: Path) {
679690
updateCachedSystemWithFile(fileName, path, eventKind);
680691

@@ -696,7 +707,7 @@ namespace ts {
696707
}
697708

698709
function watchMissingFilePath(missingFilePath: Path) {
699-
return watchFilePath(host, missingFilePath, onMissingFileChange, PollingInterval.Medium, watchOptions, missingFilePath, WatchType.MissingFile);
710+
return watchFilePath(missingFilePath, missingFilePath, onMissingFileChange, PollingInterval.Medium, watchOptions, WatchType.MissingFile);
700711
}
701712

702713
function onMissingFileChange(fileName: string, eventKind: FileWatcherEventKind, missingFilePath: Path) {
@@ -729,7 +740,6 @@ namespace ts {
729740

730741
function watchWildcardDirectory(directory: string, flags: WatchDirectoryFlags) {
731742
return watchDirectory(
732-
host,
733743
directory,
734744
fileOrDirectory => {
735745
Debug.assert(!!configFileName);

0 commit comments

Comments
 (0)