Skip to content

Commit

Permalink
move user data sync services to platform
Browse files Browse the repository at this point in the history
  • Loading branch information
sandy081 committed Sep 16, 2019
1 parent d26e39b commit 8fbdeb7
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 97 deletions.
94 changes: 94 additions & 0 deletions src/vs/platform/userDataSync/common/userDataSync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import { Event } from 'vs/base/common/event';
import { URI } from 'vs/base/common/uri';

export interface IUserData {
ref: string;
content: string;
}

export enum UserDataSyncStoreErrorCode {
Rejected = 'Rejected',
Unknown = 'Unknown'
}

export function markAsUserDataSyncStoreError(error: Error, code: UserDataSyncStoreErrorCode): Error {
error.name = code ? `${code} (UserDataSyncStoreError)` : `UserDataSyncStoreError`;

return error;
}

export function toUserDataSyncStoreErrorCode(error: Error | undefined | null): UserDataSyncStoreErrorCode {

// Guard against abuse
if (!error) {
return UserDataSyncStoreErrorCode.Unknown;
}

// FileSystemProviderError comes with the code
if (error instanceof UserDataSyncStoreError) {
return error.code;
}

// Any other error, check for name match by assuming that the error
// went through the markAsUserDataSyncStoreError() method
const match = /^(.+) \(UserDataSyncStoreError\)$/.exec(error.name);
if (!match) {
return UserDataSyncStoreErrorCode.Unknown;
}

switch (match[1]) {
case UserDataSyncStoreErrorCode.Rejected: return UserDataSyncStoreErrorCode.Rejected;
}

return UserDataSyncStoreErrorCode.Unknown;
}

export class UserDataSyncStoreError extends Error {

constructor(message: string, public readonly code: UserDataSyncStoreErrorCode) {
super(message);
}

}

export interface IUserDataSyncStore {

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

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

}

export enum SyncStatus {
Uninitialized = 'uninitialized',
Idle = 'idle',
Syncing = 'syncing',
HasConflicts = 'hasConflicts',
}

export const USER_DATA_PREVIEW_SCHEME = 'vscode-userdata-preview';
export const SETTINGS_PREVIEW_RESOURCE = URI.file('Settings-Preview').with({ scheme: USER_DATA_PREVIEW_SCHEME });

export interface ISynchroniser {

readonly status: SyncStatus;
readonly onDidChangeStatus: Event<SyncStatus>;

readonly onDidChangeLocal: Event<void>;

sync(): Promise<boolean>;
continueSync(): Promise<boolean>;
handleConflicts(): boolean;
}

export const IUserDataSyncService = createDecorator<IUserDataSyncService>('IUserDataSyncService');

export interface IUserDataSyncService extends ISynchroniser {
_serviceBrand: any;
}
3 changes: 2 additions & 1 deletion src/vs/workbench/api/browser/mainThreadUserData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import { Disposable, toDisposable } from 'vs/base/common/lifecycle';
import { MainContext, ExtHostContext, IExtHostContext, MainThreadUserDataShape, ExtHostUserDataShape } from '../common/extHost.protocol';
import { extHostNamedCustomer } from 'vs/workbench/api/common/extHostCustomers';
import { IUserDataSyncStoreService, IUserData } from 'vs/workbench/services/userData/common/userData';
import { IUserData } from 'vs/platform/userDataSync/common/userDataSync';
import { IUserDataSyncStoreService } from 'vs/workbench/services/userData/common/userData';

