userDataSyncIpc.ts 6.5 KB
Newer Older
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

S
Sandeep Somavarapu 已提交
6
import { IServerChannel, IChannel } from 'vs/base/parts/ipc/common/ipc';
S
Sandeep Somavarapu 已提交
7
import { Event, Emitter } from 'vs/base/common/event';
8
import { IUserDataSyncService, IUserDataSyncUtilService, IUserDataAutoSyncService } from 'vs/platform/userDataSync/common/userDataSync';
S
Sandeep Somavarapu 已提交
9 10 11
import { URI } from 'vs/base/common/uri';
import { IStringDictionary } from 'vs/base/common/collections';
import { FormattingOptions } from 'vs/base/common/jsonFormatter';
12 13
import { IStorageKeysSyncRegistryService, IStorageKey } from 'vs/platform/userDataSync/common/storageKeys';
import { Disposable } from 'vs/base/common/lifecycle';
S
Sandeep Somavarapu 已提交
14
import { ILogService } from 'vs/platform/log/common/log';
15 16 17

export class UserDataSyncChannel implements IServerChannel {

S
Sandeep Somavarapu 已提交
18
	constructor(private readonly service: IUserDataSyncService, private readonly logService: ILogService) { }
19 20 21 22

	listen(_: unknown, event: string): Event<any> {
		switch (event) {
			case 'onDidChangeStatus': return this.service.onDidChangeStatus;
23
			case 'onDidChangeConflicts': return this.service.onDidChangeConflicts;
24
			case 'onDidChangeLocal': return this.service.onDidChangeLocal;
25
			case 'onDidChangeLastSyncTime': return this.service.onDidChangeLastSyncTime;
S
Sandeep Somavarapu 已提交
26
			case 'onSyncErrors': return this.service.onSyncErrors;
27 28 29 30
		}
		throw new Error(`Event not found: ${event}`);
	}

S
Sandeep Somavarapu 已提交
31 32 33 34 35 36
	async call(context: any, command: string, args?: any): Promise<any> {
		try {
			const result = await this._call(context, command, args);
			return result;
		} catch (e) {
			this.logService.error(e);
37
			throw e;
S
Sandeep Somavarapu 已提交
38 39 40 41
		}
	}

	private _call(context: any, command: string, args?: any): Promise<any> {
42
		switch (command) {
43
			case '_getInitialData': return Promise.resolve([this.service.status, this.service.conflicts, this.service.lastSyncTime]);
44
			case 'pull': return this.service.pull();
45
			case 'sync': return this.service.sync();
S
Sandeep Somavarapu 已提交
46
			case 'stop': this.service.stop(); return Promise.resolve();
47
			case 'replace': return this.service.replace(URI.revive(args[0]));
S
Sandeep Somavarapu 已提交
48 49
			case 'reset': return this.service.reset();
			case 'resetLocal': return this.service.resetLocal();
50
			case 'isFirstTimeSyncWithMerge': return this.service.isFirstTimeSyncWithMerge();
51 52 53 54 55
			case 'acceptConflict': return this.service.acceptConflict(URI.revive(args[0]), args[1]);
			case 'resolveContent': return this.service.resolveContent(URI.revive(args[0]));
			case 'getLocalSyncResourceHandles': return this.service.getLocalSyncResourceHandles(args[0]);
			case 'getRemoteSyncResourceHandles': return this.service.getRemoteSyncResourceHandles(args[0]);
			case 'getAssociatedResources': return this.service.getAssociatedResources(args[0], { created: args[1].created, uri: URI.revive(args[1].uri) });
56 57 58 59
		}
		throw new Error('Invalid call');
	}
}
S
Sandeep Somavarapu 已提交
60

S
Sandeep Somavarapu 已提交
61 62 63 64 65
export class UserDataAutoSyncChannel implements IServerChannel {

	constructor(private readonly service: IUserDataAutoSyncService) { }

	listen(_: unknown, event: string): Event<any> {
S
Sandeep Somavarapu 已提交
66 67 68
		switch (event) {
			case 'onError': return this.service.onError;
		}
S
Sandeep Somavarapu 已提交
69 70 71 72 73
		throw new Error(`Event not found: ${event}`);
	}

