Skip to content

Commit 88bf57b

Browse files
Sandeep SomavarapuSandeep Somavarapu
authored andcommitted
Implement start and stop sync commands
1 parent fcffaba commit 88bf57b

4 files changed

Lines changed: 56 additions & 6 deletions

File tree

src/vs/workbench/contrib/userData/browser/userData.contribution.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import { IWorkbenchContributionsRegistry, Extensions as WorkbenchExtensions, IWorkbenchContribution } from 'vs/workbench/common/contributions';
77
import { IUserDataSyncService, SyncStatus } from 'vs/workbench/services/userData/common/userData';
88
import { localize } from 'vs/nls';
9-
import { Disposable, MutableDisposable } from 'vs/base/common/lifecycle';
9+
import { Disposable, MutableDisposable, toDisposable } from 'vs/base/common/lifecycle';
1010
import { CommandsRegistry } from 'vs/platform/commands/common/commands';
1111
import { Registry } from 'vs/platform/registry/common/platform';
1212
import { LifecyclePhase } from 'vs/platform/lifecycle/common/lifecycle';
@@ -20,6 +20,7 @@ import { GLOBAL_ACTIVITY_ID } from 'vs/workbench/common/activity';
2020
import { timeout } from 'vs/base/common/async';
2121
import { registerEditorContribution } from 'vs/editor/browser/editorExtensions';
2222
import { AcceptChangesController } from 'vs/workbench/contrib/userData/browser/userDataPreviewEditorContribution';
23+
import { INotificationService, Severity } from 'vs/platform/notification/common/notification';
2324

2425
const CONTEXT_SYNC_STATE = new RawContextKey<string>('syncStatus', SyncStatus.Uninitialized);
2526

