Files
vscode/src/vs/workbench/api/common/extHostUserData.ts
2019-09-10 15:16:57 +02:00

49 lines
1.8 KiB
TypeScript

/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { ExtHostUserDataShape, MainThreadUserDataShape } from './extHost.protocol';
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';
export class ExtHostUserData implements ExtHostUserDataShape {
private name: string | null = null;
private userDataProvider: vscode.UserDataProvider | null = null;
constructor(
private readonly proxy: MainThreadUserDataShape,
private readonly logService: ILogService,
) {
}
registerUserDataProvider(name: string, userDataProvider: vscode.UserDataProvider): vscode.Disposable {
if (this.userDataProvider) {
this.logService.warn(`A user data provider '${this.name}' already exists hence ignoring the remote user data provider '${name}'.`);
return Disposable.None;
}
this.userDataProvider = userDataProvider;
this.name = name;
this.proxy.$registerUserDataProvider(name);
return toDisposable(() => this.proxy.$deregisterUserDataProvider());
}
$read(key: string): Promise<IUserData | null> {
if (!this.userDataProvider) {
throw new Error('No remote user data provider exists.');
}
return this.userDataProvider.read(key);
}
$write(key: string, version: number, content: string): Promise<void> {
if (!this.userDataProvider) {
throw new Error('No remote user data provider exists.');
}
return this.userDataProvider.write(key, version, content);
}
}