@extHostNamedCustomer(MainContext.MainThreadUserData)
export class MainThreadUserData extends Disposable implements MainThreadUserDataShape {
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 @@ -46,7 +46,7 @@ import { ExtensionActivationError } from 'vs/workbench/services/extensions/commo
import { createExtHostContextProxyIdentifier as createExtId, createMainContextProxyIdentifier as createMainId, IRPCProtocol } from 'vs/workbench/services/extensions/common/proxyIdentifier';
import * as search from 'vs/workbench/services/search/common/search';
import { SaveReason } from 'vs/workbench/services/textfile/common/textfiles';
import { IUserData } from 'vs/workbench/services/userData/common/userData';
import { IUserData } from 'vs/platform/userDataSync/common/userDataSync';

export interface IEnvironment {
isExtensionDevelopmentDebug: boolean;
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/api/common/extHostTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { generateUuid } from 'vs/base/common/uuid';
import * as vscode from 'vscode';
import { FileSystemProviderErrorCode, markAsFileSystemProviderError } from 'vs/platform/files/common/files';
import { RemoteAuthorityResolverErrorCode } from 'vs/platform/remote/common/remoteAuthorityResolver';
import { UserDataSyncStoreErrorCode, markAsUserDataSyncStoreError } from 'vs/workbench/services/userData/common/userData';
import { UserDataSyncStoreErrorCode, markAsUserDataSyncStoreError } from 'vs/platform/userDataSync/common/userDataSync';

function es5ClassCompat(target: Function): any {
///@ts-ignore
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/api/common/extHostUserData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ExtHostUserDataShape, MainThreadUserDataShape } from './extHost.protoco
import * as vscode from 'vscode';
import { toDisposable, Disposable } from 'vs/base/common/lifecycle';
import { ILogService } from 'vs/platform/log/common/log';
import { IUserData } from 'vs/workbench/services/userData/common/userData';
import { IUserData } from 'vs/platform/userDataSync/common/userDataSync';

export class ExtHostUserData implements ExtHostUserDataShape {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
*--------------------------------------------------------------------------------------------*/

import { IWorkbenchContributionsRegistry, Extensions as WorkbenchExtensions, IWorkbenchContribution } from 'vs/workbench/common/contributions';
import { IUserDataSyncService, SyncStatus, USER_DATA_PREVIEW_SCHEME, IUserDataSyncStoreService } from 'vs/workbench/services/userData/common/userData';
import { IUserDataSyncService, SyncStatus, USER_DATA_PREVIEW_SCHEME } from 'vs/platform/userDataSync/common/userDataSync';
import { IUserDataSyncStoreService } from 'vs/workbench/services/userData/common/userData';
import { localize } from 'vs/nls';
import { Disposable, MutableDisposable, toDisposable } from 'vs/base/common/lifecycle';
import { CommandsRegistry } from 'vs/platform/commands/common/commands';
Expand Down
3 changes: 2 additions & 1 deletion src/vs/workbench/services/userData/common/settingsSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { Disposable } from 'vs/base/common/lifecycle';
import { IFileService, FileSystemProviderErrorCode, FileSystemProviderError, IFileContent } from 'vs/platform/files/common/files';
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
import { IUserDataSyncStoreService, IUserData, UserDataSyncStoreError, UserDataSyncStoreErrorCode, ISynchroniser, SyncStatus, SETTINGS_PREVIEW_RESOURCE } from 'vs/workbench/services/userData/common/userData';
import { IUserData, UserDataSyncStoreError, UserDataSyncStoreErrorCode, ISynchroniser, SyncStatus, SETTINGS_PREVIEW_RESOURCE } from 'vs/platform/userDataSync/common/userDataSync';
import { IUserDataSyncStoreService } from 'vs/workbench/services/userData/common/userData';
import { VSBuffer } from 'vs/base/common/buffer';
import { parse, findNodeAtLocation, parseTree, ParseError } from 'vs/base/common/json';
import { ITextModel } from 'vs/editor/common/model';
Expand Down
90 changes: 1 addition & 89 deletions src/vs/workbench/services/userData/common/userData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,65 +5,7 @@

import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import { Event } from 'vs/base/common/event';
import { URI } from 'vs/base/common/uri';

export interface IUserData {
ref: string;
content: string;
}

export enum UserDataSyncStoreErrorCode {
Rejected = 'Rejected',
Unknown = 'Unknown'
}

export function markAsUserDataSyncStoreError(error: Error, code: UserDataSyncStoreErrorCode): Error {
error.name = code ? `${code} (UserDataSyncStoreError)` : `UserDataSyncStoreError`;

return error;
}

export function toUserDataSyncStoreErrorCode(error: Error | undefined | null): UserDataSyncStoreErrorCode {

// Guard against abuse
if (!error) {
return UserDataSyncStoreErrorCode.Unknown;
}

// FileSystemProviderError comes with the code
if (error instanceof UserDataSyncStoreError) {
return error.code;
}

// Any other error, check for name match by assuming that the error
// went through the markAsUserDataSyncStoreError() method
const match = /^(.+) \(UserDataSyncStoreError\)$/.exec(error.name);
if (!match) {
return UserDataSyncStoreErrorCode.Unknown;
}

switch (match[1]) {
case UserDataSyncStoreErrorCode.Rejected: return UserDataSyncStoreErrorCode.Rejected;
}

return UserDataSyncStoreErrorCode.Unknown;
}

export class UserDataSyncStoreError extends Error {

constructor(message: string, public readonly code: UserDataSyncStoreErrorCode) {
super(message);
}

}

export interface IUserDataSyncStore {

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

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

}
import { IUserDataSyncStore, IUserData } from 'vs/platform/userDataSync/common/userDataSync';

export const IUserDataSyncStoreService = createDecorator<IUserDataSyncStoreService>('IUserDataSyncStoreService');

Expand All @@ -72,42 +14,12 @@ export interface IUserDataSyncStoreService {
_serviceBrand: undefined;

readonly onDidChangeEnablement: Event<boolean>;

isEnabled(): boolean;

registerUserDataSyncStore(name: string, userDataSyncStore: IUserDataSyncStore): void;

deregisterUserDataSyncStore(): void;

getName(): string | null;

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

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

}

export enum SyncStatus {
Uninitialized = 'uninitialized',
Idle = 'idle',
Syncing = 'syncing',
HasConflicts = 'hasConflicts',
}

export const USER_DATA_PREVIEW_SCHEME = 'vscode-userdata-preview';
export const SETTINGS_PREVIEW_RESOURCE = URI.file('Settings-Preview').with({ scheme: USER_DATA_PREVIEW_SCHEME });

export interface ISynchroniser {
readonly status: SyncStatus;
readonly onDidChangeStatus: Event<SyncStatus>;
readonly onDidChangeLocal: Event<void>;
sync(): Promise<boolean>;
continueSync(): Promise<boolean>;
handleConflicts(): boolean;
}

export const IUserDataSyncService = createDecorator<IUserDataSyncService>('IUserDataSyncService');

export interface IUserDataSyncService extends ISynchroniser {
_serviceBrand: any;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { IUserDataSyncService, SyncStatus, IUserDataSyncStoreService, ISynchroniser, USER_DATA_PREVIEW_SCHEME } from 'vs/workbench/services/userData/common/userData';
import { IUserDataSyncService, SyncStatus, ISynchroniser, USER_DATA_PREVIEW_SCHEME } from 'vs/platform/userDataSync/common/userDataSync';
import { IUserDataSyncStoreService } from 'vs/workbench/services/userData/common/userData';
import { Disposable } from 'vs/base/common/lifecycle';
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import { Disposable, } from 'vs/base/common/lifecycle';
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
import { Emitter, Event } from 'vs/base/common/event';
import { IUserDataSyncStoreService, IUserDataSyncStore, IUserData, UserDataSyncStoreError, toUserDataSyncStoreErrorCode } from 'vs/workbench/services/userData/common/userData';
import { IUserDataSyncStore, IUserData, UserDataSyncStoreError, toUserDataSyncStoreErrorCode } from 'vs/platform/userDataSync/common/userDataSync';
import { IUserDataSyncStoreService } from 'vs/workbench/services/userData/common/userData';
import { ILogService } from 'vs/platform/log/common/log';

export class UserDataSyncStoreService extends Disposable implements IUserDataSyncStoreService {
Expand Down

0 comments on commit 8fbdeb7

Please sign in to comment.