@@ -74,11 +75,13 @@ class SyncContribution extends Disposable implements IWorkbenchContribution {
7475

7576
private readonly syncEnablementContext: IContextKey<string>;
7677
private readonly badgeDisposable = this._register(new MutableDisposable());
78+
private readonly conflictsWarningDisposable = this._register(new MutableDisposable());
7779

7880
constructor(
79-
@IUserDataSyncService userDataSyncService: IUserDataSyncService,
81+
@IUserDataSyncService private readonly userDataSyncService: IUserDataSyncService,
8082
@IContextKeyService contextKeyService: IContextKeyService,
81-
@IActivityService private readonly activityService: IActivityService
83+
@IActivityService private readonly activityService: IActivityService,
84+
@INotificationService private readonly notificationService: INotificationService
8285
) {
8386
super();
8487
this.syncEnablementContext = CONTEXT_SYNC_STATE.bindTo(contextKeyService);
@@ -105,6 +108,24 @@ class SyncContribution extends Disposable implements IWorkbenchContribution {
105108
if (badge) {
106109
this.badgeDisposable.value = this.activityService.showActivity(GLOBAL_ACTIVITY_ID, badge, clazz);
107110
}
111+
112+
if (status !== SyncStatus.HasConflicts) {
113+
this.conflictsWarningDisposable.clear();
114+
}
115+
}
116+
117+
private async sync(): Promise<void> {
118+
await this.userDataSyncService.sync();
119+
if (this.userDataSyncService.status === SyncStatus.HasConflicts) {
120+
const handle = this.notificationService.prompt(Severity.Warning, localize('conflicts detected', "Unable to sync due to conflicts. Please resolve them to continue."),
121+
[
122+
{
123+
label: localize('resolve', "Resolve Conflicts"),
124+
run: () => this.userDataSyncService.handleConflicts()
125+
}
126+
]);
127+
this.conflictsWarningDisposable.value = toDisposable(() => handle.close());
128+
}
108129
}
109130

110131
private registerActions(): void {
@@ -154,7 +175,7 @@ class SyncContribution extends Disposable implements IWorkbenchContribution {
154175

155176
// Command Pallette Actions
156177

157-
CommandsRegistry.registerCommand('workbench.userData.actions.startSync', serviceAccessor => serviceAccessor.get(IUserDataSyncService).sync());
178+
CommandsRegistry.registerCommand('workbench.userData.actions.startSync', () => this.sync());
158179
MenuRegistry.appendMenuItem(MenuId.CommandPalette, {
159180
command: {
160181
id: 'workbench.userData.actions.startSync',
@@ -163,6 +184,15 @@ class SyncContribution extends Disposable implements IWorkbenchContribution {
163184
when: ContextKeyExpr.and(CONTEXT_SYNC_STATE.isEqualTo(SyncStatus.Idle), ContextKeyExpr.not('config.userConfiguration.autoSync')),
164185
});
165186

187+
CommandsRegistry.registerCommand('workbench.userData.actions.stopSync', serviceAccessor => serviceAccessor.get(IUserDataSyncService).stopSync());
188+
MenuRegistry.appendMenuItem(MenuId.CommandPalette, {
189+
command: {
190+
id: 'workbench.userData.actions.stopSync',
191+
title: localize('stop sync', "Sync: Stop")
192+
},
193+
when: ContextKeyExpr.and(CONTEXT_SYNC_STATE.isEqualTo(SyncStatus.HasConflicts), ContextKeyExpr.not('config.userConfiguration.autoSync')),
194+
});
195+
166196
MenuRegistry.appendMenuItem(MenuId.CommandPalette, {
167197
command: {
168198
id: 'sync.resolveConflicts',

src/vs/workbench/services/userData/common/settingsSync.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { ILogService } from 'vs/platform/log/common/log';
2626
import { Position } from 'vs/editor/common/core/position';
2727
import { IHistoryService } from 'vs/workbench/services/history/common/history';
2828
import { isEqual } from 'vs/base/common/resources';
29+
import { CancelablePromise, createCancelablePromise } from 'vs/base/common/async';
2930

3031
interface ISyncPreviewResult {
3132
readonly fileContent: IFileContent | null;
@@ -40,7 +41,7 @@ export class SettingsSynchroniser extends Disposable implements ISynchroniser {
4041
private static LAST_SYNC_SETTINGS_STORAGE_KEY: string = 'LAST_SYNC_SETTINGS_CONTENTS';
4142
private static EXTERNAL_USER_DATA_SETTINGS_KEY: string = 'settings';
4243

43-
private syncPreviewResultPromise: Promise<ISyncPreviewResult> | null = null;
44+
private syncPreviewResultPromise: CancelablePromise<ISyncPreviewResult> | null = null;
4445

4546
private _status: SyncStatus = SyncStatus.Idle;
4647
get status(): SyncStatus { return this._status; }
@@ -101,6 +102,15 @@ export class SettingsSynchroniser extends Disposable implements ISynchroniser {
101102
}
102103
}
103104

105+
async stopSync(): Promise<void> {
106+
await this.fileService.del(SETTINGS_PREVIEW_RESOURCE);
107+
if (this.syncPreviewResultPromise) {
108+
this.syncPreviewResultPromise.cancel();
109+
this.syncPreviewResultPromise = null;
110+
this.setStatus(SyncStatus.Idle);
111+
}
112+
}
113+
104114
handleConflicts(): boolean {
105115
if (this.status !== SyncStatus.HasConflicts) {
106116
return false;
@@ -152,7 +162,7 @@ export class SettingsSynchroniser extends Disposable implements ISynchroniser {
152162

153163
private getPreview(): Promise<ISyncPreviewResult> {
154164
if (!this.syncPreviewResultPromise) {
155-
this.syncPreviewResultPromise = this.generatePreview();
165+
this.syncPreviewResultPromise = createCancelablePromise(token => this.generatePreview());
156166
}
157167
return this.syncPreviewResultPromise;
158168
}

src/vs/workbench/services/userData/common/userData.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ export interface ISynchroniser {
101101
readonly status: SyncStatus;
102102
readonly onDidChangeStatus: Event<SyncStatus>;
103103
sync(): Promise<boolean>;
104+
stopSync(): Promise<void>;
104105
handleConflicts(): boolean;
105106
apply(previewResource: URI): Promise<boolean>;
106107
}

src/vs/workbench/services/userData/common/userDataSyncService.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ export class UserDataSyncService extends Disposable implements IUserDataSyncServ
5050
return true;
5151
}
5252

53+
async stopSync(): Promise<void> {
54+
if (!this.remoteUserDataService.isEnabled()) {
55+
throw new Error('Not enabled');
56+
}
57+
for (const synchroniser of this.synchronisers) {
58+
await synchroniser.stopSync();
59+
}
60+
}
61+
5362
async apply(previewResource: URI): Promise<boolean> {
5463
if (!this.remoteUserDataService.isEnabled()) {
5564
throw new Error('Not enabled');

0 commit comments

Comments
 (0)