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,40 +56,31 @@ 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) { return TreeUpdateUtils.getObjectExplorerNode(<ConnectionProfile>element, this._connectionManagementService, this._objectExplorerService);
TreeUpdateUtils.getObjectExplorerNode(<ConnectionProfile>element, this._connectionManagementService, this._objectExplorerService).then(nodes => { } else if (element instanceof ConnectionProfileGroup) {
resolve(nodes); return (element as ConnectionProfileGroup).getChildren();
}, error => { } else if (element instanceof TreeNode) {
resolve([]); let node = element;
}); if (node.children) {
} else if (element instanceof ConnectionProfileGroup) { return node.children;
resolve((<ConnectionProfileGroup>element).getChildren());
} else if (element instanceof TreeNode) {
let node = element;
if (node.children) {
resolve(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 => {
await node.setExpandedState(TreeItemCollapsibleState.Collapsed);
node.errorStateMessage = expandError;
this.showError(expandError);
// collapse node and refresh in case of error so remove tree cache
setTimeout(() => {
tree.collapse(element).then(() => tree.refresh(element));
});
resolve([]);
});
}
} else { } else {
resolve([]); try {
return this._objectExplorerService.resolveTreeNodeChildren(node.getSession(), node);
} catch (expandError) {
await node.setExpandedState(TreeItemCollapsibleState.Collapsed);
node.errorStateMessage = expandError;
this.showError(expandError);
// collapse node and refresh in case of error so remove tree cache
setTimeout(() => {
tree.collapse(element).then(() => tree.refresh(element));
});
return [];
}
} }
}); }
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,57 +151,49 @@ 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)) { return undefined;
resolve(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 {
let callbacks: IConnectionCallbacks = undefined;
if (tree) {
// Show the spinner in OE by adding the 'loading' trait to the connection, and set up callbacks to hide the spinner
tree.addTraits('loading', [connection]);
let rejectOrCancelCallback = () => {
tree.collapse(connection);
tree.removeTraits('loading', [connection]);
};
callbacks = {
onConnectStart: undefined,
onConnectReject: rejectOrCancelCallback,
onConnectSuccess: () => tree.removeTraits('loading', [connection]),
onDisconnect: undefined,
onConnectCanceled: rejectOrCancelCallback,
};
}
connectionManagementService.connect(connection, undefined, options, callbacks).then(result => {
if (result.connected) {
let existingConnection = connectionManagementService.findExistingConnection(connection);
resolve(existingConnection);
} else {
reject('connection failed');
}
}, connectionError => {
reject(connectionError);
});
}
} else { } else {
let existingConnection = connectionManagementService.findExistingConnection(connection); let callbacks: IConnectionCallbacks = undefined;
if (options && options.showDashboard) { if (tree) {
connectionManagementService.showDashboard(connection).then((value) => { // Show the spinner in OE by adding the 'loading' trait to the connection, and set up callbacks to hide the spinner
resolve(existingConnection); tree.addTraits('loading', [connection]);
}); let rejectOrCancelCallback = () => {
tree.collapse(connection);
tree.removeTraits('loading', [connection]);
};
callbacks = {
onConnectStart: undefined,
onConnectReject: rejectOrCancelCallback,
onConnectSuccess: () => tree.removeTraits('loading', [connection]),
onDisconnect: undefined,
onConnectCanceled: rejectOrCancelCallback,
};
}
const result = await connectionManagementService.connect(connection, undefined, options, callbacks);
if (result.connected) {
let existingConnection = connectionManagementService.findExistingConnection(connection);
return existingConnection;
} else { } else {
resolve(existingConnection); throw new Error('connection failed');
} }
} }
}); } else {
let existingConnection = connectionManagementService.findExistingConnection(connection);
if (options && options.showDashboard) {
await connectionManagementService.showDashboard(connection);
}
return existingConnection;
}
} }
/** /**
@@ -213,34 +205,26 @@ 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; connectedConnection.options['databaseDisplayName'] = connection.databaseName;
connectedConnection.options['databaseDisplayName'] = connection.databaseName;
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 => { } else {
reject('session failed'); return false;
}); }
} else { } else {
resolve(false); return false;
} }
} else {
resolve(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[]> {