/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ import { SyncStatus, ISettingsSyncService, IConflictSetting, SyncSource } from 'vs/platform/userDataSync/common/userDataSync'; import { ISharedProcessService } from 'vs/platform/ipc/electron-browser/sharedProcessService'; import { Disposable } from 'vs/base/common/lifecycle'; import { Emitter, Event } from 'vs/base/common/event'; import { IChannel } from 'vs/base/parts/ipc/common/ipc'; import { registerSingleton } from 'vs/platform/instantiation/common/extensions'; export class SettingsSyncService extends Disposable implements ISettingsSyncService { _serviceBrand: undefined; private readonly channel: IChannel; readonly resourceKey = 'settings'; readonly source = SyncSource.Settings; private _status: SyncStatus = SyncStatus.Uninitialized; get status(): SyncStatus { return this._status; } private _onDidChangeStatus: Emitter = this._register(new Emitter()); readonly onDidChangeStatus: Event = this._onDidChangeStatus.event; private _conflicts: IConflictSetting[] = []; get conflicts(): IConflictSetting[] { return this._conflicts; } private _onDidChangeConflicts: Emitter = this._register(new Emitter()); readonly onDidChangeConflicts: Event = this._onDidChangeConflicts.event; get onDidChangeLocal(): Event { return this.channel.listen('onDidChangeLocal'); } constructor( @ISharedProcessService sharedProcessService: ISharedProcessService ) { super(); this.channel = sharedProcessService.getChannel('settingsSync'); this.channel.call('_getInitialStatus').then(status => { this.updateStatus(status); this._register(this.channel.listen('onDidChangeStatus')(status => this.updateStatus(status))); }); this.channel.call('_getInitialConflicts').then(conflicts => { if (conflicts.length) { this.updateConflicts(conflicts); } this._register(this.channel.listen('onDidChangeConflicts')(conflicts => this.updateConflicts(conflicts))); }); } pull(): Promise { return this.channel.call('pull'); } push(): Promise { return this.channel.call('push'); } sync(): Promise { return this.channel.call('sync'); } stop(): Promise { return this.channel.call('stop'); } resetLocal(): Promise { return this.channel.call('resetLocal'); } hasPreviouslySynced(): Promise { return this.channel.call('hasPreviouslySynced'); } hasLocalData(): Promise { return this.channel.call('hasLocalData'); } accept(content: string): Promise { return this.channel.call('accept', [content]); } resolveSettingsConflicts(conflicts: { key: string, value: any | undefined }[]): Promise { return this.channel.call('resolveConflicts', [conflicts]); } getRemoteContent(preview?: boolean): Promise { return this.channel.call('getRemoteContent', [!!preview]); } resolveContent(ref: string): Promise { return this.channel.call('resolveContent', [ref]); } private async updateStatus(status: SyncStatus): Promise { this._status = status; this._onDidChangeStatus.fire(status); } private async updateConflicts(conflicts: IConflictSetting[]): Promise { this._conflicts = conflicts; this._onDidChangeConflicts.fire(conflicts); } } registerSingleton(ISettingsSyncService, SettingsSyncService);