| 60 | const CACHE_TTL_MS = 1000 * 60 * 60 * 24 * 30; // 30 days |
| 61 | |
| 62 | export class CachingWorkspace implements IWorkspace { |
| 63 | get initialized(): boolean { |
| 64 | return this.workspace.initialized; |
| 65 | } |
| 66 | |
| 67 | get workspaceRoot(): string { |
| 68 | return this.workspace.workspaceRoot; |
| 69 | } |
| 70 | |
| 71 | private fileManager: FileManager; |
| 72 | private fileInfoCache = new Map<string, CachedFileInfo>(); |
| 73 | private taskQueue = new SerialTaskQueue(); |
| 74 | |
| 75 | constructor( |
| 76 | private readonly workspace: IWorkspace, |
| 77 | private readonly cache: ICompilationCache, |
| 78 | private readonly logger: ILogger | undefined, |
| 79 | ) { |
| 80 | this.fileManager = new FileManager(workspace.workspaceRoot); |
| 81 | } |
| 82 | |
| 83 | initialize(): void { |
| 84 | this.workspace.initialize(); |
| 85 | this.cache.evictEntriesBeforeTime(Date.now() - CACHE_TTL_MS); |
| 86 | } |
| 87 | |
| 88 | destroy(): void { |
| 89 | this.workspace.destroy(); |
| 90 | this.cache.close(); |
| 91 | } |
| 92 | |
| 93 | private async measureAndLog<T>(command: string, path: string, work: () => Promise<T>): Promise<T> { |
| 94 | if (!this.logger) { |
| 95 | return await work(); |
| 96 | } |
| 97 | |
| 98 | const sw = new Stopwatch(); |
| 99 | this.logger?.debug?.(`Begin ${command} on '${path}'`); |
| 100 | const result = await work(); |
| 101 | |
| 102 | this.logger?.debug?.(`End ${command} on '${path}' in ${sw.elapsedString}`); |
| 103 | return result; |
| 104 | } |
| 105 | |
| 106 | private invalidateFileInfo(absolutePath: string): void { |
| 107 | const fileInfo = this.fileInfoCache.get(absolutePath); |
| 108 | if (fileInfo) { |
| 109 | this.fileInfoCache.delete(absolutePath); |
| 110 | |
| 111 | if (fileInfo.dependentFiles) { |
| 112 | for (const dependentFile of fileInfo.dependentFiles) { |
| 113 | this.invalidateFileInfo(dependentFile); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | registerInMemoryFile(fileName: string, fileContent: string): void { |
nothing calls this directly
no outgoing calls
no test coverage detected