From 88e5a183ae7a6a3338b97c4512788e47effbf0e2 Mon Sep 17 00:00:00 2001 From: Sandeep Somavarapu Date: Thu, 12 Sep 2019 16:17:05 +0200 Subject: [PATCH] Use ref instead of version --- src/vs/vscode.proposed.d.ts | 6 +++--- .../workbench/api/common/extHost.protocol.ts | 2 +- src/vs/workbench/api/common/extHostTypes.ts | 4 ++-- src/vs/workbench/api/common/extHostUserData.ts | 4 ++-- .../userData/common/remoteUserDataService.ts | 4 ++-- .../services/userData/common/settingsSync.ts | 18 +++++++++--------- .../services/userData/common/userData.ts | 10 +++++----- 7 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/vs/vscode.proposed.d.ts b/src/vs/vscode.proposed.d.ts index e1956b06e7fe9..734c3ea458db1 100644 --- a/src/vs/vscode.proposed.d.ts +++ b/src/vs/vscode.proposed.d.ts @@ -1026,7 +1026,7 @@ declare module 'vscode' { export class UserDataError extends Error { - static VersionExists(): FileSystemError; + static Rejected(): FileSystemError; /** * Creates a new userData error. @@ -1036,9 +1036,9 @@ declare module 'vscode' { export interface UserDataProvider { - read(key: string): Promise<{ version: number, content: string } | null>; + read(key: string): Promise<{ content: string, ref: string } | null>; - write(key: string, version: number, content: string): Promise; + write(key: string, content: string, ref: string | null): Promise; } diff --git a/src/vs/workbench/api/common/extHost.protocol.ts b/src/vs/workbench/api/common/extHost.protocol.ts index 0b790e0b9d36f..1b632ceeb172a 100644 --- a/src/vs/workbench/api/common/extHost.protocol.ts +++ b/src/vs/workbench/api/common/extHost.protocol.ts @@ -755,7 +755,7 @@ export interface ExtHostConfigurationShape { export interface ExtHostUserDataShape { $read(key: string): Promise; - $write(key: string, version: number, content: string): Promise; + $write(key: string, content: string, ref: string): Promise; } export interface ExtHostDiagnosticsShape { diff --git a/src/vs/workbench/api/common/extHostTypes.ts b/src/vs/workbench/api/common/extHostTypes.ts index 6d19f8d7669bd..562f353bf0687 100644 --- a/src/vs/workbench/api/common/extHostTypes.ts +++ b/src/vs/workbench/api/common/extHostTypes.ts @@ -2372,8 +2372,8 @@ export class Decoration { @es5ClassCompat export class UserDataError extends Error { - static VersionExists(message?: string): UserDataError { - return new UserDataError(message, RemoteUserDataErrorCode.VersionExists); + static Rejected(message?: string): UserDataError { + return new UserDataError(message, RemoteUserDataErrorCode.Rejected); } constructor(message?: string, code: RemoteUserDataErrorCode = RemoteUserDataErrorCode.Unknown) { diff --git a/src/vs/workbench/api/common/extHostUserData.ts b/src/vs/workbench/api/common/extHostUserData.ts index 5c671f795b534..a8029eaebc5fd 100644 --- a/src/vs/workbench/api/common/extHostUserData.ts +++ b/src/vs/workbench/api/common/extHostUserData.ts @@ -38,11 +38,11 @@ export class ExtHostUserData implements ExtHostUserDataShape { return this.userDataProvider.read(key); } - $write(key: string, version: number, content: string): Promise { + $write(key: string, content: string, ref: string): Promise { if (!this.userDataProvider) { throw new Error('No remote user data provider exists.'); } - return this.userDataProvider.write(key, version, content); + return this.userDataProvider.write(key, content, ref); } } diff --git a/src/vs/workbench/services/userData/common/remoteUserDataService.ts b/src/vs/workbench/services/userData/common/remoteUserDataService.ts index c244ae82529cd..2a007edc5f097 100644 --- a/src/vs/workbench/services/userData/common/remoteUserDataService.ts +++ b/src/vs/workbench/services/userData/common/remoteUserDataService.ts @@ -57,11 +57,11 @@ export class RemoteUserDataService extends Disposable implements IRemoteUserData .then(null, error => Promise.reject(new RemoteUserDataError(error.message, toUserDataErrorCode(error)))); } - write(key: string, version: number, content: string): Promise { + write(key: string, content: string, ref: string | null): Promise { if (!this.remoteUserDataProvider) { throw new Error('No remote user data provider exists.'); } - return this.remoteUserDataProvider.write(key, version, content) + return this.remoteUserDataProvider.write(key, content, ref) .then(null, error => Promise.reject(new RemoteUserDataError(error.message, toUserDataErrorCode(error)))); } diff --git a/src/vs/workbench/services/userData/common/settingsSync.ts b/src/vs/workbench/services/userData/common/settingsSync.ts index f722926677aa6..db48f9a4a5438 100644 --- a/src/vs/workbench/services/userData/common/settingsSync.ts +++ b/src/vs/workbench/services/userData/common/settingsSync.ts @@ -124,8 +124,9 @@ export class SettingsSyncService extends Disposable implements ISynchroniser, IT const result = await this.syncPreviewResultPromise; let remoteUserData = result.remoteUserData; if (result.hasRemoteChanged) { - remoteUserData = { version: result.remoteUserData ? result.remoteUserData.version + 1 : 1, content: result.settingsPreviewModel.getValue() }; - await this.writeToRemote(remoteUserData); + const content = result.settingsPreviewModel.getValue(); + const ref = await this.writeToRemote(result.settingsPreviewModel.getValue(), remoteUserData ? remoteUserData.ref : null); + remoteUserData = { ref, content }; } if (result.hasLocalChanged) { await this.writeToLocal(result.settingsPreviewModel.getValue(), result.fileContent); @@ -193,7 +194,7 @@ export class SettingsSyncService extends Disposable implements ISynchroniser, IT } // Remote has moved forward - if (remoteUserData.version !== lastSyncData.version) { + if (remoteUserData.ref !== lastSyncData.ref) { // Local content is same as last synced. So, sync with remote content. if (lastSyncData.content === localContent) { @@ -211,7 +212,7 @@ export class SettingsSyncService extends Disposable implements ISynchroniser, IT } // Remote data is same as last synced data - if (lastSyncData.version === remoteUserData.version) { + if (lastSyncData.ref === remoteUserData.ref) { // Local contents are same as last synced data. No op. if (lastSyncData.content === localContent) { @@ -362,7 +363,7 @@ export class SettingsSyncService extends Disposable implements ISynchroniser, IT // Removed in Local, but updated in Remote const position = new Position(settingsPreviewModel.getLineCount() - 1, settingsPreviewModel.getLineMaxColumn(settingsPreviewModel.getLineCount() - 1)); const editOperations = [ - EditOperation.insert(position, `${settingsPreviewModel.getEOL()}<<<<<<< local${ settingsPreviewModel.getEOL() }=======${ settingsPreviewModel.getEOL() }${ remoteContent }>>>>>>> remote`) + EditOperation.insert(position, `${settingsPreviewModel.getEOL()}<<<<<<< local${settingsPreviewModel.getEOL()}=======${settingsPreviewModel.getEOL()}${remoteContent}>>>>>>> remote`) ]; settingsPreviewModel.pushEditOperations([new Selection(position.lineNumber, position.column, position.lineNumber, position.column)], editOperations, () => []); } @@ -409,13 +410,12 @@ export class SettingsSyncService extends Disposable implements ISynchroniser, IT } } - private async writeToRemote(userData: IUserData): Promise { + private async writeToRemote(content: string, ref: string | null): Promise { try { - await this.remoteUserDataService.write(SettingsSyncService.EXTERNAL_USER_DATA_SETTINGS_KEY, userData.version, userData.content); + return await this.remoteUserDataService.write(SettingsSyncService.EXTERNAL_USER_DATA_SETTINGS_KEY, content, ref); } catch (e) { - if (e instanceof RemoteUserDataError && e.code === RemoteUserDataErrorCode.VersionExists) { + if (e instanceof RemoteUserDataError && e.code === RemoteUserDataErrorCode.Rejected) { // Rejected as there is a new version. Sync again - await this.sync(); } // An unknown error throw e; diff --git a/src/vs/workbench/services/userData/common/userData.ts b/src/vs/workbench/services/userData/common/userData.ts index 29175722d3be9..52874ff1d5678 100644 --- a/src/vs/workbench/services/userData/common/userData.ts +++ b/src/vs/workbench/services/userData/common/userData.ts @@ -7,12 +7,12 @@ import { createDecorator } from 'vs/platform/instantiation/common/instantiation' import { Event } from 'vs/base/common/event'; export interface IUserData { - version: number; + ref: string; content: string; } export enum RemoteUserDataErrorCode { - VersionExists = 'VersionExists', + Rejected = 'Rejected', Unknown = 'Unknown' } @@ -42,7 +42,7 @@ export function toUserDataErrorCode(error: Error | undefined | null): RemoteUser } switch (match[1]) { - case RemoteUserDataErrorCode.VersionExists: return RemoteUserDataErrorCode.VersionExists; + case RemoteUserDataErrorCode.Rejected: return RemoteUserDataErrorCode.Rejected; } return RemoteUserDataErrorCode.Unknown; @@ -60,7 +60,7 @@ export interface IRemoteUserDataProvider { read(key: string): Promise; - write(key: string, version: number, content: string): Promise; + write(key: string, content: string, ref: string | null): Promise; } @@ -82,7 +82,7 @@ export interface IRemoteUserDataService { read(key: string): Promise; - write(key: string, version: number, content: string): Promise; + write(key: string, content: string, ref: string | null): Promise; }