@@ -86,6 +86,8 @@ class State {
86
86
}
87
87
let state = new State ( ) ;
88
88
89
+ const reloadMemoryThresholdMb = getEnvironmentVariable ( "SEMMLE_TYPESCRIPT_MEMORY_THRESHOLD" , Number , 1000 ) ;
90
+
89
91
/**
90
92
* Debugging method for finding cycles in the TypeScript AST. Should not be used in production.
91
93
*
@@ -529,26 +531,36 @@ function getEnvironmentVariable<T>(name: string, parse: (x: string) => T, defaul
529
531
return value != null ? parse ( value ) : defaultValue ;
530
532
}
531
533
534
+ /**
535
+ * Whether the memory usage was last observed to be above the threshold for restarting the TypeScript compiler.
536
+ */
537
+ let isAboveReloadThreshold = false ;
538
+
539
+ /**
540
+ * If memory usage has moved above a the threshold, reboot the TypeScript compiler instance.
541
+ */
542
+ function checkMemoryUsage ( ) {
543
+ let bytesUsed = process . memoryUsage ( ) . heapUsed ;
544
+ let megabytesUsed = bytesUsed / 1000000 ;
545
+ if ( ! isAboveReloadThreshold && megabytesUsed > reloadMemoryThresholdMb && state . project != null ) {
546
+ console . warn ( 'Restarting TypeScript compiler due to memory usage' ) ;
547
+ state . project . reload ( ) ;
548
+ isAboveReloadThreshold = true ;
549
+ }
550
+ else if ( isAboveReloadThreshold && megabytesUsed < reloadMemoryThresholdMb ) {
551
+ isAboveReloadThreshold = false ;
552
+ }
553
+ }
554
+
532
555
function runReadLineInterface ( ) {
533
556
reset ( ) ;
534
- let reloadMemoryThresholdMb = getEnvironmentVariable ( "SEMMLE_TYPESCRIPT_MEMORY_THRESHOLD" , Number , 1000 ) ;
535
- let isAboveReloadThreshold = false ;
536
557
let rl = readline . createInterface ( { input : process . stdin , output : process . stdout } ) ;
537
558
rl . on ( "line" , ( line : string ) => {
538
559
let req : Command = JSON . parse ( line ) ;
539
560
switch ( req . command ) {
540
561
case "parse" :
541
562
handleParseCommand ( req ) ;
542
- // If memory usage has moved above the threshold, reboot the TypeScript compiler instance.
543
- let bytesUsed = process . memoryUsage ( ) . heapUsed ;
544
- let megabytesUsed = bytesUsed / 1000000 ;
545
- if ( ! isAboveReloadThreshold && megabytesUsed > reloadMemoryThresholdMb && state . project != null ) {
546
- console . warn ( 'Restarting TypeScript compiler due to memory usage' ) ;
547
- state . project . reload ( ) ;
548
- isAboveReloadThreshold = true ;
549
- } else if ( isAboveReloadThreshold && megabytesUsed < reloadMemoryThresholdMb ) {
550
- isAboveReloadThreshold = false ;
551
- }
563
+ checkMemoryUsage ( ) ;
552
564
break ;
553
565
case "open-project" :
554
566
handleOpenProjectCommand ( req ) ;
0 commit comments