From ee854778b3947c16007dcb9fbfe96933ba7f761c Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Tue, 14 Nov 2017 15:37:40 +0100 Subject: [PATCH] `code --add` breaks if executed repeatedly (fixes #38138) --- src/vs/workbench/electron-browser/window.ts | 31 +++++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/vs/workbench/electron-browser/window.ts b/src/vs/workbench/electron-browser/window.ts index c3fd3434b1f82..8e52aae722be9 100644 --- a/src/vs/workbench/electron-browser/window.ts +++ b/src/vs/workbench/electron-browser/window.ts @@ -45,6 +45,7 @@ import { RunOnceScheduler } from 'vs/base/common/async'; import { IDisposable, dispose } from 'vs/base/common/lifecycle'; import { ConfigurationTarget, IConfigurationChangeEvent } from 'vs/platform/configuration/common/configuration'; import { LifecyclePhase, ILifecycleService } from 'vs/platform/lifecycle/common/lifecycle'; +import { IWorkspaceFolderCreationData } from 'vs/platform/workspaces/common/workspaces'; const TextInputActions: IAction[] = [ new Action('undo', nls.localize('undo', "Undo"), null, true, () => document.execCommand('undo') && TPromise.as(true)), @@ -68,6 +69,9 @@ export class ElectronWindow extends Themable { private previousConfiguredZoomLevel: number; + private addFoldersScheduler: RunOnceScheduler; + private pendingFoldersToAdd: IAddFoldersRequest[]; + constructor( shellContainer: HTMLElement, @IWorkbenchEditorService private editorService: IWorkbenchEditorService, @@ -96,6 +100,10 @@ export class ElectronWindow extends Themable { this.touchBarUpdater = new RunOnceScheduler(() => this.doSetupTouchbar(), 300); this.toUnbind.push(this.touchBarUpdater); + this.pendingFoldersToAdd = []; + this.addFoldersScheduler = new RunOnceScheduler(() => this.doAddFolders(), 100); + this.toUnbind.push(this.addFoldersScheduler); + this.registerListeners(); this.create(); } @@ -180,7 +188,7 @@ export class ElectronWindow extends Themable { ipc.on('vscode:openFiles', (_event: any, request: IOpenFileRequest) => this.onOpenFiles(request)); // Support addFolders event if we have a workspace opened - ipc.on('vscode:addFolders', (_event: any, request: IAddFoldersRequest) => this.onAddFolders(request)); + ipc.on('vscode:addFolders', (_event: any, request: IAddFoldersRequest) => this.onAddFoldersRequest(request)); // Message support ipc.on('vscode:showInfoMessage', (_event: any, message: string) => { @@ -394,8 +402,25 @@ export class ElectronWindow extends Themable { }); } - private onAddFolders(request: IAddFoldersRequest): void { - const foldersToAdd = request.foldersToAdd.map(folderToAdd => ({ uri: URI.file(folderToAdd.filePath) })); + private onAddFoldersRequest(request: IAddFoldersRequest): void { + + // Buffer all pending requests + this.pendingFoldersToAdd.push(request); + + // Delay the adding of folders a bit to buffer in case more requests are coming + if (!this.addFoldersScheduler.isScheduled()) { + this.addFoldersScheduler.schedule(); + } + } + + private doAddFolders(): void { + const foldersToAdd: IWorkspaceFolderCreationData[] = []; + + this.pendingFoldersToAdd.forEach(request => { + foldersToAdd.push(...request.foldersToAdd.map(folderToAdd => ({ uri: URI.file(folderToAdd.filePath) }))); + }); + + this.pendingFoldersToAdd = []; this.workspaceEditingService.addFolders(foldersToAdd).done(null, errors.onUnexpectedError); }