Use FileMap instead of string array

This commit is contained in:
Zhengbo Li 2015-12-15 08:39:51 -08:00
parent 36cc0e017b
commit d41901576a

View file

@ -295,47 +295,52 @@ namespace ts {
} }
function createWatchedFileSet() { function createWatchedFileSet() {
const watchedDirectories: { [path: string]: FileWatcher } = {}; const watchedDirectories = createFileMap<FileWatcher>();
const watchedFiles: { [fileName: string]: (fileName: string, removed?: boolean) => void; } = {}; const watchedFiles = createFileMap<(fileName: string, removed?: boolean) => void>();
const currentDirectory = process.cwd();
return { addFile, removeFile };
function addFile(fileName: string, callback: (fileName: string, removed?: boolean) => void): WatchedFile { function addFile(fileName: string, callback: (fileName: string, removed?: boolean) => void): WatchedFile {
const file: WatchedFile = { fileName, callback }; const path = toPath(fileName, currentDirectory, getCanonicalPath);
const watchedPaths = Object.keys(watchedDirectories); const parentDirPath = toPath(ts.getDirectoryPath(fileName), currentDirectory, getCanonicalPath);
// Try to find parent paths that are already watched. If found, don't add directory watchers
const watchedParentPaths = watchedPaths.filter(path => fileName.indexOf(path) === 0); if (!watchedDirectories.contains(parentDirPath)) {
// If adding new watchers, try to find children paths that are already watched. If found, close them. watchedDirectories.set(parentDirPath, _fs.watch(
if (watchedParentPaths.length === 0) { parentDirPath,
const pathToWatch = ts.getDirectoryPath(fileName); (eventName: string, relativeFileName: string) => fileEventHandler(eventName, relativeFileName, parentDirPath)
for (const watchedPath in watchedDirectories) { ));
if (watchedPath.indexOf(pathToWatch) === 0) {
watchedDirectories[watchedPath].close();
delete watchedDirectories[watchedPath];
}
}
watchedDirectories[pathToWatch] = _fs.watch(
pathToWatch,
(eventName: string, relativeFileName: string) => fileEventHandler(eventName, ts.normalizePath(ts.combinePaths(pathToWatch, relativeFileName)))
);
} }
watchedFiles[fileName] = callback; watchedFiles.set(path, callback);
return { fileName, callback }; return { fileName, callback };
} }
function removeFile(file: WatchedFile) { function removeFile(file: WatchedFile) {
delete watchedFiles[file.fileName]; const path = toPath(file.fileName, currentDirectory, getCanonicalPath);
} watchedFiles.remove(path);
function fileEventHandler(eventName: string, fileName: string) { const parentDirPath = toPath(ts.getDirectoryPath(path), currentDirectory, getCanonicalPath);
if (watchedFiles[fileName]) { if (watchedDirectories.contains(parentDirPath)) {
const callback = watchedFiles[fileName]; let hasWatchedChildren = false;
callback(fileName); watchedFiles.forEachValue((key, _) => {
if (ts.getDirectoryPath(key) === parentDirPath) {
hasWatchedChildren = true;
}
});
if (!hasWatchedChildren) {
watchedDirectories.get(parentDirPath).close();
watchedDirectories.remove(parentDirPath);
}
} }
} }
return { function fileEventHandler(eventName: string, fileName: string, basePath: string) {
addFile: addFile, const path = ts.toPath(fileName, basePath, getCanonicalPath);
removeFile: removeFile if (watchedFiles.contains(path)) {
}; const callback = watchedFiles.get(path);
callback(fileName);
}
}
} }
// REVIEW: for now this implementation uses polling. // REVIEW: for now this implementation uses polling.
@ -352,7 +357,7 @@ namespace ts {
// to increase the chunk size or decrease the interval // to increase the chunk size or decrease the interval
// time dynamically to match the large reference set? // time dynamically to match the large reference set?
const pollingWatchedFileSet = createPollingWatchedFileSet(); const pollingWatchedFileSet = createPollingWatchedFileSet();
// const watchedFileSet = createWatchedFileSet(); const watchedFileSet = createWatchedFileSet();
function isNode4OrLater(): Boolean { function isNode4OrLater(): Boolean {
return parseInt(process.version.charAt(1)) >= 4; return parseInt(process.version.charAt(1)) >= 4;
@ -456,9 +461,10 @@ namespace ts {
// and is more efficient than `fs.watchFile` (ref: https://github.com/nodejs/node/pull/2649 // and is more efficient than `fs.watchFile` (ref: https://github.com/nodejs/node/pull/2649
// and https://github.com/Microsoft/TypeScript/issues/4643), therefore // and https://github.com/Microsoft/TypeScript/issues/4643), therefore
// if the current node.js version is newer than 4, use `fs.watch` instead. // if the current node.js version is newer than 4, use `fs.watch` instead.
const watchedFile = pollingWatchedFileSet.addFile(fileName, callback); const watchSet = isNode4OrLater() ? watchedFileSet : pollingWatchedFileSet;
const watchedFile = watchSet.addFile(fileName, callback);
return { return {
close: () => pollingWatchedFileSet.removeFile(watchedFile) close: () => watchSet.removeFile(watchedFile)
}; };
}, },
watchDirectory: (path, callback, recursive) => { watchDirectory: (path, callback, recursive) => {