提交 2da2634c 编写于 作者: S Sandeep Somavarapu

Remember current machine name

上级 476e179d
......@@ -13,10 +13,7 @@ import { isPromiseCanceledError } from 'vs/base/common/errors';
import { CancellationToken } from 'vs/base/common/cancellation';
import { IStorageService, StorageScope, IWorkspaceStorageChangeEvent } from 'vs/platform/storage/common/storage';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { IUserDataSyncMachine, IUserDataSyncMachinesService } from 'vs/platform/userDataSync/common/userDataSyncMachines';
import { PlatformToString, isWeb, Platform, platform } from 'vs/base/common/platform';
import { escapeRegExpCharacters } from 'vs/base/common/strings';
import { IProductService } from 'vs/platform/product/common/productService';
import { IUserDataSyncMachinesService } from 'vs/platform/userDataSync/common/userDataSyncMachines';
import { localize } from 'vs/nls';
type AutoSyncClassification = {
......@@ -33,7 +30,7 @@ type AutoSyncErrorClassification = {
const enablementKey = 'sync.enable';
const disableMachineEventuallyKey = 'sync.disableMachineEventually';
const SESSION_ID_KEY = 'sync.sessionId';
const sessionIdKey = 'sync.sessionId';
export class UserDataAutoSyncEnablementService extends Disposable {
......@@ -92,7 +89,6 @@ export class UserDataAutoSyncService extends UserDataAutoSyncEnablementService i
@IUserDataSyncAccountService private readonly userDataSyncAccountService: IUserDataSyncAccountService,
@ITelemetryService private readonly telemetryService: ITelemetryService,
@IUserDataSyncMachinesService private readonly userDataSyncMachinesService: IUserDataSyncMachinesService,
@IProductService private readonly productService: IProductService,
@IStorageService storageService: IStorageService,
@IEnvironmentService environmentService: IEnvironmentService
) {
......@@ -114,7 +110,7 @@ export class UserDataAutoSyncService extends UserDataAutoSyncEnablementService i
const { enabled, reason } = this.isAutoSyncEnabled();
if (enabled) {
if (this.autoSync.value === undefined) {
this.autoSync.value = new AutoSync(1000 * 60 * 5 /* 5 miutes */, this.userDataSyncStoreService, this.userDataSyncService, this.userDataSyncMachinesService, this.logService, this.storageService, this.productService);
this.autoSync.value = new AutoSync(1000 * 60 * 5 /* 5 miutes */, this.userDataSyncStoreService, this.userDataSyncService, this.userDataSyncMachinesService, this.logService, this.storageService);
this.autoSync.value.register(this.autoSync.value.onDidStartSync(() => this.lastSyncTriggerTime = new Date().getTime()));
this.autoSync.value.register(this.autoSync.value.onDidFinishSync(e => this.onDidFinishSync(e)));
if (this.startAutoSync()) {
......@@ -167,7 +163,7 @@ export class UserDataAutoSyncService extends UserDataAutoSyncEnablementService i
this.setEnablement(false);
// Reset Session
this.storageService.remove(SESSION_ID_KEY, StorageScope.GLOBAL);
this.storageService.remove(sessionIdKey, StorageScope.GLOBAL);
// Reset
if (everywhere) {
......@@ -309,7 +305,6 @@ class AutoSync extends Disposable {
private readonly userDataSyncMachinesService: IUserDataSyncMachinesService,
private readonly logService: IUserDataSyncLogService,
private readonly storageService: IStorageService,
private readonly productService: IProductService,
) {
super();
}
......@@ -368,7 +363,7 @@ class AutoSync extends Disposable {
throw new UserDataAutoSyncError(localize('turned off', "Cannot sync because syncing is turned off in the cloud"), UserDataSyncErrorCode.TurnedOff);
}
const sessionId = this.storageService.get(SESSION_ID_KEY, StorageScope.GLOBAL);
const sessionId = this.storageService.get(sessionIdKey, StorageScope.GLOBAL);
// Server session is different from client session
if (sessionId && manifest && sessionId !== manifest.session) {
throw new UserDataAutoSyncError(localize('session expired', "Cannot sync because current session is expired"), UserDataSyncErrorCode.SessionExpired);
......@@ -396,7 +391,7 @@ class AutoSync extends Disposable {
// Update local session id
if (manifest && manifest.session !== sessionId) {
this.storageService.store(SESSION_ID_KEY, manifest.session, StorageScope.GLOBAL);
this.storageService.store(sessionIdKey, manifest.session, StorageScope.GLOBAL);
}
// Return if cancellation is requested
......@@ -406,8 +401,7 @@ class AutoSync extends Disposable {
// Add current machine
if (!currentMachine) {
const name = this.computeDefaultMachineName(machines);
await this.userDataSyncMachinesService.addCurrentMachine(name, manifest || undefined);
await this.userDataSyncMachinesService.addCurrentMachine(manifest || undefined);
}
} catch (e) {
......@@ -418,20 +412,6 @@ class AutoSync extends Disposable {
this._onDidFinishSync.fire(error);
}
private computeDefaultMachineName(machines: IUserDataSyncMachine[]): string {
const namePrefix = `${this.productService.nameLong} (${PlatformToString(isWeb ? Platform.Web : platform)})`;
const nameRegEx = new RegExp(`${escapeRegExpCharacters(namePrefix)}\\s#(\\d)`);
let nameIndex = 0;
for (const machine of machines) {
const matches = nameRegEx.exec(machine.name);
const index = matches ? parseInt(matches[1]) : 0;
nameIndex = index > nameIndex ? index : nameIndex;
}
return `${namePrefix} #${nameIndex + 1}`;
}
register<T extends IDisposable>(t: T): T {
return super._register(t);
}
......
......@@ -181,7 +181,7 @@ export class UserDataSyncMachinesServiceChannel implements IServerChannel {
async call(context: any, command: string, args?: any): Promise<any> {
switch (command) {
case 'getMachines': return this.service.getMachines();
case 'addCurrentMachine': return this.service.addCurrentMachine(args[0]);
case 'addCurrentMachine': return this.service.addCurrentMachine();
case 'removeCurrentMachine': return this.service.removeCurrentMachine();
case 'renameMachine': return this.service.renameMachine(args[0], args[1]);
case 'setEnablement': return this.service.setEnablement(args[0], args[1]);
......
......@@ -8,10 +8,12 @@ import { Disposable } from 'vs/base/common/lifecycle';
import { getServiceMachineId } from 'vs/platform/serviceMachineId/common/serviceMachineId';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { IFileService } from 'vs/platform/files/common/files';
import { IStorageService } from 'vs/platform/storage/common/storage';
import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
import { IUserDataSyncStoreService, IUserData, IUserDataSyncLogService, IUserDataManifest } from 'vs/platform/userDataSync/common/userDataSync';
import { localize } from 'vs/nls';
import { IProductService } from 'vs/platform/product/common/productService';
import { PlatformToString, isWeb, Platform, platform } from 'vs/base/common/platform';
import { escapeRegExpCharacters } from 'vs/base/common/strings';
interface IMachineData {
id: string;
......@@ -26,19 +28,20 @@ interface IMachinesData {
export type IUserDataSyncMachine = Readonly<IMachineData> & { readonly isCurrent: boolean };
export const IUserDataSyncMachinesService = createDecorator<IUserDataSyncMachinesService>('IUserDataSyncMachinesService');
export interface IUserDataSyncMachinesService {
_serviceBrand: any;
getMachines(manifest?: IUserDataManifest): Promise<IUserDataSyncMachine[]>;
addCurrentMachine(name: string, manifest?: IUserDataManifest): Promise<void>;
addCurrentMachine(manifest?: IUserDataManifest): Promise<void>;
removeCurrentMachine(manifest?: IUserDataManifest): Promise<void>;
renameMachine(machineId: string, name: string): Promise<void>;
setEnablement(machineId: string, enabled: boolean): Promise<void>;
}
const currentMachineNameKey = 'sync.currentMachineName';
export class UserDataSyncMachinesService extends Disposable implements IUserDataSyncMachinesService {
private static readonly VERSION = 1;
......@@ -52,7 +55,7 @@ export class UserDataSyncMachinesService extends Disposable implements IUserData
constructor(
@IEnvironmentService environmentService: IEnvironmentService,
@IFileService fileService: IFileService,
@IStorageService storageService: IStorageService,
@IStorageService private readonly storageService: IStorageService,
@IUserDataSyncStoreService private readonly userDataSyncStoreService: IUserDataSyncStoreService,
@IUserDataSyncLogService private readonly logService: IUserDataSyncLogService,
@IProductService private readonly productService: IProductService,
......@@ -67,16 +70,13 @@ export class UserDataSyncMachinesService extends Disposable implements IUserData
return machineData.machines.map<IUserDataSyncMachine>(machine => ({ ...machine, ...{ isCurrent: machine.id === currentMachineId } }));
}
async addCurrentMachine(name: string, manifest?: IUserDataManifest): Promise<void> {
async addCurrentMachine(manifest?: IUserDataManifest): Promise<void> {
const currentMachineId = await this.currentMachineIdPromise;
const machineData = await this.readMachinesData(manifest);
let currentMachine = machineData.machines.find(({ id }) => id === currentMachineId);
if (currentMachine) {
currentMachine.name = name;
} else {
machineData.machines.push({ id: currentMachineId, name });
if (!machineData.machines.some(({ id }) => id === currentMachineId)) {
machineData.machines.push({ id: currentMachineId, name: this.computeCurrentMachineName(machineData.machines) });
await this.writeMachinesData(machineData);
}
await this.writeMachinesData(machineData);
}
async removeCurrentMachine(manifest?: IUserDataManifest): Promise<void> {
......@@ -90,11 +90,15 @@ export class UserDataSyncMachinesService extends Disposable implements IUserData
}
async renameMachine(machineId: string, name: string, manifest?: IUserDataManifest): Promise<void> {
const currentMachineId = await this.currentMachineIdPromise;
const machineData = await this.readMachinesData(manifest);
const currentMachine = machineData.machines.find(({ id }) => id === machineId);
if (currentMachine) {
currentMachine.name = name;
const machine = machineData.machines.find(({ id }) => id === machineId);
if (machine) {
machine.name = name;
await this.writeMachinesData(machineData);
if (machineData.machines.some(({ id }) => id === currentMachineId)) {
this.storageService.store(currentMachineNameKey, name, StorageScope.GLOBAL);
}
}
}
......@@ -107,6 +111,23 @@ export class UserDataSyncMachinesService extends Disposable implements IUserData
}
}
private computeCurrentMachineName(machines: IMachineData[]): string {
const previousName = this.storageService.get(currentMachineNameKey, StorageScope.GLOBAL);
if (previousName) {
return previousName;
}
const namePrefix = `${this.productService.nameLong} (${PlatformToString(isWeb ? Platform.Web : platform)})`;
const nameRegEx = new RegExp(`${escapeRegExpCharacters(namePrefix)}\\s#(\\d)`);
let nameIndex = 0;
for (const machine of machines) {
const matches = nameRegEx.exec(machine.name);
const index = matches ? parseInt(matches[1]) : 0;
nameIndex = index > nameIndex ? index : nameIndex;
}
return `${namePrefix} #${nameIndex + 1}`;
}
private async readMachinesData(manifest?: IUserDataManifest): Promise<IMachinesData> {
this.userData = await this.readUserData(manifest);
const machinesData = this.parse(this.userData);
......
......@@ -12,7 +12,6 @@ import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { IStorageService } from 'vs/platform/storage/common/storage';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { IUserDataSyncMachinesService } from 'vs/platform/userDataSync/common/userDataSyncMachines';
import { IProductService } from 'vs/platform/product/common/productService';
export class UserDataAutoSyncService extends BaseUserDataAutoSyncService {
......@@ -25,11 +24,10 @@ export class UserDataAutoSyncService extends BaseUserDataAutoSyncService {
@IUserDataSyncAccountService authTokenService: IUserDataSyncAccountService,
@ITelemetryService telemetryService: ITelemetryService,
@IUserDataSyncMachinesService userDataSyncMachinesService: IUserDataSyncMachinesService,
@IProductService productService: IProductService,
@IStorageService storageService: IStorageService,
@IEnvironmentService environmentService: IEnvironmentService,
) {
super(userDataSyncStoreService, userDataSyncResourceEnablementService, userDataSyncService, logService, authTokenService, telemetryService, userDataSyncMachinesService, productService, storageService, environmentService);
super(userDataSyncStoreService, userDataSyncResourceEnablementService, userDataSyncService, logService, authTokenService, telemetryService, userDataSyncMachinesService, storageService, environmentService);
this._register(Event.debounce<string, string[]>(Event.any<string>(
Event.map(electronService.onWindowFocus, () => 'windowFocus'),
......
......@@ -14,7 +14,6 @@ import { UserDataSyncTrigger } from 'vs/workbench/contrib/userDataSync/browser/u
import { IStorageService } from 'vs/platform/storage/common/storage';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { IUserDataSyncMachinesService } from 'vs/platform/userDataSync/common/userDataSyncMachines';
import { IProductService } from 'vs/platform/product/common/productService';
export class UserDataAutoSyncService extends BaseUserDataAutoSyncService {
......@@ -28,11 +27,10 @@ export class UserDataAutoSyncService extends BaseUserDataAutoSyncService {
@IHostService hostService: IHostService,
@ITelemetryService telemetryService: ITelemetryService,
@IUserDataSyncMachinesService userDataSyncMachinesService: IUserDataSyncMachinesService,
@IProductService productService: IProductService,
@IStorageService storageService: IStorageService,
@IEnvironmentService environmentService: IEnvironmentService,
) {
super(userDataSyncStoreService, userDataSyncResourceEnablementService, userDataSyncService, logService, authTokenService, telemetryService, userDataSyncMachinesService, productService, storageService, environmentService);
super(userDataSyncStoreService, userDataSyncResourceEnablementService, userDataSyncService, logService, authTokenService, telemetryService, userDataSyncMachinesService, storageService, environmentService);
this._register(Event.debounce<string, string[]>(Event.any<string>(
Event.map(hostService.onDidChangeFocus, () => 'windowFocus'),
......
......@@ -26,8 +26,8 @@ class UserDataSyncMachinesService extends Disposable implements IUserDataSyncMac
return this.channel.call<IUserDataSyncMachine[]>('getMachines');
}
addCurrentMachine(name: string): Promise<void> {
return this.channel.call('addCurrentMachine', [name]);
addCurrentMachine(): Promise<void> {
return this.channel.call('addCurrentMachine');
}
removeCurrentMachine(): Promise<void> {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册