storageIpc.ts 6.6 KB
Newer Older
B
wip  
Benjamin Pasero 已提交
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.
 *--------------------------------------------------------------------------------------------*/

6
import { IChannel, IServerChannel } from 'vs/base/parts/ipc/common/ipc';
J
Joao Moreno 已提交
7
import { Event, Emitter } from 'vs/base/common/event';
8
import { StorageMainService, IStorageChangeEvent } from 'vs/platform/storage/node/storageMainService';
9
import { IUpdateRequest, IStorageDatabase, IStorageItemsChangeEvent } from 'vs/base/parts/storage/common/storage';
10
import { mapToSerializable, serializableToMap, values } from 'vs/base/common/map';
11
import { Disposable, IDisposable, dispose } from 'vs/base/common/lifecycle';
12 13 14 15
import { onUnexpectedError } from 'vs/base/common/errors';
import { ILogService } from 'vs/platform/log/common/log';
import { generateUuid } from 'vs/base/common/uuid';
import { instanceStorageKey, firstSessionDateStorageKey, lastSessionDateStorageKey, currentSessionDateStorageKey } from 'vs/platform/telemetry/node/workbenchCommonProperties';
16

17 18 19 20 21 22 23 24 25 26 27
type Key = string;
type Value = string;
type Item = [Key, Value];

interface ISerializableUpdateRequest {
	insert?: Item[];
	delete?: Key[];
}

interface ISerializableItemsChangeEvent {
	items: Item[];
28
}
B
wip  
Benjamin Pasero 已提交
29

30
export class GlobalStorageDatabaseChannel extends Disposable implements IServerChannel {
B
wip  
Benjamin Pasero 已提交
31

32 33
	private static STORAGE_CHANGE_DEBOUNCE_TIME = 100;

M
Matt Bierner 已提交
34
	private readonly _onDidChangeItems: Emitter<ISerializableItemsChangeEvent> = this._register(new Emitter<ISerializableItemsChangeEvent>());
35
	get onDidChangeItems(): Event<ISerializableItemsChangeEvent> { return this._onDidChangeItems.event; }
B
wip  
Benjamin Pasero 已提交
36

37 38 39 40 41 42
	private whenReady: Promise<void>;

	constructor(
		private logService: ILogService,
		private storageMainService: StorageMainService
	) {
43 44
		super();

45 46 47
		this.whenReady = this.init();
	}

48 49 50 51
	private async init(): Promise<void> {
		try {
			await this.storageMainService.initialize();
		} catch (error) {
52 53
			onUnexpectedError(error);
			this.logService.error(error);
54
		}
55

56 57 58 59
		// Apply global telemetry values as part of the initialization
		// These are global across all windows and thereby should be
		// written from the main process once.
		this.initTelemetry();
60

61 62
		// Setup storage change listeners
		this.registerListeners();
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
	}

	private initTelemetry(): void {
		const instanceId = this.storageMainService.get(instanceStorageKey, undefined);
		if (instanceId === undefined) {
			this.storageMainService.store(instanceStorageKey, generateUuid());
		}

		const firstSessionDate = this.storageMainService.get(firstSessionDateStorageKey, undefined);
		if (firstSessionDate === undefined) {
			this.storageMainService.store(firstSessionDateStorageKey, new Date().toUTCString());
		}

		const lastSessionDate = this.storageMainService.get(currentSessionDateStorageKey, undefined); // previous session date was the "current" one at that time
		const currentSessionDate = new Date().toUTCString(); // current session date is "now"
		this.storageMainService.store(lastSessionDateStorageKey, typeof lastSessionDate === 'undefined' ? null : lastSessionDate);
		this.storageMainService.store(currentSessionDateStorageKey, currentSessionDate);
80 81 82 83 84 85
	}

	private registerListeners(): void {

		// Listen for changes in global storage to send to listeners
		// that are listening. Use a debouncer to reduce IPC traffic.
J
Joao Moreno 已提交
86
		this._register(Event.debounce(this.storageMainService.onDidChangeStorage, (prev: IStorageChangeEvent[], cur: IStorageChangeEvent) => {
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
			if (!prev) {
				prev = [cur];
			} else {
				prev.push(cur);
			}

			return prev;
		}, GlobalStorageDatabaseChannel.STORAGE_CHANGE_DEBOUNCE_TIME)(events => {
			if (events.length) {
				this._onDidChangeItems.fire(this.serializeEvents(events));
			}
		}));
	}

