Skip to content

Commit 4d753d3

Browse files
committed
Some refactoring
1 parent 10a7b4e commit 4d753d3

File tree

6 files changed

+146
-809
lines changed

6 files changed

+146
-809
lines changed

src/compiler/sys.ts

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ namespace ts {
3535

3636
export type FileWatcherCallback = (fileName: string, eventKind: FileWatcherEventKind, modifiedTime?: Date) => void;
3737
export type DirectoryWatcherCallback = (fileName: string) => void;
38-
/*@internal*/
39-
export interface WatchedFile {
38+
interface WatchedFile {
4039
readonly fileName: string;
4140
readonly callback: FileWatcherCallback;
4241
mtime: Date;
@@ -81,8 +80,7 @@ namespace ts {
8180
/* @internal */
8281
export let unchangedPollThresholds = createPollingIntervalBasedLevels(defaultChunkLevels);
8382

84-
/* @internal */
85-
export function setCustomPollingValues(system: System) {
83+
function setCustomPollingValues(system: System) {
8684
if (!system.getEnvironmentVariable) {
8785
return;
8886
}
@@ -189,31 +187,28 @@ namespace ts {
189187
}
190188
}
191189

192-
/* @internal */
193-
export function createDynamicPriorityPollingWatchFile(host: {
190+
interface WatchedFileWithUnchangedPolls extends WatchedFileWithIsClosed {
191+
unchangedPolls: number;
192+
}
193+
function createDynamicPriorityPollingWatchFile(host: {
194194
getModifiedTime: NonNullable<System["getModifiedTime"]>;
195195
setTimeout: NonNullable<System["setTimeout"]>;
196196
}): HostWatchFile {
197-
interface WatchedFile extends ts.WatchedFile {
198-
isClosed?: boolean;
199-
unchangedPolls: number;
200-
}
201-
202-
interface PollingIntervalQueue extends Array<WatchedFile> {
197+
interface PollingIntervalQueue extends Array<WatchedFileWithUnchangedPolls> {
203198
pollingInterval: PollingInterval;
204199
pollIndex: number;
205200
pollScheduled: boolean;
206201
}
207202

208-
const watchedFiles: WatchedFile[] = [];
209-
const changedFilesInLastPoll: WatchedFile[] = [];
203+
const watchedFiles: WatchedFileWithUnchangedPolls[] = [];
204+
const changedFilesInLastPoll: WatchedFileWithUnchangedPolls[] = [];
210205
const lowPollingIntervalQueue = createPollingIntervalQueue(PollingInterval.Low);
211206
const mediumPollingIntervalQueue = createPollingIntervalQueue(PollingInterval.Medium);
212207
const highPollingIntervalQueue = createPollingIntervalQueue(PollingInterval.High);
213208
return watchFile;
214209

215210
function watchFile(fileName: string, callback: FileWatcherCallback, defaultPollingInterval: PollingInterval): FileWatcher {
216-
const file: WatchedFile = {
211+
const file: WatchedFileWithUnchangedPolls = {
217212
fileName,
218213
callback,
219214
unchangedPolls: 0,
@@ -233,7 +228,7 @@ namespace ts {
233228
}
234229

235230
function createPollingIntervalQueue(pollingInterval: PollingInterval): PollingIntervalQueue {
236-
const queue = [] as WatchedFile[] as PollingIntervalQueue;
231+
const queue = [] as WatchedFileWithUnchangedPolls[] as PollingIntervalQueue;
237232
queue.pollingInterval = pollingInterval;
238233
queue.pollIndex = 0;
239234
queue.pollScheduled = false;
@@ -265,7 +260,7 @@ namespace ts {
265260
}
266261
}
267262

268-
function pollQueue(queue: (WatchedFile | undefined)[], pollingInterval: PollingInterval, pollIndex: number, chunkSize: number) {
263+
function pollQueue(queue: (WatchedFileWithUnchangedPolls | undefined)[], pollingInterval: PollingInterval, pollIndex: number, chunkSize: number) {
269264
return pollWatchedFileQueue(
270265
host,
271266
queue,
@@ -274,7 +269,7 @@ namespace ts {
274269
onWatchFileStat
275270
);
276271

277-
function onWatchFileStat(watchedFile: WatchedFile, pollIndex: number, fileChanged: boolean) {
272+
function onWatchFileStat(watchedFile: WatchedFileWithUnchangedPolls, pollIndex: number, fileChanged: boolean) {
278273
if (fileChanged) {
279274
watchedFile.unchangedPolls = 0;
280275
// Changed files go to changedFilesInLastPoll queue
@@ -311,12 +306,12 @@ namespace ts {
311306
}
312307
}
313308

314-
function addToPollingIntervalQueue(file: WatchedFile, pollingInterval: PollingInterval) {
309+
function addToPollingIntervalQueue(file: WatchedFileWithUnchangedPolls, pollingInterval: PollingInterval) {
315310
pollingIntervalQueue(pollingInterval).push(file);
316311
scheduleNextPollIfNotAlreadyScheduled(pollingInterval);
317312
}
318313

319-
function addChangedFileToLowPollingIntervalQueue(file: WatchedFile) {
314+
function addChangedFileToLowPollingIntervalQueue(file: WatchedFileWithUnchangedPolls) {
320315
changedFilesInLastPoll.push(file);
321316
scheduleNextPollIfNotAlreadyScheduled(PollingInterval.Low);
322317
}
@@ -423,8 +418,7 @@ namespace ts {
423418
}
424419
}
425420

426-
/* @internal */
427-
export function createSingleFileWatcherPerName(
421+
function createSingleFileWatcherPerName(
428422
watchFile: HostWatchFile,
429423
useCaseSensitiveFileNames: boolean
430424
): HostWatchFile {
@@ -474,8 +468,7 @@ namespace ts {
474468
/**
475469
* Returns true if file status changed
476470
*/
477-
/*@internal*/
478-
export function onWatchedFileStat(watchedFile: WatchedFile, modifiedTime: Date): boolean {
471+
function onWatchedFileStat(watchedFile: WatchedFile, modifiedTime: Date): boolean {
479472
const oldTime = watchedFile.mtime.getTime();
480473
const newTime = modifiedTime.getTime();
481474
if (oldTime !== newTime) {
@@ -512,8 +505,7 @@ namespace ts {
512505
curSysLog = logger;
513506
}
514507

515-
/*@internal*/
516-
export interface RecursiveDirectoryWatcherHost {
508+
interface RecursiveDirectoryWatcherHost {
517509
watchDirectory: HostWatchDirectory;
518510
useCaseSensitiveFileNames: boolean;
519511
getCurrentDirectory: System["getCurrentDirectory"];
@@ -529,8 +521,7 @@ namespace ts {
529521
* that means if this is recursive watcher, watch the children directories as well
530522
* (eg on OS that dont support recursive watch using fs.watch use fs.watchFile)
531523
*/
532-
/*@internal*/
533-
export function createDirectoryWatcherSupportingRecursive({
524+
function createDirectoryWatcherSupportingRecursive({
534525
watchDirectory,
535526
useCaseSensitiveFileNames,
536527
getCurrentDirectory,
@@ -792,8 +783,7 @@ namespace ts {
792783
Directory,
793784
}
794785

795-
/*@internal*/
796-
export function createFileWatcherCallback(callback: FsWatchCallback): FileWatcherCallback {
786+
function createFileWatcherCallback(callback: FsWatchCallback): FileWatcherCallback {
797787
return (_fileName, eventKind, modifiedTime) => callback(eventKind === FileWatcherEventKind.Changed ? "change" : "rename", "", modifiedTime);
798788
}
799789

@@ -854,7 +844,7 @@ namespace ts {
854844
/*@internal*/
855845
export interface CreateSystemWatchFunctions {
856846
// Polling watch file
857-
pollingWatchFile: HostWatchFile;
847+
pollingWatchFileWorker: HostWatchFile;
858848
// For dynamic polling watch file
859849
getModifiedTime: NonNullable<System["getModifiedTime"]>;
860850
setTimeout: NonNullable<System["setTimeout"]>;
@@ -878,7 +868,7 @@ namespace ts {
878868

879869
/*@internal*/
880870
export function createSystemWatchFunctions({
881-
pollingWatchFile,
871+
pollingWatchFileWorker,
882872
getModifiedTime,
883873
setTimeout,
884874
clearTimeout,
@@ -896,6 +886,7 @@ namespace ts {
896886
inodeWatching,
897887
sysLog,
898888
}: CreateSystemWatchFunctions): { watchFile: HostWatchFile; watchDirectory: HostWatchDirectory; } {
889+
const pollingWatchFile = createSingleFileWatcherPerName(pollingWatchFileWorker, useCaseSensitiveFileNames);
899890
let dynamicPollingWatchFile: HostWatchFile | undefined;
900891
let fixedChunkSizePollingWatchFile: HostWatchFile | undefined;
901892
let nonPollingWatchFile: HostWatchFile | undefined;
@@ -1445,7 +1436,7 @@ namespace ts {
14451436
const fsSupportsRecursiveFsWatch = isNode4OrLater && (process.platform === "win32" || process.platform === "darwin");
14461437
const getCurrentDirectory = memoize(() => process.cwd());
14471438
const { watchFile, watchDirectory } = createSystemWatchFunctions({
1448-
pollingWatchFile: createSingleFileWatcherPerName(fsWatchFileWorker, useCaseSensitiveFileNames),
1439+
pollingWatchFileWorker: fsWatchFileWorker,
14491440
getModifiedTime,
14501441
setTimeout,
14511442
clearTimeout,

src/harness/virtualFileSystemWithWatch.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,6 @@ interface Array<T> { length: number; [n: number]: T; }`
308308

309309
export enum Tsc_WatchFile {
310310
DynamicPolling = "DynamicPriorityPolling",
311-
SingleFileWatcherPerName = "SingleFileWatcherPerName"
312311
}
313312

314313
export enum Tsc_WatchDirectory {
@@ -386,12 +385,7 @@ interface Array<T> { length: number; [n: number]: T; }`
386385
// We dont have polling watch file
387386
// it is essentially fsWatch but lets get that separate from fsWatch and
388387
// into watchedFiles for easier testing
389-
pollingWatchFile: tscWatchFile === Tsc_WatchFile.SingleFileWatcherPerName ?
390-
createSingleFileWatcherPerName(
391-
this.watchFileWorker.bind(this),
392-
this.useCaseSensitiveFileNames
393-
) :
394-
this.watchFileWorker.bind(this),
388+
pollingWatchFileWorker: this.watchFileWorker.bind(this),
395389
getModifiedTime: this.getModifiedTime.bind(this),
396390
setTimeout: this.setTimeout.bind(this),
397391
clearTimeout: this.clearTimeout.bind(this),

0 commit comments

Comments
 (0)