	call(context: any, command: string, args?: any): Promise<any> {
		switch (command) {
74
			case 'triggerAutoSync': return this.service.triggerAutoSync(args[0]);
S
Sandeep Somavarapu 已提交
75 76 77 78 79
		}
		throw new Error('Invalid call');
	}
}

S
Sandeep Somavarapu 已提交
80 81 82 83 84 85 86 87 88 89
export class UserDataSycnUtilServiceChannel implements IServerChannel {

	constructor(private readonly service: IUserDataSyncUtilService) { }

	listen(_: unknown, event: string): Event<any> {
		throw new Error(`Event not found: ${event}`);
	}

	call(context: any, command: string, args?: any): Promise<any> {
		switch (command) {
S
Sandeep Somavarapu 已提交
90
			case 'resolveDefaultIgnoredSettings': return this.service.resolveDefaultIgnoredSettings();
S
Sandeep Somavarapu 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104
			case 'resolveUserKeybindings': return this.service.resolveUserBindings(args[0]);
			case 'resolveFormattingOptions': return this.service.resolveFormattingOptions(URI.revive(args[0]));
		}
		throw new Error('Invalid call');
	}
}

export class UserDataSyncUtilServiceClient implements IUserDataSyncUtilService {

	_serviceBrand: undefined;

	constructor(private readonly channel: IChannel) {
	}

S
Sandeep Somavarapu 已提交
105 106 107 108
	async resolveDefaultIgnoredSettings(): Promise<string[]> {
		return this.channel.call('resolveDefaultIgnoredSettings');
	}

S
Sandeep Somavarapu 已提交
109 110 111 112 113 114 115 116 117 118
	async resolveUserBindings(userbindings: string[]): Promise<IStringDictionary<string>> {
		return this.channel.call('resolveUserKeybindings', [userbindings]);
	}

	async resolveFormattingOptions(file: URI): Promise<FormattingOptions> {
		return this.channel.call('resolveFormattingOptions', [file]);
	}

}

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
export class StorageKeysSyncRegistryChannel implements IServerChannel {

	constructor(private readonly service: IStorageKeysSyncRegistryService) { }

	listen(_: unknown, event: string): Event<any> {
		switch (event) {
			case 'onDidChangeStorageKeys': return this.service.onDidChangeStorageKeys;
		}
		throw new Error(`Event not found: ${event}`);
	}

	call(context: any, command: string, args?: any): Promise<any> {
		switch (command) {
			case '_getInitialData': return Promise.resolve(this.service.storageKeys);
			case 'registerStorageKey': return Promise.resolve(this.service.registerStorageKey(args[0]));
		}
		throw new Error('Invalid call');
	}
}

export class StorageKeysSyncRegistryChannelClient extends Disposable implements IStorageKeysSyncRegistryService {

	_serviceBrand: undefined;

	private _storageKeys: ReadonlyArray<IStorageKey> = [];
	get storageKeys(): ReadonlyArray<IStorageKey> { return this._storageKeys; }
	private readonly _onDidChangeStorageKeys: Emitter<ReadonlyArray<IStorageKey>> = this._register(new Emitter<ReadonlyArray<IStorageKey>>());
	readonly onDidChangeStorageKeys = this._onDidChangeStorageKeys.event;

	constructor(private readonly channel: IChannel) {
		super();
		this.channel.call<IStorageKey[]>('_getInitialData').then(storageKeys => {
			this.updateStorageKeys(storageKeys);
			this._register(this.channel.listen<ReadonlyArray<IStorageKey>>('onDidChangeStorageKeys')(storageKeys => this.updateStorageKeys(storageKeys)));
		});
	}

	private async updateStorageKeys(storageKeys: ReadonlyArray<IStorageKey>): Promise<void> {
		this._storageKeys = storageKeys;
		this._onDidChangeStorageKeys.fire(this.storageKeys);
	}

	registerStorageKey(storageKey: IStorageKey): void {
		this.channel.call('registerStorageKey', [storageKey]);
	}

}