Promise cleanup (#7210)

This commit is contained in:
Amir Omidi
2019-09-25 14:46:14 -07:00
committed by GitHub
parent e6cac8cc14
commit f7c468d6f0
3 changed files with 78 additions and 103 deletions

View File

@@ -56,26 +56,19 @@ export class ServerTreeDataSource implements IDataSource {
/** /**
* Returns the element's children as an array in a promise. * Returns the element's children as an array in a promise.
*/ */
public getChildren(tree: ITree, element: any): Promise<any> { public async getChildren(tree: ITree, element: any): Promise<(ConnectionProfile | ConnectionProfileGroup | TreeNode)[]> {
return new Promise<any>((resolve) => {
if (element instanceof ConnectionProfile) { if (element instanceof ConnectionProfile) {
TreeUpdateUtils.getObjectExplorerNode(<ConnectionProfile>element, this._connectionManagementService, this._objectExplorerService).then(nodes => { return TreeUpdateUtils.getObjectExplorerNode(<ConnectionProfile>element, this._connectionManagementService, this._objectExplorerService);
resolve(nodes);
}, error => {
resolve([]);
});
} else if (element instanceof ConnectionProfileGroup) { } else if (element instanceof ConnectionProfileGroup) {
resolve((<ConnectionProfileGroup>element).getChildren()); return (element as ConnectionProfileGroup).getChildren();
} else if (element instanceof TreeNode) { } else if (element instanceof TreeNode) {
let node = element; let node = element;
if (node.children) { if (node.children) {
resolve(node.children); return node.children;
} else { } else {
// These similar changes are probably needed for a ConnectionProfile group element as well. However, we do not have a repro of a failiure in that scenario so they will be tackled in a future checkin. try {
// It has been tested for connecting to the server in profile itself and things work fine there. return this._objectExplorerService.resolveTreeNodeChildren(node.getSession(), node);
this._objectExplorerService.resolveTreeNodeChildren(node.getSession(), node).then(() => { } catch (expandError) {
resolve(node.children);
}, async expandError => {
await node.setExpandedState(TreeItemCollapsibleState.Collapsed); await node.setExpandedState(TreeItemCollapsibleState.Collapsed);
node.errorStateMessage = expandError; node.errorStateMessage = expandError;
this.showError(expandError); this.showError(expandError);
@@ -83,13 +76,11 @@ export class ServerTreeDataSource implements IDataSource {
setTimeout(() => { setTimeout(() => {
tree.collapse(element).then(() => tree.refresh(element)); tree.collapse(element).then(() => tree.refresh(element));
}); });
resolve([]); return [];
});
} }
} else {
resolve([]);
} }
}); }
return [];
} }
/** /**

View File

@@ -172,7 +172,7 @@ export class ServerTreeView extends Disposable {
} }
return new Promise<void>(async (resolve, reject) => { return new Promise<void>(async (resolve, reject) => {
this.refreshTree(); await this.refreshTree();
const root = <ConnectionProfileGroup>this._tree.getInput(); const root = <ConnectionProfileGroup>this._tree.getInput();
const expandGroups: boolean = this._configurationService.getValue(SERVER_GROUP_CONFIG)[SERVER_GROUP_AUTOEXPAND_CONFIG]; const expandGroups: boolean = this._configurationService.getValue(SERVER_GROUP_CONFIG)[SERVER_GROUP_AUTOEXPAND_CONFIG];
@@ -216,7 +216,7 @@ export class ServerTreeView extends Disposable {
} }
await this.refreshTree(); await this.refreshTree();
if (newProfile && !newProfileIsSelected) { if (newProfile && !newProfileIsSelected) {
this._tree.reveal(newProfile); await this._tree.reveal(newProfile);
this._tree.select(newProfile); this._tree.select(newProfile);
} }
} }

View File

@@ -151,16 +151,15 @@ export class TreeUpdateUtils {
return isConnected; return isConnected;
} }
public static connectIfNotConnected( public static async connectIfNotConnected(
connection: IConnectionProfile, connection: IConnectionProfile,
options: IConnectionCompletionOptions, options: IConnectionCompletionOptions,
connectionManagementService: IConnectionManagementService, connectionManagementService: IConnectionManagementService,
tree: ITree): Promise<ConnectionProfile> { tree: ITree): Promise<ConnectionProfile | undefined> {
return new Promise<ConnectionProfile>((resolve, reject) => {
if (!connectionManagementService.isProfileConnected(connection)) { if (!connectionManagementService.isProfileConnected(connection)) {
// don't try to reconnect if currently connecting // don't try to reconnect if currently connecting
if (connectionManagementService.isProfileConnecting(connection)) { if (connectionManagementService.isProfileConnecting(connection)) {
resolve(undefined); return undefined;
// else if we aren't connected or connecting then try to connect // else if we aren't connected or connecting then try to connect
} else { } else {
@@ -180,28 +179,21 @@ export class TreeUpdateUtils {
onConnectCanceled: rejectOrCancelCallback, onConnectCanceled: rejectOrCancelCallback,
}; };
} }
connectionManagementService.connect(connection, undefined, options, callbacks).then(result => { const result = await connectionManagementService.connect(connection, undefined, options, callbacks);
if (result.connected) { if (result.connected) {
let existingConnection = connectionManagementService.findExistingConnection(connection); let existingConnection = connectionManagementService.findExistingConnection(connection);
resolve(existingConnection); return existingConnection;
} else { } else {
reject('connection failed'); throw new Error('connection failed');
} }
}, connectionError => {
reject(connectionError);
});
} }
} else { } else {
let existingConnection = connectionManagementService.findExistingConnection(connection); let existingConnection = connectionManagementService.findExistingConnection(connection);
if (options && options.showDashboard) { if (options && options.showDashboard) {
connectionManagementService.showDashboard(connection).then((value) => { await connectionManagementService.showDashboard(connection);
resolve(existingConnection);
});
} else {
resolve(existingConnection);
} }
return existingConnection;
} }
});
} }
/** /**
@@ -213,10 +205,9 @@ export class TreeUpdateUtils {
* @param connectionManagementService Connection management service instance * @param connectionManagementService Connection management service instance
* @param objectExplorerService Object explorer service instance * @param objectExplorerService Object explorer service instance
*/ */
public static connectAndCreateOeSession(connection: IConnectionProfile, options: IConnectionCompletionOptions, public static async connectAndCreateOeSession(connection: IConnectionProfile, options: IConnectionCompletionOptions,
connectionManagementService: IConnectionManagementService, objectExplorerService: IObjectExplorerService, tree: ITree): Promise<boolean> { connectionManagementService: IConnectionManagementService, objectExplorerService: IObjectExplorerService, tree: ITree): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => { const connectedConnection = await TreeUpdateUtils.connectIfNotConnected(connection, options, connectionManagementService, tree);
TreeUpdateUtils.connectIfNotConnected(connection, options, connectionManagementService, tree).then(connectedConnection => {
if (connectedConnection) { if (connectedConnection) {
// append group ID and original display name to build unique OE session ID // append group ID and original display name to build unique OE session ID
connectedConnection.options['groupId'] = connection.groupId; connectedConnection.options['groupId'] = connection.groupId;
@@ -224,23 +215,16 @@ export class TreeUpdateUtils {
let rootNode: TreeNode = objectExplorerService.getObjectExplorerNode(connectedConnection); let rootNode: TreeNode = objectExplorerService.getObjectExplorerNode(connectedConnection);
if (!rootNode) { if (!rootNode) {
objectExplorerService.updateObjectExplorerNodes(connectedConnection).then(() => { await objectExplorerService.updateObjectExplorerNodes(connectedConnection);
rootNode = objectExplorerService.getObjectExplorerNode(connectedConnection); rootNode = objectExplorerService.getObjectExplorerNode(connectedConnection);
resolve(true); return true;
// The oe request is sent. an event will be raised when the session is created // The oe request is sent. an event will be raised when the session is created
}, error => {
reject('session failed');
});
} else { } else {
resolve(false); return false;
} }
} else { } else {
resolve(false); return false;
} }
}, connectionError => {
reject(connectionError);
});
});
} }
public static getObjectExplorerNode(connection: ConnectionProfile, connectionManagementService: IConnectionManagementService, objectExplorerService: IObjectExplorerService): Promise<TreeNode[]> { public static getObjectExplorerNode(connection: ConnectionProfile, connectionManagementService: IConnectionManagementService, objectExplorerService: IObjectExplorerService): Promise<TreeNode[]> {