Skip to content

Commit

Permalink
Use ref instead of version
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandeep Somavarapu authored and Sandeep Somavarapu committed Sep 12, 2019
1 parent eb55f59 commit 88e5a18
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/vs/vscode.proposed.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,7 @@ declare module 'vscode' {

export class UserDataError extends Error {

static VersionExists(): FileSystemError;
static Rejected(): FileSystemError;

/**
* Creates a new userData error.
Expand All @@ -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<void>;
write(key: string, content: string, ref: string | null): Promise<string>;

}

Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/api/common/extHost.protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ export interface ExtHostConfigurationShape {

export interface ExtHostUserDataShape {
$read(key: string): Promise<IUserData | null>;
$write(key: string, version: number, content: string): Promise<void>;
$write(key: string, content: string, ref: string): Promise<string>;
}

export interface ExtHostDiagnosticsShape {
Expand Down
4 changes: 2 additions & 2 deletions src/vs/workbench/api/common/extHostTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/vs/workbench/api/common/extHostUserData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ export class ExtHostUserData implements ExtHostUserDataShape {
return this.userDataProvider.read(key);
}

$write(key: string, version: number, content: string): Promise<void> {
$write(key: string, content: string, ref: string): Promise<string> {
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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
write(key: string, content: string, ref: string | null): Promise<string> {
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))));
}

Expand Down
18 changes: 9 additions & 9 deletions src/vs/workbench/services/userData/common/settingsSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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, () => []);
}
Expand Down Expand Up @@ -409,13 +410,12 @@ export class SettingsSyncService extends Disposable implements ISynchroniser, IT
}
}

private async writeToRemote(userData: IUserData): Promise<void> {
private async writeToRemote(content: string, ref: string | null): Promise<string> {
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;
Expand Down
10 changes: 5 additions & 5 deletions src/vs/workbench/services/userData/common/userData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}

Expand Down Expand Up @@ -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;
Expand All @@ -60,7 +60,7 @@ export interface IRemoteUserDataProvider {

read(key: string): Promise<IUserData | null>;

write(key: string, version: number, content: string): Promise<void>;
write(key: string, content: string, ref: string | null): Promise<string>;

}

Expand All @@ -82,7 +82,7 @@ export interface IRemoteUserDataService {

read(key: string): Promise<IUserData | null>;

write(key: string, version: number, content: string): Promise<void>;
write(key: string, content: string, ref: string | null): Promise<string>;

}

Expand Down

0 comments on commit 88e5a18

Please sign in to comment.