mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-01-14 09:59:47 -05:00
Accounts: Enable notification for accounts change (#2432)
* Enable notification for accounts change * Fixed bugs in extHostAccountManagement.test.ts * Fixed as commented: 1. Removed AccountWithProviderHandle 2. Use a private dictionary _accounts in ExtHostAccountManagement to cache all accounts and corresponding provider handles. 3. getSecurityToken gets provider handle from _accounts for specified account. 4. Added / changed unit tests for getAllAccounts & getSecurityToken
This commit is contained in:
@@ -14,11 +14,14 @@ import {
|
||||
SqlMainContext,
|
||||
} from 'sql/workbench/api/node/sqlExtHost.protocol';
|
||||
import { IMainContext } from 'vs/workbench/api/node/extHost.protocol';
|
||||
import { Event, Emitter } from 'vs/base/common/event';
|
||||
|
||||
export class ExtHostAccountManagement extends ExtHostAccountManagementShape {
|
||||
private _handlePool: number = 0;
|
||||
private _proxy: MainThreadAccountManagementShape;
|
||||
private _providers: { [handle: number]: AccountProviderWithMetadata } = {};
|
||||
private _accounts: { [handle: number]: sqlops.Account[] } = {};
|
||||
private readonly _onDidChangeAccounts = new Emitter<sqlops.DidChangeAccountsParams>();
|
||||
|
||||
constructor(mainContext: IMainContext) {
|
||||
super();
|
||||
@@ -31,10 +34,6 @@ export class ExtHostAccountManagement extends ExtHostAccountManagementShape {
|
||||
return this._withProvider(handle, (provider: sqlops.AccountProvider) => provider.clear(accountKey));
|
||||
}
|
||||
|
||||
public $getSecurityToken(handle: number, account: sqlops.Account): Thenable<{}> {
|
||||
return this._withProvider(handle, (provider: sqlops.AccountProvider) => provider.getSecurityToken(account));
|
||||
}
|
||||
|
||||
public $initialize(handle: number, restoredAccounts: sqlops.Account[]): Thenable<sqlops.Account[]> {
|
||||
return this._withProvider(handle, (provider: sqlops.AccountProvider) => provider.initialize(restoredAccounts));
|
||||
}
|
||||
@@ -64,31 +63,49 @@ export class ExtHostAccountManagement extends ExtHostAccountManagementShape {
|
||||
this._proxy.$accountUpdated(updatedAccount);
|
||||
}
|
||||
|
||||
public $getAllAccounts(): Thenable<sqlops.AccountWithProviderHandle[]> {
|
||||
public $getAllAccounts(): Thenable<sqlops.Account[]> {
|
||||
if (Object.keys(this._providers).length === 0) {
|
||||
throw new Error('No account providers registered.');
|
||||
}
|
||||
|
||||
let accountWithProviderHandles: sqlops.AccountWithProviderHandle[] = [];
|
||||
let promises: Thenable<void>[] = [];
|
||||
this._accounts = {};
|
||||
|
||||
for (let providerKey in this._providers) {
|
||||
let providerHandle = parseInt(providerKey);
|
||||
let provider = this._providers[providerHandle];
|
||||
const resultAccounts: sqlops.Account[] = [];
|
||||
|
||||
const promises: Thenable<void>[] = [];
|
||||
|
||||
for (const providerKey in this._providers) {
|
||||
const providerHandle = parseInt(providerKey);
|
||||
|
||||
const provider = this._providers[providerHandle];
|
||||
promises.push(this._proxy.$getAccountsForProvider(provider.metadata.id).then(
|
||||
(accounts) => {
|
||||
accounts.forEach((account) => {
|
||||
accountWithProviderHandles.push({
|
||||
account: account,
|
||||
providerHandle: providerHandle
|
||||
});
|
||||
});
|
||||
this._accounts[providerHandle] = accounts;
|
||||
resultAccounts.push(...accounts);
|
||||
}
|
||||
));
|
||||
}
|
||||
|
||||
return Promise.all(promises).then(() => accountWithProviderHandles);
|
||||
return Promise.all(promises).then(() => resultAccounts);
|
||||
}
|
||||
|
||||
public $getSecurityToken(account: sqlops.Account): Thenable<{}> {
|
||||
for (const handle in this._accounts) {
|
||||
const providerHandle = parseInt(handle);
|
||||
if (this._accounts[handle].findIndex((acct) => acct.key.accountId === account.key.accountId) !== -1) {
|
||||
return this._withProvider(providerHandle, (provider: sqlops.AccountProvider) => provider.getSecurityToken(account));
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(`Account ${account.key.accountId} not found.`);
|
||||
}
|
||||
|
||||
public get onDidChangeAccounts(): Event<sqlops.DidChangeAccountsParams> {
|
||||
return this._onDidChangeAccounts.event;
|
||||
}
|
||||
|
||||
public $accountsChanged(handle: number, accounts: sqlops.Account[]): Thenable<void> {
|
||||
return this._onDidChangeAccounts.fire({ accounts: accounts });
|
||||
}
|
||||
|
||||
public $registerAccountProvider(providerMetadata: sqlops.AccountProviderMetadata, provider: sqlops.AccountProvider): Disposable {
|
||||
|
||||
Reference in New Issue
Block a user