Skip to content

Commit 09f3600

Browse files
committed
refactor - generate preview for replace
1 parent 5cfe140 commit 09f3600

7 files changed

Lines changed: 93 additions & 92 deletions

File tree

src/vs/platform/userDataSync/common/abstractSynchronizer.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,8 @@ export abstract class AbstractSynchroniser extends Disposable {
259259
this.setStatus(SyncStatus.Syncing);
260260
const lastSyncUserData = await this.getLastSyncUserData();
261261
const remoteUserData = await this.getLatestRemoteUserData(null, lastSyncUserData);
262-
await this.performReplace(syncData, remoteUserData, lastSyncUserData);
262+
const preview = await this.generateReplacePreview(syncData, remoteUserData, lastSyncUserData);
263+
await this.applyPreview(preview, false);
263264
this.logService.info(`${this.syncResourceLogLabel}: Finished resetting ${this.resource.toLowerCase()}.`);
264265
} finally {
265266
this.setStatus(SyncStatus.Idle);
@@ -527,9 +528,9 @@ export abstract class AbstractSynchroniser extends Disposable {
527528
}
528529

529530
protected abstract readonly version: number;
530-
protected abstract performReplace(syncData: ISyncData, remoteUserData: IRemoteUserData, lastSyncUserData: IRemoteUserData | null): Promise<void>;
531531
protected abstract generatePullPreview(remoteUserData: IRemoteUserData, lastSyncUserData: IRemoteUserData | null, token: CancellationToken): Promise<ISyncPreview>;
532532
protected abstract generatePushPreview(remoteUserData: IRemoteUserData, lastSyncUserData: IRemoteUserData | null, token: CancellationToken): Promise<ISyncPreview>;
533+
protected abstract generateReplacePreview(syncData: ISyncData, remoteUserData: IRemoteUserData, lastSyncUserData: IRemoteUserData | null): Promise<ISyncPreview>;
533534
protected abstract generatePreview(remoteUserData: IRemoteUserData, lastSyncUserData: IRemoteUserData | null, token: CancellationToken): Promise<ISyncPreview>;
534535
protected abstract updatePreviewWithConflict(preview: ISyncPreview, conflictResource: URI, content: string, token: CancellationToken): Promise<ISyncPreview>;
535536
protected abstract applyPreview(preview: ISyncPreview, forcePush: boolean): Promise<void>;

src/vs/platform/userDataSync/common/extensionsSync.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,22 @@ export class ExtensionsSynchroniser extends AbstractSynchroniser implements IUse
114114
};
115115
}
116116

117+
protected async generateReplacePreview(syncData: ISyncData, remoteUserData: IRemoteUserData, lastSyncUserData: ILastSyncUserData | null): Promise<IExtensionsSyncPreview> {
118+
const installedExtensions = await this.extensionManagementService.getInstalled();
119+
const localExtensions = this.getLocalExtensions(installedExtensions);
120+
const syncExtensions = await this.parseAndMigrateExtensions(syncData);
121+
const ignoredExtensions = getIgnoredExtensions(installedExtensions, this.configurationService);
122+
const { added, updated, removed } = merge(localExtensions, syncExtensions, localExtensions, [], ignoredExtensions);
123+
124+
return {
125+
added, removed, updated, remote: syncExtensions, remoteUserData, localExtensions, skippedExtensions: [], lastSyncUserData,
126+
hasLocalChanged: added.length > 0 || removed.length > 0 || updated.length > 0,
127+
hasRemoteChanged: true,
128+
isLastSyncFromCurrentMachine: false,
129+
hasConflicts: false,
130+
};
131+
}
132+
117133
protected async generatePreview(remoteUserData: IRemoteUserData, lastSyncUserData: ILastSyncUserData | null): Promise<IExtensionsSyncPreview> {
118134
const remoteExtensions: ISyncExtension[] | null = remoteUserData.syncData ? await this.parseAndMigrateExtensions(remoteUserData.syncData) : null;
119135
const skippedExtensions: ISyncExtension[] = lastSyncUserData ? lastSyncUserData.skippedExtensions || [] : [];
@@ -159,22 +175,6 @@ export class ExtensionsSynchroniser extends AbstractSynchroniser implements IUse
159175
throw new Error(`${this.syncResourceLogLabel}: Conflicts should not occur`);
160176
}
161177

