Select Git revision
build-plugins.sh
extensionStore.ts 2.10 KiB
import { ExtensionContext, Memento, SecretStorage, window } from 'vscode';
import { log } from '../recorder/utils';
export type GlobalStorageType = Memento & { setKeysForSync(keys: readonly string[]): void };
export default class ExtensionStore {
private static instance: ExtensionStore;
private globalStorage: GlobalStorageType;
private secretStorage: SecretStorage;
private constructor(context: ExtensionContext) {
this.globalStorage = context.globalState;
this.secretStorage = context.secrets;
}
public static getInstance(): ExtensionStore {
if (!ExtensionStore.instance) {
throw new Error('ExtensionStore should be initiate with a storage first time');
}
return ExtensionStore.instance;
}
public static createInstance(context: ExtensionContext) {
if (ExtensionStore.instance) {
return ExtensionStore.instance;
}
ExtensionStore.instance = new ExtensionStore(context);
}
getMissionWorkdir(): string | undefined {
return this.globalStorage.get<string>(StoreKey.MissionWorkDirKey);
}
setMissionWorkdir(path: string) {
this.globalStorage.update(StoreKey.MissionWorkDirKey, path);
window.showInformationMessage(`Nouveau dossier de stockage des missions: ${path}`);
}
public async getAccessToken() {
return this._readSecret(StoreKey.AccessTokenKey);
}
public async getRefreshToken() {
return this._readSecret(StoreKey.RefreshTokenKey);
}
public async setAccessToken(accessToken: string) {
if (!accessToken) {
log('Attempt to store undefined access token');
return;
}
return this._storeSecret(StoreKey.AccessTokenKey, accessToken);
}
public async setRefreshToken(refreshToken: string) {
if (!refreshToken) {
log('Attempt to store undefined refresh token');
return;
}
return this._storeSecret(StoreKey.RefreshTokenKey, refreshToken);
}
private async _storeSecret(key: StoreKey, value: string) {
this.secretStorage.store(key, value);
}
private async _readSecret(key: StoreKey) {