Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make single per directory native watchers now that we are using it as…
… default
  • Loading branch information
sheetalkamat committed Aug 18, 2022
commit 61a99bf49426ddd6c6e883a8856f161afa47b4a3
103 changes: 61 additions & 42 deletions src/compiler/sys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,50 +418,41 @@ namespace ts {
}
}

function createSingleFileWatcherPerName(
watchFile: HostWatchFile,
useCaseSensitiveFileNames: boolean
): HostWatchFile {
interface SingleFileWatcher {
watcher: FileWatcher;
refCount: number;
}
const cache = new Map<string, SingleFileWatcher>();
const callbacksCache = createMultiMap<FileWatcherCallback>();
interface SingleFileWatcher<T extends FileWatcherCallback | FsWatchCallback>{
watcher: FileWatcher;
callbacks: T[];
}
function createSingleWatcherPerName<T extends FileWatcherCallback | FsWatchCallback>(
cache: Map<SingleFileWatcher<T>>,
useCaseSensitiveFileNames: boolean,
name: string,
callback: T,
createWatcher: (callback: T) => FileWatcher,
): FileWatcher {
const toCanonicalFileName = createGetCanonicalFileName(useCaseSensitiveFileNames);
const path = toCanonicalFileName(name);
const existing = cache.get(path);
if (existing) {
existing.callbacks.push(callback);
}
else {
cache.set(path, {
watcher: createWatcher((
// Cant infer types correctly so lets satisfy checker
(param1: any, param2: never, param3: any) => cache.get(path)?.callbacks.slice().forEach(cb => cb(param1, param2, param3))
Comment thread
sheetalkamat marked this conversation as resolved.
Comment thread
sheetalkamat marked this conversation as resolved.
) as T),
callbacks: [callback]
});
}

return (fileName, callback, pollingInterval, options) => {
const path = toCanonicalFileName(fileName);
const existing = cache.get(path);
if (existing) {
existing.refCount++;
}
else {
cache.set(path, {
watcher: watchFile(
fileName,
(fileName, eventKind, modifiedTime) => forEach(
callbacksCache.get(path),
cb => cb(fileName, eventKind, modifiedTime)
),
pollingInterval,
options
),
refCount: 1
});
return {
close: () => {
const watcher = cache.get(path);
if (!watcher) return;
Comment thread
sheetalkamat marked this conversation as resolved.
if (!orderedRemoveItem(watcher.callbacks, callback) || watcher.callbacks.length) return;
Comment thread
sheetalkamat marked this conversation as resolved.
Comment thread
sheetalkamat marked this conversation as resolved.
cache.delete(path);
closeFileWatcherOf(watcher);
}
callbacksCache.add(path, callback);

return {
close: () => {
const watcher = Debug.checkDefined(cache.get(path));
callbacksCache.remove(path, callback);
watcher.refCount--;
if (watcher.refCount) return;
cache.delete(path);
closeFileWatcherOf(watcher);
}
};
};
}

Expand Down Expand Up @@ -886,7 +877,9 @@ namespace ts {
inodeWatching,
sysLog,
}: CreateSystemWatchFunctions): { watchFile: HostWatchFile; watchDirectory: HostWatchDirectory; } {
const pollingWatchFile = createSingleFileWatcherPerName(pollingWatchFileWorker, useCaseSensitiveFileNames);
const pollingWatches = new Map<string, SingleFileWatcher<FileWatcherCallback>>();
const fsWatches = new Map<string, SingleFileWatcher<FsWatchCallback>>();
const fsWatchesRecursive = new Map<string, SingleFileWatcher<FsWatchCallback>>();
let dynamicPollingWatchFile: HostWatchFile | undefined;
let fixedChunkSizePollingWatchFile: HostWatchFile | undefined;
let nonPollingWatchFile: HostWatchFile | undefined;
Expand Down Expand Up @@ -1064,13 +1057,39 @@ namespace ts {
}
}

function pollingWatchFile(fileName: string, callback: FileWatcherCallback, pollingInterval: PollingInterval, options: WatchOptions | undefined) {
return createSingleWatcherPerName(
pollingWatches,
useCaseSensitiveFileNames,
fileName,
callback,
cb => pollingWatchFileWorker(fileName, cb, pollingInterval, options),
);
}
function fsWatch(
fileOrDirectory: string,
entryKind: FileSystemEntryKind,
callback: FsWatchCallback,
recursive: boolean,
fallbackPollingInterval: PollingInterval,
fallbackOptions: WatchOptions | undefined
): FileWatcher {
return createSingleWatcherPerName(
recursive ? fsWatchesRecursive : fsWatches,
useCaseSensitiveFileNames,
fileOrDirectory,
callback,
cb => fsWatchHandlingPresence(fileOrDirectory, entryKind, cb, recursive, fallbackPollingInterval, fallbackOptions),
);
}

function fsWatchHandlingPresence(
Comment thread
sheetalkamat marked this conversation as resolved.
Outdated
fileOrDirectory: string,
entryKind: FileSystemEntryKind,
callback: FsWatchCallback,
recursive: boolean,
fallbackPollingInterval: PollingInterval,
fallbackOptions: WatchOptions | undefined
): FileWatcher {
let lastDirectoryPartWithDirectorySeparator: string | undefined;
let lastDirectoryPart: string | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,10 @@ PolledWatches::
FsWatches::
/a/c/f2.ts:
{}
{}
/a/d/f3.ts:
{}
{}
/a/lib/lib.d.ts:
{}
{}
/a/b/f1.ts:
{}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,8 @@ FsWatches::
FsWatchesRecursive::
/user/username/projects/transitivereferences/b:
{}
{}
/user/username/projects/transitivereferences/a:
{}
{}
/user/username/projects/transitivereferences/refs:
{}
/user/username/projects/transitivereferences/c:
Expand Down Expand Up @@ -419,10 +417,8 @@ FsWatches::
FsWatchesRecursive::
/user/username/projects/transitivereferences/b:
{}
{}
/user/username/projects/transitivereferences/a:
{}
{}
/user/username/projects/transitivereferences/refs:
{}
/user/username/projects/transitivereferences/c:
Expand Down Expand Up @@ -519,10 +515,8 @@ FsWatches::
FsWatchesRecursive::
/user/username/projects/transitivereferences/b:
{}
{}
/user/username/projects/transitivereferences/a:
{}
{}
/user/username/projects/transitivereferences/c:
{}
/user/username/projects/transitivereferences/nrefs:
Expand Down Expand Up @@ -614,10 +608,8 @@ FsWatches::
FsWatchesRecursive::
/user/username/projects/transitivereferences/b:
{}
{}
/user/username/projects/transitivereferences/a:
{}
{}
/user/username/projects/transitivereferences/c:
{}
/user/username/projects/transitivereferences/refs:
Expand Down Expand Up @@ -709,7 +701,6 @@ FsWatches::
FsWatchesRecursive::
/user/username/projects/transitivereferences/b:
{}
{}
/user/username/projects/transitivereferences/a:
{}
/user/username/projects/transitivereferences/c:
Expand Down Expand Up @@ -795,7 +786,6 @@ FsWatches::
FsWatchesRecursive::
/user/username/projects/transitivereferences/b:
{}
{}
/user/username/projects/transitivereferences/a:
{}
/user/username/projects/transitivereferences/c:
Expand Down Expand Up @@ -976,14 +966,12 @@ FsWatches::
FsWatchesRecursive::
/user/username/projects/transitivereferences/b:
{}
{}
/user/username/projects/transitivereferences/c:
{}
/user/username/projects/transitivereferences/refs:
{}
/user/username/projects/transitivereferences/a:
{}
{}

exitCode:: ExitStatus.undefined

Expand Down Expand Up @@ -1074,7 +1062,6 @@ FsWatches::
FsWatchesRecursive::
/user/username/projects/transitivereferences/b:
{}
{}
/user/username/projects/transitivereferences/c:
{}
/user/username/projects/transitivereferences/refs:
Expand Down Expand Up @@ -1170,14 +1157,12 @@ FsWatches::
FsWatchesRecursive::
/user/username/projects/transitivereferences/b:
{}
{}
/user/username/projects/transitivereferences/c:
{}
/user/username/projects/transitivereferences/refs:
{}
/user/username/projects/transitivereferences/a:
{}
{}

exitCode:: ExitStatus.undefined

Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ FsWatchesRecursive::
{}
/user/username/projects/myproject/packages/a/src:
{}
{}
/user/username/projects/myproject/node_modules:
{}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ FsWatchesRecursive::
{}
/user/username/projects/myproject/packages/a/src:
{}
{}
/user/username/projects/myproject/node_modules:
{}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ FsWatchesRecursive::
{}
/user/username/projects/myproject/packages/a/src:
{}
{}
/user/username/projects/myproject/node_modules:
{}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ FsWatchesRecursive::
{}
/user/username/projects/myproject/packages/a/src:
{}
{}
/user/username/projects/myproject/node_modules:
{}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ FsWatchesRecursive::
{}
/user/username/projects/myproject/packages/a/src:
{}
{}
/user/username/projects/myproject/node_modules:
{}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ FsWatchesRecursive::
{}
/user/username/projects/myproject/packages/a/src:
{}
{}
/user/username/projects/myproject/node_modules:
{}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ FsWatchesRecursive::
{}
/user/username/projects/myproject/packages/a/src:
{}
{}
/user/username/projects/myproject/node_modules:
{}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ FsWatchesRecursive::
{}
/user/username/projects/myproject/packages/a/src:
{}
{}
/user/username/projects/myproject/node_modules:
{}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ FsWatchesRecursive::
{}
/user/username/projects/myproject/packages/a/src:
{}
{}
/user/username/projects/myproject/node_modules:
{}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ FsWatchesRecursive::
{}
/user/username/projects/myproject/packages/a/src:
{}
{}
/user/username/projects/myproject/node_modules:
{}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ FsWatchesRecursive::
{}
/user/username/projects/myproject/packages/a/src:
{}
{}
/user/username/projects/myproject/node_modules:
{}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ FsWatchesRecursive::
{}
/user/username/projects/myproject/packages/a/src:
{}
{}
/user/username/projects/myproject/node_modules:
{}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ FsWatchesRecursive::
{}
/user/username/projects/myproject/packages/a/src:
{}
{}
/user/username/projects/myproject/node_modules:
{}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ FsWatchesRecursive::
{}
/user/username/projects/myproject/packages/a/src:
{}
{}
/user/username/projects/myproject/node_modules:
{}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ FsWatchesRecursive::
{}
/user/username/projects/myproject/packages/a/src:
{}
{}
/user/username/projects/myproject/node_modules:
{}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ FsWatchesRecursive::
{}
/user/username/projects/myproject/packages/a/src:
{}
{}
/user/username/projects/myproject/node_modules:
{}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ PolledWatches::
FsWatches::
/user/username/projects/myproject/tsconfig.json:
{}
{}
/user/username/projects/myproject:
{}
/a/lib/lib.d.ts:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,5 @@ FsWatches::
FsWatchesRecursive::
/user/username/projects/myproject/src:
{}
{}
/user/username/projects/myproject/node_modules:
{}
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,3 @@ FsWatches::
FsWatchesRecursive::
/user/username/projects/myproject/src:
{}
{}