162-
protected async performReplace(syncData: ISyncData, remoteUserData: IRemoteUserData, lastSyncUserData: ILastSyncUserData | null): Promise<void> {
163-
const installedExtensions = await this.extensionManagementService.getInstalled();
164-
const localExtensions = this.getLocalExtensions(installedExtensions);
165-
const syncExtensions = await this.parseAndMigrateExtensions(syncData);
166-
const ignoredExtensions = getIgnoredExtensions(installedExtensions, this.configurationService);
167-
const { added, updated, removed } = merge(localExtensions, syncExtensions, localExtensions, [], ignoredExtensions);
168-
169-
await this.applyPreview({
170-
added, removed, updated, remote: syncExtensions, remoteUserData, localExtensions, skippedExtensions: [], lastSyncUserData,
171-
hasLocalChanged: added.length > 0 || removed.length > 0 || updated.length > 0,
172-
hasRemoteChanged: true,
173-
isLastSyncFromCurrentMachine: false,
174-
hasConflicts: false,
175-
}, false);
176-
}
177-
178178
protected async applyPreview({ added, removed, updated, remote, remoteUserData, skippedExtensions, lastSyncUserData, localExtensions, hasLocalChanged, hasRemoteChanged }: IExtensionsSyncPreview, forcePush: boolean): Promise<void> {
179179

180180
if (!hasLocalChanged && !hasRemoteChanged) {

src/vs/platform/userDataSync/common/globalStateSync.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,20 @@ export class GlobalStateSynchroniser extends AbstractSynchroniser implements IUs
111111
};
112112
}
113113

114+
protected async generateReplacePreview(syncData: ISyncData, remoteUserData: IRemoteUserData, lastSyncUserData: ILastSyncUserData | null): Promise<IGlobalStateSyncPreview> {
115+
const localUserData = await this.getLocalGlobalState();
116+
const syncGlobalState: IGlobalState = JSON.parse(syncData.content);
117+
const { local, skipped } = merge(localUserData.storage, syncGlobalState.storage, localUserData.storage, this.getSyncStorageKeys(), lastSyncUserData?.skippedStorageKeys || [], this.logService);
118+
return {
119+
local, remote: syncGlobalState.storage, remoteUserData, localUserData, lastSyncUserData,
120+
skippedStorageKeys: skipped,
121+
hasLocalChanged: Object.keys(local.added).length > 0 || Object.keys(local.updated).length > 0 || local.removed.length > 0,
122+
hasRemoteChanged: true,
123+
isLastSyncFromCurrentMachine: false,
124+
hasConflicts: false
125+
};
126+
}
127+
114128
protected async generatePreview(remoteUserData: IRemoteUserData, lastSyncUserData: ILastSyncUserData | null, token: CancellationToken): Promise<IGlobalStateSyncPreview> {
115129
const remoteGlobalState: IGlobalState = remoteUserData.syncData ? JSON.parse(remoteUserData.syncData.content) : null;
116130
const isLastSyncFromCurrentMachine = await this.isLastSyncFromCurrentMachine(remoteUserData);
@@ -147,20 +161,6 @@ export class GlobalStateSynchroniser extends AbstractSynchroniser implements IUs
147161
throw new Error(`${this.syncResourceLogLabel}: Conflicts should not occur`);
148162
}
149163

