Skip to content
Permalink
Browse files

Avoid incrementing memory cache unnecessarily

  • Loading branch information...
blakeembrey committed Jan 27, 2019
1 parent b4bdb08 commit 2609663bf3a5b14d76c0dfe989ceba67e68d5efa
Showing with 8 additions and 3 deletions.
  1. +8 −3 src/index.ts
@@ -79,7 +79,9 @@ class MemoryCache {
fileContents = new Map<string, string>()
fileVersions = new Map<string, number>()

constructor (public rootFiles: string[] = []) {}
constructor (public rootFileNames: string[] = []) {
for (const fileName of rootFileNames) this.fileVersions.set(fileName, 1)
}
}

/**
@@ -269,7 +271,7 @@ export function register (opts: Options = {}): Register {

// Create the compiler host for type checking.
const serviceHost: _ts.LanguageServiceHost = {
getScriptFileNames: () => memoryCache.rootFiles,
getScriptFileNames: () => memoryCache.rootFileNames,
getScriptVersion: (fileName: string) => {
const version = memoryCache.fileVersions.get(fileName)
return version === undefined ? '' : version.toString()
@@ -310,7 +312,10 @@ export function register (opts: Options = {}): Register {
const fileVersion = memoryCache.fileVersions.get(fileName) || 0

// Add to `rootFiles` when discovered for the first time.
if (fileVersion === 0) memoryCache.rootFiles.push(fileName)
if (fileVersion === 0) memoryCache.rootFileNames.push(fileName)

// Avoid incrementing cache when nothing has changed.
if (memoryCache.fileContents.get(fileName) === contents) return

memoryCache.fileVersions.set(fileName, fileVersion + 1)
memoryCache.fileContents.set(fileName, contents)

0 comments on commit 2609663

Please sign in to comment.
You can’t perform that action at this time.