	private serializeEvents(events: IStorageChangeEvent[]): ISerializableItemsChangeEvent {
		const items = new Map<Key, Value>();
B
Benjamin Pasero 已提交
103
		events.forEach(event => items.set(event.key, this.storageMainService.get(event.key)));
104

105
		return { items: mapToSerializable(items) };
B
wip  
Benjamin Pasero 已提交
106 107
	}

108
	listen(_: unknown, event: string): Event<any> {
109 110 111
		switch (event) {
			case 'onDidChangeItems': return this.onDidChangeItems;
		}
B
wip  
Benjamin Pasero 已提交
112 113 114 115

		throw new Error(`Event not found: ${event}`);
	}

116 117 118 119 120 121
	async call(_: unknown, command: string, arg?: any): Promise<any> {

		// ensure to always wait for ready
		await this.whenReady;

		// handle call
B
wip  
Benjamin Pasero 已提交
122 123
		switch (command) {
			case 'getItems': {
124
				return mapToSerializable(this.storageMainService.items);
B
wip  
Benjamin Pasero 已提交
125 126 127
			}

			case 'updateItems': {
128 129 130 131
				const items: ISerializableUpdateRequest = arg;
				if (items.insert) {
					for (const [key, value] of items.insert) {
						this.storageMainService.store(key, value);
132
					}
133
				}
B
wip  
Benjamin Pasero 已提交
134

135 136 137 138 139
				if (items.delete) {
					items.delete.forEach(key => this.storageMainService.remove(key));
				}

				break;
B
wip  
Benjamin Pasero 已提交
140 141
			}

142 143 144
			default:
				throw new Error(`Call not found: ${command}`);
		}
B
wip  
Benjamin Pasero 已提交
145 146 147
	}
}

148
export class GlobalStorageDatabaseChannelClient extends Disposable implements IStorageDatabase {
B
wip  
Benjamin Pasero 已提交
149 150 151

	_serviceBrand: any;

M
Matt Bierner 已提交
152
	private readonly _onDidChangeItemsExternal: Emitter<IStorageItemsChangeEvent> = this._register(new Emitter<IStorageItemsChangeEvent>());
153 154
	get onDidChangeItemsExternal(): Event<IStorageItemsChangeEvent> { return this._onDidChangeItemsExternal.event; }

155 156
	private onDidChangeItemsOnMainListener: IDisposable;

157 158
	constructor(private channel: IChannel) {
		super();
B
wip  
Benjamin Pasero 已提交
159

160 161 162 163
		this.registerListeners();
	}

	private registerListeners(): void {
164
		this.onDidChangeItemsOnMainListener = this.channel.listen('onDidChangeItems')((e: ISerializableItemsChangeEvent) => this.onDidChangeItemsOnMain(e));
165 166 167 168 169 170 171
	}

	private onDidChangeItemsOnMain(e: ISerializableItemsChangeEvent): void {
		if (Array.isArray(e.items)) {
			this._onDidChangeItemsExternal.fire({ items: serializableToMap(e.items) });
		}
	}
B
wip  
Benjamin Pasero 已提交
172

173 174 175 176
	async getItems(): Promise<Map<string, string>> {
		const items: Item[] = await this.channel.call('getItems');

		return serializableToMap(items);
B
wip  
Benjamin Pasero 已提交
177 178
	}

J
Johannes Rieken 已提交
179
	updateItems(request: IUpdateRequest): Promise<void> {
180 181 182 183 184 185 186 187 188 189 190
		const serializableRequest: ISerializableUpdateRequest = Object.create(null);

		if (request.insert) {
			serializableRequest.insert = mapToSerializable(request.insert);
		}

		if (request.delete) {
			serializableRequest.delete = values(request.delete);
		}

		return this.channel.call('updateItems', serializableRequest);
B
wip  
Benjamin Pasero 已提交
191 192
	}

J
Johannes Rieken 已提交
193
	close(): Promise<void> {
194 195

		// when we are about to close, we start to ignore main-side changes since we close anyway
B
Benjamin Pasero 已提交
196
		dispose(this.onDidChangeItemsOnMainListener);
197

B
wip  
Benjamin Pasero 已提交
198 199
		return Promise.resolve(); // global storage is closed on the main side
	}
200 201 202 203

	dispose(): void {
		super.dispose();

B
Benjamin Pasero 已提交
204
		dispose(this.onDidChangeItemsOnMainListener);
205
	}
B
wip  
Benjamin Pasero 已提交
206
}