150-
protected async performReplace(syncData: ISyncData, remoteUserData: IRemoteUserData, lastSyncUserData: ILastSyncUserData | null): Promise<void> {
151-
const localUserData = await this.getLocalGlobalState();
152-
const syncGlobalState: IGlobalState = JSON.parse(syncData.content);
153-
const { local, skipped } = merge(localUserData.storage, syncGlobalState.storage, localUserData.storage, this.getSyncStorageKeys(), lastSyncUserData?.skippedStorageKeys || [], this.logService);
154-
await this.applyPreview({
155-
local, remote: syncGlobalState.storage, remoteUserData, localUserData, lastSyncUserData,
156-
skippedStorageKeys: skipped,
157-
hasLocalChanged: Object.keys(local.added).length > 0 || Object.keys(local.updated).length > 0 || local.removed.length > 0,
158-
hasRemoteChanged: true,
159-
isLastSyncFromCurrentMachine: false,
160-
hasConflicts: false
161-
}, false);
162-
}
163-
164164
protected async applyPreview({ local, remote, remoteUserData, lastSyncUserData, localUserData, hasLocalChanged, hasRemoteChanged, skippedStorageKeys }: IGlobalStateSyncPreview, forcePush: boolean): Promise<void> {
165165

166166
if (!hasLocalChanged && !hasRemoteChanged) {

src/vs/platform/userDataSync/common/keybindingsSync.ts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,21 @@ export class KeybindingsSynchroniser extends AbstractJsonFileSynchroniser implem
8484
};
8585
}
8686

87+
protected async generateReplacePreview(syncData: ISyncData, remoteUserData: IRemoteUserData, lastSyncUserData: IRemoteUserData | null): Promise<IFileSyncPreview> {
88+
const fileContent = await this.getLocalFileContent();
89+
const content = this.getKeybindingsContentFromSyncContent(syncData.content);
90+
return {
91+
fileContent,
92+
remoteUserData,
93+
lastSyncUserData,
94+
content,
95+
hasConflicts: false,
96+
hasLocalChanged: content !== null,
97+
hasRemoteChanged: content !== null,
98+
isLastSyncFromCurrentMachine: false
99+
};
100+
}
101+
87102
protected async generatePreview(remoteUserData: IRemoteUserData, lastSyncUserData: IRemoteUserData | null, token: CancellationToken = CancellationToken.None): Promise<IFileSyncPreview> {
88103
const remoteContent = remoteUserData.syncData ? this.getKeybindingsContentFromSyncContent(remoteUserData.syncData.content) : null;
89104
const isLastSyncFromCurrentMachine = await this.isLastSyncFromCurrentMachine(remoteUserData);
@@ -150,24 +165,6 @@ export class KeybindingsSynchroniser extends AbstractJsonFileSynchroniser implem
150165
return preview;
151166
}
152167

153-
protected async performReplace(syncData: ISyncData, remoteUserData: IRemoteUserData, lastSyncUserData: IRemoteUserData | null): Promise<void> {
154-
const content = this.getKeybindingsContentFromSyncContent(syncData.content);
155-
156-
if (content !== null) {
157-
const fileContent = await this.getLocalFileContent();
158-
await this.applyPreview({
159-
fileContent,
160-
remoteUserData,
161-
lastSyncUserData,
162-
content,
163-
hasConflicts: false,
164-
hasLocalChanged: true,
165-
hasRemoteChanged: true,
166-
isLastSyncFromCurrentMachine: false
167-
}, false);
168-
}
169-
}
170-
171168
protected async applyPreview(preview: IFileSyncPreview, forcePush: boolean): Promise<void> {
172169
let { fileContent, remoteUserData, lastSyncUserData, content, hasLocalChanged, hasRemoteChanged } = preview;
173170

src/vs/platform/userDataSync/common/settingsSync.ts

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,30 @@ export class SettingsSynchroniser extends AbstractJsonFileSynchroniser implement
107107
};
108108
}
109109

