Skip to content

Commit 61a99bf

Browse files
committed
Make single per directory native watchers now that we are using it as default
1 parent 4d753d3 commit 61a99bf

File tree

22 files changed

+61
-79
lines changed

22 files changed

+61
-79
lines changed

src/compiler/sys.ts

+61-42
Original file line numberDiff line numberDiff line change
@@ -418,50 +418,41 @@ namespace ts {
418418
}
419419
}
420420

421-
function createSingleFileWatcherPerName(
422-
watchFile: HostWatchFile,
423-
useCaseSensitiveFileNames: boolean
424-
): HostWatchFile {
425-
interface SingleFileWatcher {
426-
watcher: FileWatcher;
427-
refCount: number;
428-
}
429-
const cache = new Map<string, SingleFileWatcher>();
430-
const callbacksCache = createMultiMap<FileWatcherCallback>();
421+
interface SingleFileWatcher<T extends FileWatcherCallback | FsWatchCallback>{
422+
watcher: FileWatcher;
423+
callbacks: T[];
424+
}
425+
function createSingleWatcherPerName<T extends FileWatcherCallback | FsWatchCallback>(
426+
cache: Map<SingleFileWatcher<T>>,
427+
useCaseSensitiveFileNames: boolean,
428+
name: string,
429+
callback: T,
430+
createWatcher: (callback: T) => FileWatcher,
431+
): FileWatcher {
431432
const toCanonicalFileName = createGetCanonicalFileName(useCaseSensitiveFileNames);
433+
const path = toCanonicalFileName(name);
434+
const existing = cache.get(path);
435+
if (existing) {
436+
existing.callbacks.push(callback);
437+
}
438+
else {
439+
cache.set(path, {
440+
watcher: createWatcher((
441+
// Cant infer types correctly so lets satisfy checker
442+
(param1: any, param2: never, param3: any) => cache.get(path)?.callbacks.slice().forEach(cb => cb(param1, param2, param3))
443+
) as T),
444+
callbacks: [callback]
445+
});
446+
}
432447

433-
return (fileName, callback, pollingInterval, options) => {
434-
const path = toCanonicalFileName(fileName);
435-
const existing = cache.get(path);
436-
if (existing) {
437-
existing.refCount++;
438-
}
439-
else {
440-
cache.set(path, {
441-
watcher: watchFile(
442-
fileName,
443-
(fileName, eventKind, modifiedTime) => forEach(
444-
callbacksCache.get(path),
445-
cb => cb(fileName, eventKind, modifiedTime)
446-
),
447-
pollingInterval,
448-
options
449-
),
450-
refCount: 1
451-
});
448+
return {
449+
close: () => {
450+
const watcher = cache.get(path);
451+
if (!watcher) return;
452+
if (!orderedRemoveItem(watcher.callbacks, callback) || watcher.callbacks.length) return;
453+
cache.delete(path);
454+
closeFileWatcherOf(watcher);
452455
}
453-
callbacksCache.add(path, callback);
454-
455-
return {
456-
close: () => {
457-
const watcher = Debug.checkDefined(cache.get(path));
458-
callbacksCache.remove(path, callback);
459-
watcher.refCount--;
460-
if (watcher.refCount) return;
461-
cache.delete(path);
462-
closeFileWatcherOf(watcher);
463-
}
464-
};
465456
};
466457
}
467458

@@ -886,7 +877,9 @@ namespace ts {
886877
inodeWatching,
887878
sysLog,
888879
}: CreateSystemWatchFunctions): { watchFile: HostWatchFile; watchDirectory: HostWatchDirectory; } {
889-
const pollingWatchFile = createSingleFileWatcherPerName(pollingWatchFileWorker, useCaseSensitiveFileNames);
880+
const pollingWatches = new Map<string, SingleFileWatcher<FileWatcherCallback>>();
881+
const fsWatches = new Map<string, SingleFileWatcher<FsWatchCallback>>();
882+
const fsWatchesRecursive = new Map<string, SingleFileWatcher<FsWatchCallback>>();
890883
let dynamicPollingWatchFile: HostWatchFile | undefined;
891884
let fixedChunkSizePollingWatchFile: HostWatchFile | undefined;
892885
let nonPollingWatchFile: HostWatchFile | undefined;
@@ -1064,13 +1057,39 @@ namespace ts {
10641057
}
10651058
}
10661059

1060+
function pollingWatchFile(fileName: string, callback: FileWatcherCallback, pollingInterval: PollingInterval, options: WatchOptions | undefined) {
1061+
return createSingleWatcherPerName(
1062+
pollingWatches,
1063+
useCaseSensitiveFileNames,
1064+
fileName,
1065+
callback,
1066+
cb => pollingWatchFileWorker(fileName, cb, pollingInterval, options),
1067+
);
1068+
}
10671069
function fsWatch(
10681070
fileOrDirectory: string,
10691071
entryKind: FileSystemEntryKind,
10701072
callback: FsWatchCallback,
10711073
recursive: boolean,
10721074
fallbackPollingInterval: PollingInterval,
10731075
fallbackOptions: WatchOptions | undefined
1076+
): FileWatcher {
1077+
return createSingleWatcherPerName(
1078+
recursive ? fsWatchesRecursive : fsWatches,
1079+
useCaseSensitiveFileNames,
1080+
fileOrDirectory,
1081+
callback,
1082+
cb => fsWatchHandlingPresence(fileOrDirectory, entryKind, cb, recursive, fallbackPollingInterval, fallbackOptions),
1083+
);
1084+
}
1085+
1086+
function fsWatchHandlingPresence(
1087+
fileOrDirectory: string,
1088+
entryKind: FileSystemEntryKind,
1089+
callback: FsWatchCallback,
1090+
recursive: boolean,
1091+
fallbackPollingInterval: PollingInterval,
1092+
fallbackOptions: WatchOptions | undefined
10741093
): FileWatcher {
10751094
let lastDirectoryPartWithDirectorySeparator: string | undefined;
10761095
let lastDirectoryPart: string | undefined;

tests/baselines/reference/tscWatch/programUpdates/two-watch-programs-are-not-affected-by-each-other.js

-3
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,10 @@ PolledWatches::
115115
FsWatches::
116116
/a/c/f2.ts:
117117
{}
118-
{}
119118
/a/d/f3.ts:
120119
{}
121-
{}
122120
/a/lib/lib.d.ts:
123121
{}
124-
{}
125122
/a/b/f1.ts:
126123
{}
127124

tests/baselines/reference/tscWatch/projectsWithReferences/on-transitive-references-in-different-folders-with-no-files-clause.js

-15
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,8 @@ FsWatches::
252252
FsWatchesRecursive::
253253
/user/username/projects/transitivereferences/b:
254254
{}
255-
{}
256255
/user/username/projects/transitivereferences/a:
257256
{}
258-
{}
259257
/user/username/projects/transitivereferences/refs:
260258
{}
261259
/user/username/projects/transitivereferences/c:
@@ -419,10 +417,8 @@ FsWatches::
419417
FsWatchesRecursive::
420418
/user/username/projects/transitivereferences/b:
421419
{}
422-
{}
423420
/user/username/projects/transitivereferences/a:
424421
{}
425-
{}
426422
/user/username/projects/transitivereferences/refs:
427423
{}
428424
/user/username/projects/transitivereferences/c:
@@ -519,10 +515,8 @@ FsWatches::
519515
FsWatchesRecursive::
520516
/user/username/projects/transitivereferences/b:
521517
{}
522-
{}
523518
/user/username/projects/transitivereferences/a:
524519
{}
525-
{}
526520
/user/username/projects/transitivereferences/c:
527521
{}
528522
/user/username/projects/transitivereferences/nrefs:
@@ -614,10 +608,8 @@ FsWatches::
614608
FsWatchesRecursive::
615609
/user/username/projects/transitivereferences/b:
616610
{}
617-
{}
618611
/user/username/projects/transitivereferences/a:
619612
{}
620-
{}
621613
/user/username/projects/transitivereferences/c:
622614
{}
623615
/user/username/projects/transitivereferences/refs:
@@ -709,7 +701,6 @@ FsWatches::
709701
FsWatchesRecursive::
710702
/user/username/projects/transitivereferences/b:
711703
{}
712-
{}
713704
/user/username/projects/transitivereferences/a:
714705
{}
715706
/user/username/projects/transitivereferences/c:
@@ -795,7 +786,6 @@ FsWatches::
795786
FsWatchesRecursive::
796787
/user/username/projects/transitivereferences/b:
797788
{}
798-
{}
799789
/user/username/projects/transitivereferences/a:
800790
{}
801791
/user/username/projects/transitivereferences/c:
@@ -976,14 +966,12 @@ FsWatches::
976966
FsWatchesRecursive::
977967
/user/username/projects/transitivereferences/b:
978968
{}
979-
{}
980969
/user/username/projects/transitivereferences/c:
981970
{}
982971
/user/username/projects/transitivereferences/refs:
983972
{}
984973
/user/username/projects/transitivereferences/a:
985974
{}
986-
{}
987975

988976
exitCode:: ExitStatus.undefined
989977

@@ -1074,7 +1062,6 @@ FsWatches::
10741062
FsWatchesRecursive::
10751063
/user/username/projects/transitivereferences/b:
10761064
{}
1077-
{}
10781065
/user/username/projects/transitivereferences/c:
10791066
{}
10801067
/user/username/projects/transitivereferences/refs:
@@ -1170,14 +1157,12 @@ FsWatches::
11701157
FsWatchesRecursive::
11711158
/user/username/projects/transitivereferences/b:
11721159
{}
1173-
{}
11741160
/user/username/projects/transitivereferences/c:
11751161
{}
11761162
/user/username/projects/transitivereferences/refs:
11771163
{}
11781164
/user/username/projects/transitivereferences/a:
11791165
{}
1180-
{}
11811166

11821167
exitCode:: ExitStatus.undefined
11831168

tests/baselines/reference/tscWatch/sourceOfProjectReferenceRedirect/when-packageJson-has-types-field-when-solution-is-already-built.js

-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ FsWatchesRecursive::
239239
{}
240240
/user/username/projects/myproject/packages/a/src:
241241
{}
242-
{}
243242
/user/username/projects/myproject/node_modules:
244243
{}
245244

tests/baselines/reference/tscWatch/sourceOfProjectReferenceRedirect/when-packageJson-has-types-field-with-preserveSymlinks-when-solution-is-already-built.js

-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ FsWatchesRecursive::
239239
{}
240240
/user/username/projects/myproject/packages/a/src:
241241
{}
242-
{}
243242
/user/username/projects/myproject/node_modules:
244243
{}
245244

tests/baselines/reference/tscWatch/sourceOfProjectReferenceRedirect/when-packageJson-has-types-field-with-preserveSymlinks.js

-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ FsWatchesRecursive::
9999
{}
100100
/user/username/projects/myproject/packages/a/src:
101101
{}
102-
{}
103102
/user/username/projects/myproject/node_modules:
104103
{}
105104

tests/baselines/reference/tscWatch/sourceOfProjectReferenceRedirect/when-packageJson-has-types-field-with-scoped-package-when-solution-is-already-built.js

-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ FsWatchesRecursive::
239239
{}
240240
/user/username/projects/myproject/packages/a/src:
241241
{}
242-
{}
243242
/user/username/projects/myproject/node_modules:
244243
{}
245244

tests/baselines/reference/tscWatch/sourceOfProjectReferenceRedirect/when-packageJson-has-types-field-with-scoped-package-with-preserveSymlinks-when-solution-is-already-built.js

-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ FsWatchesRecursive::
239239
{}
240240
/user/username/projects/myproject/packages/a/src:
241241
{}
242-
{}
243242
/user/username/projects/myproject/node_modules:
244243
{}
245244

tests/baselines/reference/tscWatch/sourceOfProjectReferenceRedirect/when-packageJson-has-types-field-with-scoped-package-with-preserveSymlinks.js

-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ FsWatchesRecursive::
9999
{}
100100
/user/username/projects/myproject/packages/a/src:
101101
{}
102-
{}
103102
/user/username/projects/myproject/node_modules:
104103
{}
105104

tests/baselines/reference/tscWatch/sourceOfProjectReferenceRedirect/when-packageJson-has-types-field-with-scoped-package.js

-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ FsWatchesRecursive::
9999
{}
100100
/user/username/projects/myproject/packages/a/src:
101101
{}
102-
{}
103102
/user/username/projects/myproject/node_modules:
104103
{}
105104

tests/baselines/reference/tscWatch/sourceOfProjectReferenceRedirect/when-packageJson-has-types-field.js

-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ FsWatchesRecursive::
9999
{}
100100
/user/username/projects/myproject/packages/a/src:
101101
{}
102-
{}
103102
/user/username/projects/myproject/node_modules:
104103
{}
105104

tests/baselines/reference/tscWatch/sourceOfProjectReferenceRedirect/when-referencing-file-from-subFolder-when-solution-is-already-built.js

-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ FsWatchesRecursive::
239239
{}
240240
/user/username/projects/myproject/packages/a/src:
241241
{}
242-
{}
243242
/user/username/projects/myproject/node_modules:
244243
{}
245244

tests/baselines/reference/tscWatch/sourceOfProjectReferenceRedirect/when-referencing-file-from-subFolder-with-preserveSymlinks-when-solution-is-already-built.js

-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ FsWatchesRecursive::
239239
{}
240240
/user/username/projects/myproject/packages/a/src:
241241
{}
242-
{}
243242
/user/username/projects/myproject/node_modules:
244243
{}
245244

tests/baselines/reference/tscWatch/sourceOfProjectReferenceRedirect/when-referencing-file-from-subFolder-with-preserveSymlinks.js

-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ FsWatchesRecursive::
9999
{}
100100
/user/username/projects/myproject/packages/a/src:
101101
{}
102-
{}
103102
/user/username/projects/myproject/node_modules:
104103
{}
105104

tests/baselines/reference/tscWatch/sourceOfProjectReferenceRedirect/when-referencing-file-from-subFolder-with-scoped-package-when-solution-is-already-built.js

-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ FsWatchesRecursive::
239239
{}
240240
/user/username/projects/myproject/packages/a/src:
241241
{}
242-
{}
243242
/user/username/projects/myproject/node_modules:
244243
{}
245244

tests/baselines/reference/tscWatch/sourceOfProjectReferenceRedirect/when-referencing-file-from-subFolder-with-scoped-package-with-preserveSymlinks-when-solution-is-already-built.js

-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ FsWatchesRecursive::
239239
{}
240240
/user/username/projects/myproject/packages/a/src:
241241
{}
242-
{}
243242
/user/username/projects/myproject/node_modules:
244243
{}
245244

tests/baselines/reference/tscWatch/sourceOfProjectReferenceRedirect/when-referencing-file-from-subFolder-with-scoped-package-with-preserveSymlinks.js

-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ FsWatchesRecursive::
9999
{}
100100
/user/username/projects/myproject/packages/a/src:
101101
{}
102-
{}
103102
/user/username/projects/myproject/node_modules:
104103
{}
105104

tests/baselines/reference/tscWatch/sourceOfProjectReferenceRedirect/when-referencing-file-from-subFolder-with-scoped-package.js

-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ FsWatchesRecursive::
9999
{}
100100
/user/username/projects/myproject/packages/a/src:
101101
{}
102-
{}
103102
/user/username/projects/myproject/node_modules:
104103
{}
105104

tests/baselines/reference/tscWatch/sourceOfProjectReferenceRedirect/when-referencing-file-from-subFolder.js

-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ FsWatchesRecursive::
9999
{}
100100
/user/username/projects/myproject/packages/a/src:
101101
{}
102-
{}
103102
/user/username/projects/myproject/node_modules:
104103
{}
105104

tests/baselines/reference/tsserver/watchEnvironment/when-watchFile-is-single-watcher-per-file.js

-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ PolledWatches::
6161
FsWatches::
6262
/user/username/projects/myproject/tsconfig.json:
6363
{}
64-
{}
6564
/user/username/projects/myproject:
6665
{}
6766
/a/lib/lib.d.ts:

tests/baselines/reference/tsserver/watchEnvironment/with-excludeDirectories-option-in-configFile.js

-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,5 @@ FsWatches::
6666
FsWatchesRecursive::
6767
/user/username/projects/myproject/src:
6868
{}
69-
{}
7069
/user/username/projects/myproject/node_modules:
7170
{}

tests/baselines/reference/tsserver/watchEnvironment/with-excludeDirectories-option-in-configuration.js

-1
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,3 @@ FsWatches::
6666
FsWatchesRecursive::
6767
/user/username/projects/myproject/src:
6868
{}
69-
{}

0 commit comments

Comments
 (0)