diff options
Diffstat (limited to 'packages/vscode-ide-companion/src/recent-files-manager.ts')
| -rw-r--r-- | packages/vscode-ide-companion/src/recent-files-manager.ts | 29 |
1 files changed, 20 insertions, 9 deletions
diff --git a/packages/vscode-ide-companion/src/recent-files-manager.ts b/packages/vscode-ide-companion/src/recent-files-manager.ts index 84316363..cbe7e9a9 100644 --- a/packages/vscode-ide-companion/src/recent-files-manager.ts +++ b/packages/vscode-ide-companion/src/recent-files-manager.ts @@ -16,14 +16,14 @@ interface RecentFile { /** * Keeps track of the 10 most recently-opened files - * opened less than 5 ago. If a file is closed or deleted, - * it will be removed. If the length is maxxed out, - * the now-removed file will not be replaced by an older file. + * opened less than 5 min ago. If a file is closed or deleted, + * it will be removed. If the max length is reached, older files will get removed first. */ export class RecentFilesManager { private readonly files: RecentFile[] = []; private readonly onDidChangeEmitter = new vscode.EventEmitter<void>(); readonly onDidChange = this.onDidChangeEmitter.event; + private debounceTimer: NodeJS.Timeout | undefined; constructor(private readonly context: vscode.ExtensionContext) { const editorWatcher = vscode.window.onDidChangeActiveTextEditor( @@ -33,7 +33,7 @@ export class RecentFilesManager { } }, ); - const fileWatcher = vscode.workspace.onDidDeleteFiles((event) => { + const deleteWatcher = vscode.workspace.onDidDeleteFiles((event) => { for (const uri of event.files) { this.remove(uri); } @@ -49,12 +49,21 @@ export class RecentFilesManager { }); context.subscriptions.push( editorWatcher, - fileWatcher, + deleteWatcher, closeWatcher, renameWatcher, ); } + private fireWithDebounce() { + if (this.debounceTimer) { + clearTimeout(this.debounceTimer); + } + this.debounceTimer = setTimeout(() => { + this.onDidChangeEmitter.fire(); + }, 50); // 50ms + } + private remove(uri: vscode.Uri, fireEvent = true) { const index = this.files.findIndex( (file) => file.uri.fsPath === uri.fsPath, @@ -62,21 +71,23 @@ export class RecentFilesManager { if (index !== -1) { this.files.splice(index, 1); if (fireEvent) { - this.onDidChangeEmitter.fire(); + this.fireWithDebounce(); } } } add(uri: vscode.Uri) { - // Remove if it already exists to avoid duplicates and move it to the top. - this.remove(uri, false); + if (uri.scheme !== 'file') { + return; + } + this.remove(uri, false); this.files.unshift({ uri, timestamp: Date.now() }); if (this.files.length > MAX_FILES) { this.files.pop(); } - this.onDidChangeEmitter.fire(); + this.fireWithDebounce(); } get recentFiles(): Array<{ filePath: string; timestamp: number }> { |
