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.
*/
public getChildren(tree: ITree, element: any): Promise<any> {
return new Promise<any>((resolve) => {
public async getChildren(tree: ITree, element: any): Promise<(ConnectionProfile | ConnectionProfileGroup | TreeNode)[]> {
if (element instanceof ConnectionProfile) {
TreeUpdateUtils.getObjectExplorerNode(<ConnectionProfile>element, this._connectionManagementService, this._objectExplorerService).then(nodes => {
resolve(nodes);
}, error => {
resolve([]);
});
return TreeUpdateUtils.getObjectExplorerNode(<ConnectionProfile>element, this._connectionManagementService, this._objectExplorerService);
} else if (element instanceof ConnectionProfileGroup) {
resolve((<ConnectionProfileGroup>element).getChildren());
return (element as ConnectionProfileGroup).getChildren();
} else if (element instanceof TreeNode) {
let node = element;
if (node.children) {
resolve(node.children);
return node.children;
} 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.
// It has been tested for connecting to the server in profile itself and things work fine there.
this._objectExplorerService.resolveTreeNodeChildren(node.getSession(), node).then(() => {
resolve(node.children);
}, async expandError => {
try {
return this._objectExplorerService.resolveTreeNodeChildren(node.getSession(), node);
} catch (expandError) {
await node.setExpandedState(TreeItemCollapsibleState.Collapsed);
node.errorStateMessage = expandError;
this.showError(expandError);
@@ -83,13 +76,11 @@ export class ServerTreeDataSource implements IDataSource {
setTimeout(() => {
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) => {
this.refreshTree();
await this.refreshTree();
const root = <ConnectionProfileGroup>this._tree.getInput();
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();
if (newProfile && !newProfileIsSelected) {
this._tree.reveal(newProfile);
await this._tree.reveal(newProfile);
this._tree.select(newProfile);
}
}

View File

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