110+
protected async generateReplacePreview(syncData: ISyncData, remoteUserData: IRemoteUserData, lastSyncUserData: IRemoteUserData | null): Promise<IFileSyncPreview> {
111+
112+
const fileContent = await this.getLocalFileContent();
113+
const formatUtils = await this.getFormattingOptions();
114+
const ignoredSettings = await this.getIgnoredSettings();
115+
116+
let content: string | null = null;
117+
const settingsSyncContent = this.parseSettingsSyncContent(syncData.content);
118+
if (settingsSyncContent) {
119+
content = updateIgnoredSettings(settingsSyncContent.settings, fileContent ? fileContent.value.toString() : '{}', ignoredSettings, formatUtils);
120+
}
121+
122+
return {
123+
fileContent,
124+
remoteUserData,
125+
lastSyncUserData,
126+
content,
127+
hasLocalChanged: content !== null,
128+
hasRemoteChanged: content !== null,
129+
hasConflicts: false,
130+
isLastSyncFromCurrentMachine: false
131+
};
132+
}
133+
110134
protected async generatePreview(remoteUserData: IRemoteUserData, lastSyncUserData: IRemoteUserData | null, token: CancellationToken = CancellationToken.None): Promise<IFileSyncPreview> {
111135
const fileContent = await this.getLocalFileContent();
112136
const formattingOptions = await this.getFormattingOptions();
@@ -168,27 +192,6 @@ export class SettingsSynchroniser extends AbstractJsonFileSynchroniser implement
168192
return preview;
169193
}
170194

171-
protected async performReplace(syncData: ISyncData, remoteUserData: IRemoteUserData, lastSyncUserData: IRemoteUserData | null): Promise<void> {
172-
const settingsSyncContent = this.parseSettingsSyncContent(syncData.content);
173-
if (settingsSyncContent) {
174-
const fileContent = await this.getLocalFileContent();
175-
const formatUtils = await this.getFormattingOptions();
176-
const ignoredSettings = await this.getIgnoredSettings();
177-
const content = updateIgnoredSettings(settingsSyncContent.settings, fileContent ? fileContent.value.toString() : '{}', ignoredSettings, formatUtils);
178-
179-
await this.applyPreview({
180-
fileContent,
181-
remoteUserData,
182-
lastSyncUserData,
183-
content,
184-
hasLocalChanged: true,
185-
hasRemoteChanged: true,
186-
hasConflicts: false,
187-
isLastSyncFromCurrentMachine: false
188-
}, false);
189-
}
190-
}
191-
192195
protected async applyPreview(preview: IFileSyncPreview, forcePush: boolean): Promise<void> {
193196
let { fileContent, remoteUserData, lastSyncUserData, content, hasLocalChanged, hasRemoteChanged } = preview;
194197

src/vs/platform/userDataSync/common/snippetsSync.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,19 @@ export class SnippetsSynchroniser extends AbstractSynchroniser implements IUserD
101101
};
102102
}
103103

104+
protected async generateReplacePreview(syncData: ISyncData, remoteUserData: IRemoteUserData, lastSyncUserData: IRemoteUserData | null): Promise<ISinppetsSyncPreview> {
105+
const local = await this.getSnippetsFileContents();
106+
const localSnippets = this.toSnippetsContents(local);
107+
const snippets = this.parseSnippets(syncData);
108+
const { added, updated, removed } = merge(localSnippets, snippets, localSnippets);
109+
return {
110+
added, removed, updated, remote: snippets, remoteUserData, local, lastSyncUserData, conflicts: [], resolvedConflicts: {}, hasConflicts: false,
111+
hasLocalChanged: Object.keys(added).length > 0 || removed.length > 0 || Object.keys(updated).length > 0,
112+
hasRemoteChanged: true,
113+
isLastSyncFromCurrentMachine: false,
114+
};
115+
}
116+
104117
protected async generatePreview(remoteUserData: IRemoteUserData, lastSyncUserData: IRemoteUserData | null, token: CancellationToken = CancellationToken.None): Promise<ISinppetsSyncPreview> {
105118
const local = await this.getSnippetsFileContents();
106119
return this.doGeneratePreview(local, remoteUserData, lastSyncUserData, {}, token);
@@ -178,19 +191,6 @@ export class SnippetsSynchroniser extends AbstractSynchroniser implements IUserD
178191
return preview;
179192
}
180193

