From e6e9f8bc9c18903ed448b9fc11e0e367f1b83edc Mon Sep 17 00:00:00 2001 From: Joyce Er Date: Fri, 7 Apr 2023 16:35:49 +0000 Subject: [PATCH] Avoid side effects when parsing edit session payload --- .../browser/editSessions.contribution.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/vs/workbench/contrib/editSessions/browser/editSessions.contribution.ts b/src/vs/workbench/contrib/editSessions/browser/editSessions.contribution.ts index 76052477cf3c9..3fc2fad4fdfa8 100644 --- a/src/vs/workbench/contrib/editSessions/browser/editSessions.contribution.ts +++ b/src/vs/workbench/contrib/editSessions/browser/editSessions.contribution.ts @@ -489,8 +489,8 @@ export class EditSessionsContribution extends Disposable implements IWorkbenchCo } try { - const { changes, conflictingChanges } = await this.generateChanges(editSession, ref, force); - if (changes.length === 0) { + const { changes, conflictingChanges, contributedStateHandlers } = await this.generateChanges(editSession, ref, force); + if (changes.length === 0 && contributedStateHandlers.length === 0) { return; } @@ -519,6 +519,10 @@ export class EditSessionsContribution extends Disposable implements IWorkbenchCo } } + for (const handleContributedState of contributedStateHandlers) { + handleContributedState(); + } + this.logService.info(`Deleting edit session with ref ${ref} after successfully applying it to current workspace...`); await this.editSessionsStorageService.delete(ref); this.logService.info(`Deleted edit session with ref ${ref}.`); @@ -534,6 +538,7 @@ export class EditSessionsContribution extends Disposable implements IWorkbenchCo private async generateChanges(editSession: EditSession, ref: string, force = false) { const changes: ({ uri: URI; type: ChangeType; contents: string | undefined })[] = []; + const contributedStateHandlers: (() => void)[] = []; const conflictingChanges = []; const workspaceFolders = this.contextService.getWorkspace().folders; @@ -580,7 +585,7 @@ export class EditSessionsContribution extends Disposable implements IWorkbenchCo if (!folderRoot) { this.logService.info(`Skipping applying ${folder.workingChanges.length} changes from edit session with ref ${ref} as no matching workspace folder was found.`); - return { changes: [], conflictingChanges: [] }; + return { changes: [], conflictingChanges: [], contributedStateHandlers: [] }; } const localChanges = new Set(); @@ -604,18 +609,17 @@ export class EditSessionsContribution extends Disposable implements IWorkbenchCo const workspaceFolder = folderRoot; if (workspaceFolder) { - // apply additional state from registered edit session contributors // look through all registered contributions to gather additional state EditSessionRegistry.getEditSessionContributions().forEach(([key, contrib]) => { const state = folder[key]; if (state) { - contrib.resumeState(workspaceFolder, state); + contributedStateHandlers.push(() => contrib.resumeState(workspaceFolder, state)); } }); } } - return { changes, conflictingChanges }; + return { changes, conflictingChanges, contributedStateHandlers }; } private async willChangeLocalContents(localChanges: Set, uriWithIncomingChanges: URI, incomingChange: Change) {