|
3 | 3 | namespace ts {
|
4 | 4 | export type FileWatcherCallback = (fileName: string, removed?: boolean) => void;
|
5 | 5 | export type DirectoryWatcherCallback = (directoryName: string) => void;
|
| 6 | + export interface WatchedFile { |
| 7 | + fileName: string; |
| 8 | + callback: FileWatcherCallback; |
| 9 | + mtime?: Date; |
| 10 | + } |
6 | 11 |
|
7 | 12 | export interface System {
|
8 | 13 | args: string[];
|
@@ -224,6 +229,91 @@ namespace ts {
|
224 | 229 | const _os = require("os");
|
225 | 230 | const _crypto = require("crypto");
|
226 | 231 |
|
| 232 | + const useNonPollingWatchers = process.env["TSC_NONPOLLING_WATCHER"]; |
| 233 | + |
| 234 | + function createWatchedFileSet() { |
| 235 | + const dirWatchers: Map<DirectoryWatcher> = {}; |
| 236 | + // One file can have multiple watchers |
| 237 | + const fileWatcherCallbacks: Map<FileWatcherCallback[]> = {}; |
| 238 | + return { addFile, removeFile }; |
| 239 | + |
| 240 | + function reduceDirWatcherRefCountForFile(fileName: string) { |
| 241 | + const dirName = getDirectoryPath(fileName); |
| 242 | + if (hasProperty(dirWatchers, dirName)) { |
| 243 | + const watcher = dirWatchers[dirName]; |
| 244 | + watcher.referenceCount -= 1; |
| 245 | + if (watcher.referenceCount <= 0) { |
| 246 | + watcher.close(); |
| 247 | + delete dirWatchers[dirName]; |
| 248 | + } |
| 249 | + } |
| 250 | + } |
| 251 | + |
| 252 | + function addDirWatcher(dirPath: string): void { |
| 253 | + if (hasProperty(dirWatchers, dirPath)) { |
| 254 | + const watcher = dirWatchers[dirPath]; |
| 255 | + watcher.referenceCount += 1; |
| 256 | + return; |
| 257 | + } |
| 258 | + |
| 259 | + const watcher: DirectoryWatcher = _fs.watch( |
| 260 | + dirPath, |
| 261 | + { persistent: true }, |
| 262 | + (eventName: string, relativeFileName: string) => fileEventHandler(eventName, relativeFileName, dirPath) |
| 263 | + ); |
| 264 | + watcher.referenceCount = 1; |
| 265 | + dirWatchers[dirPath] = watcher; |
| 266 | + return; |
| 267 | + } |
| 268 | + |
| 269 | + function addFileWatcherCallback(filePath: string, callback: FileWatcherCallback): void { |
| 270 | + if (hasProperty(fileWatcherCallbacks, filePath)) { |
| 271 | + fileWatcherCallbacks[filePath].push(callback); |
| 272 | + } |
| 273 | + else { |
| 274 | + fileWatcherCallbacks[filePath] = [callback]; |
| 275 | + } |
| 276 | + } |
| 277 | + |
| 278 | + function addFile(fileName: string, callback: FileWatcherCallback): WatchedFile { |
| 279 | + addFileWatcherCallback(fileName, callback); |
| 280 | + addDirWatcher(getDirectoryPath(fileName)); |
| 281 | + |
| 282 | + return { fileName, callback }; |
| 283 | + } |
| 284 | + |
| 285 | + function removeFile(watchedFile: WatchedFile) { |
| 286 | + removeFileWatcherCallback(watchedFile.fileName, watchedFile.callback); |
| 287 | + reduceDirWatcherRefCountForFile(watchedFile.fileName); |
| 288 | + } |
| 289 | + |
| 290 | + function removeFileWatcherCallback(filePath: string, callback: FileWatcherCallback) { |
| 291 | + if (hasProperty(fileWatcherCallbacks, filePath)) { |
| 292 | + const newCallbacks = copyListRemovingItem(callback, fileWatcherCallbacks[filePath]); |
| 293 | + if (newCallbacks.length === 0) { |
| 294 | + delete fileWatcherCallbacks[filePath]; |
| 295 | + } |
| 296 | + else { |
| 297 | + fileWatcherCallbacks[filePath] = newCallbacks; |
| 298 | + } |
| 299 | + } |
| 300 | + } |
| 301 | + |
| 302 | + function fileEventHandler(eventName: string, relativeFileName: string, baseDirPath: string) { |
| 303 | + // When files are deleted from disk, the triggered "rename" event would have a relativefileName of "undefined" |
| 304 | + const fileName = typeof relativeFileName !== "string" |
| 305 | + ? undefined |
| 306 | + : ts.getNormalizedAbsolutePath(relativeFileName, baseDirPath); |
| 307 | + // Some applications save a working file via rename operations |
| 308 | + if ((eventName === "change" || eventName === "rename") && hasProperty(fileWatcherCallbacks, fileName)) { |
| 309 | + for (const fileCallback of fileWatcherCallbacks[fileName]) { |
| 310 | + fileCallback(fileName); |
| 311 | + } |
| 312 | + } |
| 313 | + } |
| 314 | + } |
| 315 | + const watchedFileSet = createWatchedFileSet(); |
| 316 | + |
227 | 317 | function isNode4OrLater(): boolean {
|
228 | 318 | return parseInt(process.version.charAt(1)) >= 4;
|
229 | 319 | }
|
@@ -319,7 +409,7 @@ namespace ts {
|
319 | 409 | const files = _fs.readdirSync(path || ".").sort();
|
320 | 410 | const directories: string[] = [];
|
321 | 411 | for (const current of files) {
|
322 |
| - // This is necessary because on some file system node fails to exclude |
| 412 | + // This is necessary because on some file system node fails to exclude |
323 | 413 | // "." and "..". See https://github.com/nodejs/node/issues/4002
|
324 | 414 | if (current === "." || current === "..") {
|
325 | 415 | continue;
|
@@ -353,7 +443,26 @@ namespace ts {
|
353 | 443 | readFile,
|
354 | 444 | writeFile,
|
355 | 445 | watchFile: (fileName, callback) => {
|
356 |
| - return _fs.watchFile(fileName, callback); |
| 446 | + if (useNonPollingWatchers) { |
| 447 | + const watchedFile = watchedFileSet.addFile(fileName, callback); |
| 448 | + return { |
| 449 | + close: () => watchedFileSet.removeFile(watchedFile) |
| 450 | + }; |
| 451 | + } |
| 452 | + else { |
| 453 | + _fs.watchFile(fileName, { persistent: true, interval: 250 }, fileChanged); |
| 454 | + return { |
| 455 | + close: () => _fs.unwatchFile(fileName, fileChanged) |
| 456 | + }; |
| 457 | + } |
| 458 | + |
| 459 | + function fileChanged(curr: any, prev: any) { |
| 460 | + if (+curr.mtime <= +prev.mtime) { |
| 461 | + return; |
| 462 | + } |
| 463 | + |
| 464 | + callback(fileName); |
| 465 | + } |
357 | 466 | },
|
358 | 467 | watchDirectory: (directoryName, callback, recursive) => {
|
359 | 468 | // Node 4.0 `fs.watch` function supports the "recursive" option on both OSX and Windows
|
|
0 commit comments