181-
protected async performReplace(syncData: ISyncData, remoteUserData: IRemoteUserData, lastSyncUserData: IRemoteUserData | null): Promise<void> {
182-
const local = await this.getSnippetsFileContents();
183-
const localSnippets = this.toSnippetsContents(local);
184-
const snippets = this.parseSnippets(syncData);
185-
const { added, updated, removed } = merge(localSnippets, snippets, localSnippets);
186-
await this.applyPreview({
187-
added, removed, updated, remote: snippets, remoteUserData, local, lastSyncUserData, conflicts: [], resolvedConflicts: {}, hasConflicts: false,
188-
hasLocalChanged: Object.keys(added).length > 0 || removed.length > 0 || Object.keys(updated).length > 0,
189-
hasRemoteChanged: true,
190-
isLastSyncFromCurrentMachine: false,
191-
}, false);
192-
}
193-
194194
protected async applyPreview(preview: ISinppetsSyncPreview, forcePush: boolean): Promise<void> {
195195
let { added, removed, updated, local, remote, remoteUserData, lastSyncUserData, hasLocalChanged, hasRemoteChanged } = preview;
196196

src/vs/platform/userDataSync/test/common/synchronizer.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,21 @@ class TestSynchroniser extends AbstractSynchroniser {
4848
return { hasLocalChanged: false, hasRemoteChanged: false, isLastSyncFromCurrentMachine: false, hasConflicts: this.syncResult.hasConflicts, remoteUserData, lastSyncUserData };
4949
}
5050

51+
protected async generateReplacePreview(syncData: ISyncData, remoteUserData: IRemoteUserData, lastSyncUserData: IRemoteUserData | null): Promise<ITestSyncPreview> {
52+
return { hasLocalChanged: false, hasRemoteChanged: false, isLastSyncFromCurrentMachine: false, hasConflicts: this.syncResult.hasConflicts, remoteUserData, lastSyncUserData };
53+
}
54+
5155
protected async generatePreview(remoteUserData: IRemoteUserData, lastSyncUserData: IRemoteUserData | null, token: CancellationToken): Promise<ITestSyncPreview> {
5256
if (this.syncResult.hasError) {
5357
throw new Error('failed');
5458
}
5559
return { ref: remoteUserData.ref, hasLocalChanged: false, hasRemoteChanged: false, isLastSyncFromCurrentMachine: false, hasConflicts: this.syncResult.hasConflicts, remoteUserData, lastSyncUserData };
5660
}
5761

62+
protected async updatePreviewWithConflict(preview: ISyncPreview, conflictResource: URI, conflictContent: string): Promise<ISyncPreview> {
63+
return preview;
64+
}
65+
5866
protected async applyPreview({ ref }: ITestSyncPreview, forcePush: boolean): Promise<void> {
5967
if (ref) {
6068
await this.apply(ref);
@@ -82,14 +90,6 @@ class TestSynchroniser extends AbstractSynchroniser {
8290
this.onDidTriggerLocalChangeCall.fire();
8391
}
8492

85-
protected async performReplace(syncData: ISyncData, remoteUserData: IRemoteUserData, lastSyncUserData: IRemoteUserData | null): Promise<void> {
86-
87-
}
88-
89-
protected async updatePreviewWithConflict(preview: ISyncPreview, conflictResource: URI, conflictContent: string): Promise<ISyncPreview> {
90-
return preview;
91-
}
92-
9393
}
9494

9595
suite('TestSynchronizer', () => {

0 commit comments

Comments
 (0)