diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index 585fb60df32a6..13bbfc2ab153f 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -34,6 +34,8 @@ namespace ts { realpath?(path: string): string; /*@internal*/ getEnvironmentVariable(name: string): string; /*@internal*/ tryEnableSourceMapsForHost?(): void; + setTimeout?(callback: (...args: any[]) => void, ms: number, ...args: any[]): any; + clearTimeout?(timeoutId: any): void; } export interface FileWatcher { @@ -560,7 +562,9 @@ namespace ts { catch (e) { // Could not enable source maps. } - } + }, + setTimeout, + clearTimeout }; return nodeSystem; } diff --git a/src/compiler/tsc.ts b/src/compiler/tsc.ts index aa277c9b5070d..10d4a0c1d3679 100644 --- a/src/compiler/tsc.ts +++ b/src/compiler/tsc.ts @@ -250,8 +250,8 @@ namespace ts { let compilerOptions: CompilerOptions; // Compiler options for compilation let compilerHost: CompilerHost; // Compiler host let hostGetSourceFile: typeof compilerHost.getSourceFile; // getSourceFile method from default host - let timerHandleForRecompilation: number; // Handle for 0.25s wait timer to trigger recompilation - let timerHandleForDirectoryChanges: number; // Handle for 0.25s wait timer to trigger directory change handler + let timerHandleForRecompilation: any; // Handle for 0.25s wait timer to trigger recompilation + let timerHandleForDirectoryChanges: any; // Handle for 0.25s wait timer to trigger directory change handler // This map stores and reuses results of fileExists check that happen inside 'createProgram' // This allows to save time in module resolution heavy scenarios when existence of the same file might be checked multiple times. @@ -503,10 +503,14 @@ namespace ts { } function startTimerForHandlingDirectoryChanges() { + if (!sys.setTimeout || !sys.clearTimeout) { + return; + } + if (timerHandleForDirectoryChanges) { - clearTimeout(timerHandleForDirectoryChanges); + sys.clearTimeout(timerHandleForDirectoryChanges); } - timerHandleForDirectoryChanges = setTimeout(directoryChangeHandler, 250); + timerHandleForDirectoryChanges = sys.setTimeout(directoryChangeHandler, 250); } function directoryChangeHandler() { @@ -525,10 +529,14 @@ namespace ts { // operations (such as saving all modified files in an editor) a chance to complete before we kick // off a new compilation. function startTimerForRecompilation() { + if (!sys.setTimeout || !sys.clearTimeout) { + return; + } + if (timerHandleForRecompilation) { - clearTimeout(timerHandleForRecompilation); + sys.clearTimeout(timerHandleForRecompilation); } - timerHandleForRecompilation = setTimeout(recompile, 250); + timerHandleForRecompilation = sys.setTimeout(recompile, 250); } function recompile() {