Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be lazier in account menu & secrets #189572

Merged
merged 1 commit into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 21 additions & 13 deletions src/vs/platform/secrets/common/secrets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { IStorageService, InMemoryStorageService, StorageScope, StorageTarget }
import { Emitter, Event } from 'vs/base/common/event';
import { ILogService } from 'vs/platform/log/common/log';
import { DisposableStore } from 'vs/base/common/lifecycle';
import { Lazy } from 'vs/base/common/lazy';

export const ISecretStorageService = createDecorator<ISecretStorageService>('secretStorageService');

Expand Down Expand Up @@ -39,28 +40,24 @@ export class BaseSecretStorageService implements ISecretStorageService {

private readonly _onDidChangeValueDisposable = new DisposableStore();

protected resolvedStorageService = this.initialize();

constructor(
private readonly _useInMemoryStorage: boolean,
@IStorageService private _storageService: IStorageService,
@IEncryptionService protected _encryptionService: IEncryptionService,
@ILogService protected readonly _logService: ILogService
@ILogService protected readonly _logService: ILogService,
) { }

/**
* @Note initialize must be called first so that this can be resolved properly
* otherwise it will return 'unknown'.
*/
get type() {
return this._type;
}

private onDidChangeValue(key: string): void {
if (!key.startsWith(this._storagePrefix)) {
return;
}

const secretKey = key.slice(this._storagePrefix.length);

this._logService.trace(`[SecretStorageService] Notifying change in value for secret: ${secretKey}`);
this.onDidChangeSecretEmitter.fire(secretKey);
private _lazyStorageService: Lazy<Promise<IStorageService>> = new Lazy(() => this.initialize());
protected get resolvedStorageService() {
return this._lazyStorageService.value;
}

get(key: string): Promise<string | undefined> {
Expand Down Expand Up @@ -147,7 +144,18 @@ export class BaseSecretStorageService implements ISecretStorageService {
}

protected reinitialize(): void {
this.resolvedStorageService = this.initialize();
this._lazyStorageService = new Lazy(() => this.initialize());
}

private onDidChangeValue(key: string): void {
if (!key.startsWith(this._storagePrefix)) {
return;
}

const secretKey = key.slice(this._storagePrefix.length);

this._logService.trace(`[SecretStorageService] Notifying change in value for secret: ${secretKey}`);
this.onDidChangeSecretEmitter.fire(secretKey);
}

private getKey(key: string): string {
Expand Down
19 changes: 12 additions & 7 deletions src/vs/platform/secrets/test/common/secrets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import * as assert from 'assert';
import * as sinon from 'sinon';
import { timeout } from 'vs/base/common/async';
import { IEncryptionService, KnownStorageProvider } from 'vs/platform/encryption/common/encryptionService';
import { NullLogService } from 'vs/platform/log/common/log';
import { BaseSecretStorageService } from 'vs/platform/secrets/common/secrets';
Expand Down Expand Up @@ -67,8 +66,10 @@ suite('secrets', () => {
});

test('type', async () => {
// allow initialization to complete
await timeout(0);
assert.strictEqual(service.type, 'unknown');
// trigger lazy initialization
await service.set('my-secret', 'my-secret-value');

assert.strictEqual(service.type, 'in-memory');
});

Expand Down Expand Up @@ -122,8 +123,10 @@ suite('secrets', () => {
});

test('type', async () => {
// allow initialization to complete
await timeout(0);
assert.strictEqual(service.type, 'unknown');
// trigger lazy initialization
await service.set('my-secret', 'my-secret-value');

assert.strictEqual(service.type, 'persisted');
});

Expand Down Expand Up @@ -177,8 +180,10 @@ suite('secrets', () => {
});

test('type', async () => {
// allow initialization to complete
await timeout(0);
assert.strictEqual(service.type, 'unknown');
// trigger lazy initialization
await service.set('my-secret', 'my-secret-value');

assert.strictEqual(service.type, 'in-memory');
});

Expand Down
13 changes: 13 additions & 0 deletions src/vs/workbench/browser/parts/activitybar/activitybarActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import { IUserDataProfileService } from 'vs/workbench/services/userDataProfile/c
import { StandardMouseEvent } from 'vs/base/browser/mouseEvent';
import { ILogService } from 'vs/platform/log/common/log';
import { ISecretStorageService } from 'vs/platform/secrets/common/secrets';
import { ILifecycleService, LifecyclePhase } from 'vs/workbench/services/lifecycle/common/lifecycle';
import { runWhenIdle } from 'vs/base/common/async';

export class ViewContainerActivityAction extends ActivityAction {

Expand Down Expand Up @@ -239,6 +241,7 @@ export class AccountsActivityActionViewItem extends MenuActivityActionViewItem {
colors: (theme: IColorTheme) => ICompositeBarColors,
activityHoverOptions: IActivityHoverOptions,
@IThemeService themeService: IThemeService,
@ILifecycleService private readonly lifecycleService: ILifecycleService,
@IHoverService hoverService: IHoverService,
@IContextMenuService contextMenuService: IContextMenuService,
@IMenuService menuService: IMenuService,
Expand Down Expand Up @@ -285,6 +288,16 @@ export class AccountsActivityActionViewItem extends MenuActivityActionViewItem {
// This function exists to ensure that the accounts are added for auth providers that had already been registered
// before the menu was created.
private async initialize(): Promise<void> {
// Resolving the menu doesn't need to happen immediately, so we can wait until after the workbench has been restored
// and only run this when the system is idle.
await this.lifecycleService.when(LifecyclePhase.Restored);
const disposable = this._register(runWhenIdle(async () => {
await this.doInitialize();
disposable.dispose();
}));
}

private async doInitialize(): Promise<void> {
const providerIds = this.authenticationService.getProviderIds();
const results = await Promise.allSettled(providerIds.map(providerId => this.addAccountsFromProvider(providerId)));

Expand Down