From 02f497712d73471b2edbb2a95888326430fc4414 Mon Sep 17 00:00:00 2001 From: Anthony Dresser Date: Fri, 6 Sep 2019 15:58:47 -0700 Subject: [PATCH] clean up some promise use in cms (#7004) --- src/sql/base/common/promise.ts | 21 +- .../common/connectionManagementService.ts | 564 ++++++++---------- .../browser/connectionDialogService.ts | 6 +- .../common/connectionDialogService.ts | 4 +- 4 files changed, 261 insertions(+), 334 deletions(-) diff --git a/src/sql/base/common/promise.ts b/src/sql/base/common/promise.ts index 0c7e9ef9e0..843c0c5249 100644 --- a/src/sql/base/common/promise.ts +++ b/src/sql/base/common/promise.ts @@ -6,7 +6,7 @@ /** * Deferred promise */ -export class Deferred { +export class Deferred implements Promise { promise: Promise; resolve: (value?: T | PromiseLike) => void; reject: (reason?: any) => void; @@ -17,9 +17,20 @@ export class Deferred { }); } - then(onfulfilled?: (value: T) => TResult | Thenable, onrejected?: (reason: any) => TResult | Thenable): Thenable; - then(onfulfilled?: (value: T) => TResult | Thenable, onrejected?: (reason: any) => void): Thenable; - then(onfulfilled?: (value: T) => TResult | Thenable, onrejected?: (reason: any) => TResult | Thenable | void): Thenable { - return this.promise.then(onfulfilled, onrejected); + then(onfulfilled?: (value: T) => TResult1 | PromiseLike, onrejected?: (reason: any) => TResult2 | PromiseLike): Promise; + then(onFulfilled?: (value: T) => U | Thenable, onRejected?: (error: any) => U | Thenable): Promise; + then(onFulfilled?: (value: T) => U | Thenable, onRejected?: (error: any) => void): Promise; + then(onFulfilled?: any, onRejected?: any) { + return this.promise.then(onFulfilled, onRejected); + } + + catch(onrejected?: (reason: any) => TResult | PromiseLike): Promise; + catch(onRejected?: (error: any) => U | Thenable): Promise; + catch(onRejected?: any) { + return this.promise.catch(onRejected); + } + + finally(onfinally?: () => void): Promise { + return this.promise.finally(onfinally); } } diff --git a/src/sql/platform/connection/common/connectionManagementService.ts b/src/sql/platform/connection/common/connectionManagementService.ts index 1c3cf49a6b..aebe5852a6 100644 --- a/src/sql/platform/connection/common/connectionManagementService.ts +++ b/src/sql/platform/connection/common/connectionManagementService.ts @@ -30,14 +30,13 @@ import { ConnectionOptionSpecialType } from 'sql/workbench/api/common/sqlExtHost import { values, entries } from 'sql/base/common/objects'; import { ConnectionProviderProperties, IConnectionProviderRegistry, Extensions as ConnectionProviderExtensions } from 'sql/workbench/parts/connection/common/connectionProviderExtension'; import { IAccountManagementService, AzureResource } from 'sql/platform/accounts/common/interfaces'; -import { IServerGroupController, IServerGroupDialogCallbacks } from 'sql/platform/serverGroup/common/serverGroupController'; import * as azdata from 'azdata'; import * as nls from 'vs/nls'; import * as errors from 'vs/base/common/errors'; import { Disposable } from 'vs/base/common/lifecycle'; -import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; +import { IInstantiationService, ServiceIdentifier } from 'vs/platform/instantiation/common/instantiation'; import { IEditorService, ACTIVE_GROUP } from 'vs/workbench/services/editor/common/editorService'; import * as platform from 'vs/platform/registry/common/platform'; import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry'; @@ -55,9 +54,9 @@ import { INotificationService } from 'vs/platform/notification/common/notificati export class ConnectionManagementService extends Disposable implements IConnectionManagementService { - _serviceBrand: any; + _serviceBrand!: ServiceIdentifier; - private _providers = new Map, properties: ConnectionProviderProperties }>(); + private _providers = new Map, properties: ConnectionProviderProperties }>(); private _iconProviders = new Map(); private _uriToProvider: { [uri: string]: string; } = Object.create(null); private _onAddConnectionProfile = new Emitter(); @@ -184,20 +183,15 @@ export class ConnectionManagementService extends Disposable implements IConnecti * @param model the existing connection profile to create a new one from */ public showConnectionDialog(params?: INewConnectionParams, options?: IConnectionCompletionOptions, model?: IConnectionProfile, connectionResult?: IConnectionResult): Promise { - let self = this; - return new Promise((resolve, reject) => { - if (!params) { - params = { connectionType: ConnectionType.default }; - } - if (!model && params.input && params.input.uri) { - model = this._connectionStatusManager.getConnectionProfile(params.input.uri); - } - self._connectionDialogService.showDialog(self, params, model, connectionResult, options).then(() => { - resolve(); - }, dialogError => { - this._logService.warn('failed to open the connection dialog. error: ' + dialogError); - reject(dialogError); - }); + if (!params) { + params = { connectionType: ConnectionType.default }; + } + if (!model && params.input && params.input.uri) { + model = this._connectionStatusManager.getConnectionProfile(params.input.uri); + } + return this._connectionDialogService.showDialog(this, params, model, connectionResult, options).catch(dialogError => { + this._logService.warn('failed to open the connection dialog. error: ' + dialogError); + throw dialogError; }); } @@ -229,45 +223,38 @@ export class ConnectionManagementService extends Disposable implements IConnecti * @param options to use after the connection is complete */ private tryConnect(connection: IConnectionProfile, owner: IConnectableInput, options?: IConnectionCompletionOptions): Promise { - let self = this; - return new Promise((resolve, reject) => { - // Load the password if it's not already loaded - self._connectionStore.addSavedPassword(connection).then(async result => { - let newConnection = result.profile; - let foundPassword = result.savedCred; + // Load the password if it's not already loaded + return this._connectionStore.addSavedPassword(connection).then(async result => { + let newConnection = result.profile; + let foundPassword = result.savedCred; - // If there is no password, try to load it from an existing connection - if (!foundPassword && self._connectionStore.isPasswordRequired(newConnection)) { - let existingConnection = self._connectionStatusManager.findConnectionProfile(connection); - if (existingConnection && existingConnection.connectionProfile) { - newConnection.password = existingConnection.connectionProfile.password; - foundPassword = true; + // If there is no password, try to load it from an existing connection + if (!foundPassword && this._connectionStore.isPasswordRequired(newConnection)) { + let existingConnection = this._connectionStatusManager.findConnectionProfile(connection); + if (existingConnection && existingConnection.connectionProfile) { + newConnection.password = existingConnection.connectionProfile.password; + foundPassword = true; + } + } + + // Fill in the Azure account token if needed and open the connection dialog if it fails + let tokenFillSuccess = await this.fillInOrClearAzureToken(newConnection); + + // If the password is required and still not loaded show the dialog + if ((!foundPassword && this._connectionStore.isPasswordRequired(newConnection) && !newConnection.password) || !tokenFillSuccess) { + return this.showConnectionDialogOnError(connection, owner, { connected: false, errorMessage: undefined, callStack: undefined, errorCode: undefined }, options); + } else { + // Try to connect + return this.connectWithOptions(newConnection, owner.uri, options, owner).then(connectionResult => { + if (!connectionResult.connected && !connectionResult.errorHandled) { + // If connection fails show the dialog + return this.showConnectionDialogOnError(connection, owner, connectionResult, options); + } else { + //Resolve with the connection result + return connectionResult; } - } - - // Fill in the Azure account token if needed and open the connection dialog if it fails - let tokenFillSuccess = await self.fillInOrClearAzureToken(newConnection); - - // If the password is required and still not loaded show the dialog - if ((!foundPassword && self._connectionStore.isPasswordRequired(newConnection) && !newConnection.password) || !tokenFillSuccess) { - resolve(self.showConnectionDialogOnError(connection, owner, { connected: false, errorMessage: undefined, callStack: undefined, errorCode: undefined }, options)); - } else { - // Try to connect - self.connectWithOptions(newConnection, owner.uri, options, owner).then(connectionResult => { - if (!connectionResult.connected && !connectionResult.errorHandled) { - // If connection fails show the dialog - resolve(self.showConnectionDialogOnError(connection, owner, connectionResult, options)); - } else { - //Resolve with the connection result - resolve(connectionResult); - } - }).catch(connectionError => { - reject(connectionError); - }); - } - }).catch(err => { - reject(err); - }); + }); + } }); } @@ -280,24 +267,19 @@ export class ConnectionManagementService extends Disposable implements IConnecti owner: IConnectableInput, connectionResult: IConnectionResult, options?: IConnectionCompletionOptions): Promise { - - return new Promise((resolve, reject) => { - if (options && options.showConnectionDialogOnError) { - let params: INewConnectionParams = options && options.params ? options.params : { - connectionType: this._connectionStatusManager.isDefaultTypeUri(owner.uri) ? ConnectionType.default : ConnectionType.editor, - input: owner, - runQueryOnCompletion: RunQueryOnConnectionMode.none, - showDashboard: options.showDashboard - }; - this.showConnectionDialog(params, options, connection, connectionResult).then(() => { - resolve(connectionResult); - }).catch(err => { - reject(err); - }); - } else { - resolve(connectionResult); - } - }); + if (options && options.showConnectionDialogOnError) { + let params: INewConnectionParams = options && options.params ? options.params : { + connectionType: this._connectionStatusManager.isDefaultTypeUri(owner.uri) ? ConnectionType.default : ConnectionType.editor, + input: owner, + runQueryOnCompletion: RunQueryOnConnectionMode.none, + showDashboard: options.showDashboard + }; + return this.showConnectionDialog(params, options, connection, connectionResult).then(() => { + return connectionResult; + }); + } else { + return Promise.resolve(connectionResult); + } } /** @@ -337,29 +319,25 @@ export class ConnectionManagementService extends Disposable implements IConnecti * The purpose is connection by default */ public connectIfNotConnected(connection: IConnectionProfile, purpose?: 'dashboard' | 'insights' | 'connection' | 'notebook', saveConnection: boolean = false): Promise { - return new Promise((resolve, reject) => { - let ownerUri: string = Utils.generateUri(connection, purpose); - if (this._connectionStatusManager.isConnected(ownerUri)) { - resolve(this._connectionStatusManager.getOriginalOwnerUri(ownerUri)); - } else { - const options: IConnectionCompletionOptions = { - saveTheConnection: saveConnection, - showConnectionDialogOnError: true, - showDashboard: purpose === 'dashboard', - params: undefined, - showFirewallRuleOnError: true, - }; - this.connect(connection, ownerUri, options).then(connectionResult => { - if (connectionResult && connectionResult.connected) { - resolve(this._connectionStatusManager.getOriginalOwnerUri(ownerUri)); - } else { - reject(connectionResult.errorMessage); - } - }, error => { - reject(error); - }); - } - }); + let ownerUri: string = Utils.generateUri(connection, purpose); + if (this._connectionStatusManager.isConnected(ownerUri)) { + return Promise.resolve(this._connectionStatusManager.getOriginalOwnerUri(ownerUri)); + } else { + const options: IConnectionCompletionOptions = { + saveTheConnection: saveConnection, + showConnectionDialogOnError: true, + showDashboard: purpose === 'dashboard', + params: undefined, + showFirewallRuleOnError: true, + }; + return this.connect(connection, ownerUri, options).then(connectionResult => { + if (connectionResult && connectionResult.connected) { + return this._connectionStatusManager.getOriginalOwnerUri(ownerUri); + } else { + throw connectionResult.errorMessage; + } + }); + } } /** @@ -384,8 +362,7 @@ export class ConnectionManagementService extends Disposable implements IConnecti return this.connectWithOptions(connection, uri, options, callbacks); } - private connectWithOptions(connection: IConnectionProfile, uri: string, options?: IConnectionCompletionOptions, callbacks?: IConnectionCallbacks): - Promise { + private async connectWithOptions(connection: IConnectionProfile, uri: string, options?: IConnectionCompletionOptions, callbacks?: IConnectionCallbacks): Promise { connection.options['groupId'] = connection.groupId; connection.options['databaseDisplayName'] = connection.databaseName; @@ -411,110 +388,90 @@ export class ConnectionManagementService extends Disposable implements IConnecti showFirewallRuleOnError: true }; } - return new Promise(async (resolve, reject) => { - if (callbacks.onConnectStart) { - callbacks.onConnectStart(); - } - let tokenFillSuccess = await this.fillInOrClearAzureToken(connection); - if (!tokenFillSuccess) { - throw new Error(nls.localize('connection.noAzureAccount', "Failed to get Azure account token for connection")); - } - this.createNewConnection(uri, connection).then(connectionResult => { - if (connectionResult && connectionResult.connected) { - // The connected succeeded so add it to our active connections now, optionally adding it to the MRU based on - // the options.saveTheConnection setting - let connectionMgmtInfo = this._connectionStatusManager.findConnection(uri); - this.tryAddActiveConnection(connectionMgmtInfo, connection, options.saveTheConnection); + if (callbacks.onConnectStart) { + callbacks.onConnectStart(); + } + let tokenFillSuccess = await this.fillInOrClearAzureToken(connection); + if (!tokenFillSuccess) { + throw new Error(nls.localize('connection.noAzureAccount', "Failed to get Azure account token for connection")); + } + return this.createNewConnection(uri, connection).then(connectionResult => { + if (connectionResult && connectionResult.connected) { + // The connected succeeded so add it to our active connections now, optionally adding it to the MRU based on + // the options.saveTheConnection setting + let connectionMgmtInfo = this._connectionStatusManager.findConnection(uri); + this.tryAddActiveConnection(connectionMgmtInfo, connection, options.saveTheConnection); - if (callbacks.onConnectSuccess) { - callbacks.onConnectSuccess(options.params, connectionResult.connectionProfile); - } - if (options.saveTheConnection) { - this.saveToSettings(uri, connection).then(value => { - this._onAddConnectionProfile.fire(connection); - this.doActionsAfterConnectionComplete(value, options); - }); - } else { - connection.saveProfile = false; - this.doActionsAfterConnectionComplete(uri, options); - } - if (connection.savePassword) { - this._connectionStore.savePassword(connection).then(() => { - resolve(connectionResult); - }); - } else { - resolve(connectionResult); - } - } else if (connectionResult && connectionResult.errorMessage) { - this.handleConnectionError(connection, uri, options, callbacks, connectionResult).then(result => { - resolve(result); - }).catch(handleConnectionError => { - if (callbacks.onConnectReject) { - callbacks.onConnectReject(handleConnectionError); - } - reject(handleConnectionError); + if (callbacks.onConnectSuccess) { + callbacks.onConnectSuccess(options.params, connectionResult.connectionProfile); + } + if (options.saveTheConnection) { + this.saveToSettings(uri, connection).then(value => { + this._onAddConnectionProfile.fire(connection); + this.doActionsAfterConnectionComplete(value, options); }); } else { + connection.saveProfile = false; + this.doActionsAfterConnectionComplete(uri, options); + } + if (connection.savePassword) { + return this._connectionStore.savePassword(connection).then(() => { + return connectionResult; + }); + } else { + return connectionResult; + } + } else if (connectionResult && connectionResult.errorMessage) { + return this.handleConnectionError(connection, uri, options, callbacks, connectionResult).catch(handleConnectionError => { if (callbacks.onConnectReject) { - callbacks.onConnectReject(nls.localize('connectionNotAcceptedError', "Connection Not Accepted")); + callbacks.onConnectReject(handleConnectionError); } - resolve(connectionResult); - } - }).catch(err => { + throw handleConnectionError; + }); + } else { if (callbacks.onConnectReject) { - callbacks.onConnectReject(err); + callbacks.onConnectReject(nls.localize('connectionNotAcceptedError', "Connection Not Accepted")); } - reject(err); - }); + return connectionResult; + } + }).catch(err => { + if (callbacks.onConnectReject) { + callbacks.onConnectReject(err); + } + throw err; }); } private handleConnectionError(connection: IConnectionProfile, uri: string, options: IConnectionCompletionOptions, callbacks: IConnectionCallbacks, connectionResult: IConnectionResult) { - return new Promise((resolve, reject) => { - let connectionNotAcceptedError = nls.localize('connectionNotAcceptedError', "Connection Not Accepted"); - if (options.showFirewallRuleOnError && connectionResult.errorCode) { - this.handleFirewallRuleError(connection, connectionResult).then(success => { - if (success) { - options.showFirewallRuleOnError = false; - this.connectWithOptions(connection, uri, options, callbacks).then((result) => { - resolve(result); - }).catch(connectionError => { - reject(connectionError); - }); - } else { - if (callbacks.onConnectReject) { - callbacks.onConnectReject(connectionNotAcceptedError); - } - resolve(connectionResult); + let connectionNotAcceptedError = nls.localize('connectionNotAcceptedError', "Connection Not Accepted"); + if (options.showFirewallRuleOnError && connectionResult.errorCode) { + return this.handleFirewallRuleError(connection, connectionResult).then(success => { + if (success) { + options.showFirewallRuleOnError = false; + return this.connectWithOptions(connection, uri, options, callbacks); + } else { + if (callbacks.onConnectReject) { + callbacks.onConnectReject(connectionNotAcceptedError); } - }).catch(handleFirewallRuleError => { - reject(handleFirewallRuleError); - }); - } else { - if (callbacks.onConnectReject) { - callbacks.onConnectReject(connectionNotAcceptedError); + return connectionResult; } - resolve(connectionResult); + }); + } else { + if (callbacks.onConnectReject) { + callbacks.onConnectReject(connectionNotAcceptedError); } - }); + return Promise.resolve(connectionResult); + } } private handleFirewallRuleError(connection: IConnectionProfile, connectionResult: IConnectionResult): Promise { - return new Promise((resolve, reject) => { - this._resourceProviderService.handleFirewallRule(connectionResult.errorCode, connectionResult.errorMessage, connection.providerName).then(response => { - if (response.canHandleFirewallRule) { - connectionResult.errorHandled = true; - this._resourceProviderService.showFirewallRuleDialog(connection, response.ipAddress, response.resourceProviderId).then(success => { - resolve(success); - }).catch(showFirewallRuleError => { - reject(showFirewallRuleError); - }); - } else { - resolve(false); - } - }).catch(handleFirewallRuleError => { - reject(handleFirewallRuleError); - }); + return this._resourceProviderService.handleFirewallRule(connectionResult.errorCode, connectionResult.errorMessage, connection.providerName).then(response => { + if (response.canHandleFirewallRule) { + connectionResult.errorHandled = true; + return this._resourceProviderService.showFirewallRuleDialog(connection, response.ipAddress, response.resourceProviderId); + } else { + return false; + } }); } @@ -573,12 +530,6 @@ export class ConnectionManagementService extends Disposable implements IConnecti private focusDashboard(profile: IConnectionProfile): boolean { let found: boolean = false; - let options = { - preserveFocus: false, - revealIfVisible: true, - revealInCenterIfOutsideViewport: true, - pinned: true - }; this._editorService.editors.map(editor => { if (editor instanceof DashboardInput) { @@ -637,13 +588,9 @@ export class ConnectionManagementService extends Disposable implements IConnecti public saveProfileGroup(profile: IConnectionProfileGroup): Promise { TelemetryUtils.addTelemetry(this._telemetryService, this._logService, TelemetryKeys.AddServerGroup); - return new Promise((resolve, reject) => { - this._connectionStore.saveProfileGroup(profile).then(groupId => { - this._onAddConnectionProfile.fire(undefined); - resolve(groupId); - }).catch(err => { - reject(err); - }); + return this._connectionStore.saveProfileGroup(profile).then(groupId => { + this._onAddConnectionProfile.fire(undefined); + return groupId; }); } @@ -815,7 +762,7 @@ export class ConnectionManagementService extends Disposable implements IConnecti }); } - private sendDisconnectRequest(uri: string): Thenable { + private sendDisconnectRequest(uri: string): Promise { let providerId: string = this.getProviderIdFromUri(uri); if (!providerId) { return Promise.resolve(false); @@ -827,7 +774,7 @@ export class ConnectionManagementService extends Disposable implements IConnecti }); } - private sendCancelRequest(uri: string): Thenable { + private sendCancelRequest(uri: string): Promise { let providerId: string = this.getProviderIdFromUri(uri); if (!providerId) { return Promise.resolve(false); @@ -856,12 +803,9 @@ export class ConnectionManagementService extends Disposable implements IConnecti } private saveToSettings(id: string, connection: IConnectionProfile): Promise { - - return new Promise((resolve, reject) => { - this._connectionStore.saveProfile(connection).then(savedProfile => { - let newId = this._connectionStatusManager.updateConnectionProfile(savedProfile, id); - return resolve(newId); - }); + return this._connectionStore.saveProfile(connection).then(savedProfile => { + let newId = this._connectionStatusManager.updateConnectionProfile(savedProfile, id); + return newId; }); } @@ -900,7 +844,6 @@ export class ConnectionManagementService extends Disposable implements IConnecti } public onConnectionComplete(handle: number, info: azdata.ConnectionInfoSummary): void { - const self = this; let connection = this._connectionStatusManager.onConnectionComplete(info); if (info.connectionId) { @@ -911,10 +854,10 @@ export class ConnectionManagementService extends Disposable implements IConnecti connection.extensionTimer.stop(); connection.connectHandler(true); - self.addTelemetryForConnection(connection); + this.addTelemetryForConnection(connection); - if (self._connectionStatusManager.isDefaultTypeUri(info.ownerUri)) { - self._connectionGlobalStatus.setStatusToConnected(info.connectionSummary); + if (this._connectionStatusManager.isDefaultTypeUri(info.ownerUri)) { + this._connectionGlobalStatus.setStatusToConnected(info.connectionSummary); } } else { connection.connectHandler(false, info.errorMessage, info.errorNumber, info.messages); @@ -967,40 +910,37 @@ export class ConnectionManagementService extends Disposable implements IConnecti }); return (recentConnections.length >= 1); } + // Disconnect a URI from its current connection // The default editor implementation does not perform UI updates // The default force implementation is set to false public disconnectEditor(owner: IConnectableInput, force: boolean = false): Promise { - const self = this; - - return new Promise((resolve, reject) => { - // If the URI is connected, disconnect it and the editor - if (self.isConnected(owner.uri)) { - let connection = self.getConnectionProfile(owner.uri); - owner.onDisconnect(); - resolve(self.doDisconnect(owner.uri, connection)); - - // If the URI is connecting, prompt the user to cancel connecting - } else if (self.isConnecting(owner.uri)) { - if (!force) { - self.shouldCancelConnect(owner.uri).then((result) => { - // If the user wants to cancel, then disconnect - if (result) { - owner.onDisconnect(); - resolve(self.cancelEditorConnection(owner)); - } - // If the user does not want to cancel, then ignore - resolve(false); - }); - } else { - owner.onDisconnect(); - resolve(self.cancelEditorConnection(owner)); - } - } - // If the URI is disconnected, ensure the UI state is consistent and resolve true + // If the URI is connected, disconnect it and the editor + if (this.isConnected(owner.uri)) { + let connection = this.getConnectionProfile(owner.uri); owner.onDisconnect(); - resolve(true); - }); + return this.doDisconnect(owner.uri, connection); + + // If the URI is connecting, prompt the user to cancel connecting + } else if (this.isConnecting(owner.uri)) { + if (!force) { + return this.shouldCancelConnect(owner.uri).then((result) => { + // If the user wants to cancel, then disconnect + if (result) { + owner.onDisconnect(); + return this.cancelEditorConnection(owner); + } + // If the user does not want to cancel, then ignore + return false; + }); + } else { + owner.onDisconnect(); + return this.cancelEditorConnection(owner); + } + } + // If the URI is disconnected, ensure the UI state is consistent and resolve true + owner.onDisconnect(); + return Promise.resolve(true); } /** @@ -1036,75 +976,64 @@ export class ConnectionManagementService extends Disposable implements IConnecti } // Ask user if they are sure they want to cancel connection request - private shouldCancelConnect(fileUri: string): Thenable { - const self = this; - + private shouldCancelConnect(fileUri: string): Promise { // Double check if the user actually wants to cancel their connection request - return new Promise((resolve, reject) => { - // Setup our cancellation choices - let choices: { key, value }[] = [ - { key: nls.localize('connectionService.yes', "Yes"), value: true }, - { key: nls.localize('connectionService.no', "No"), value: false } - ]; + // Setup our cancellation choices + let choices: { key, value }[] = [ + { key: nls.localize('connectionService.yes', "Yes"), value: true }, + { key: nls.localize('connectionService.no', "No"), value: false } + ]; - self._quickInputService.pick(choices.map(x => x.key), { placeHolder: nls.localize('cancelConnectionConfirmation', "Are you sure you want to cancel this connection?"), ignoreFocusLost: true }).then((choice) => { - let confirm = choices.find(x => x.key === choice); - resolve(confirm && confirm.value); - }); + return this._quickInputService.pick(choices.map(x => x.key), { placeHolder: nls.localize('cancelConnectionConfirmation', "Are you sure you want to cancel this connection?"), ignoreFocusLost: true }).then((choice) => { + let confirm = choices.find(x => x.key === choice); + return confirm && confirm.value; }); } private doDisconnect(fileUri: string, connection?: IConnectionProfile): Promise { - const self = this; + let disconnectParams = new ConnectionContracts.DisconnectParams(); + disconnectParams.ownerUri = fileUri; - return new Promise((resolve, reject) => { - let disconnectParams = new ConnectionContracts.DisconnectParams(); - disconnectParams.ownerUri = fileUri; - - // Send a disconnection request for the input URI - self.sendDisconnectRequest(fileUri).then((result) => { - // If the request was sent - if (result) { - this._connectionStatusManager.deleteConnection(fileUri); - if (connection) { - this._notifyDisconnected(connection, fileUri); - } - - if (this._connectionStatusManager.isDefaultTypeUri(fileUri)) { - this._connectionGlobalStatus.setStatusToDisconnected(fileUri); - } - - // TODO: send telemetry events - // Telemetry.sendTelemetryEvent('DatabaseDisconnected'); + // Send a disconnection request for the input URI + return this.sendDisconnectRequest(fileUri).then((result) => { + // If the request was sent + if (result) { + this._connectionStatusManager.deleteConnection(fileUri); + if (connection) { + this._notifyDisconnected(connection, fileUri); } - resolve(result); - }); + if (this._connectionStatusManager.isDefaultTypeUri(fileUri)) { + this._connectionGlobalStatus.setStatusToDisconnected(fileUri); + } + + // TODO: send telemetry events + // Telemetry.sendTelemetryEvent('DatabaseDisconnected'); + } + + return result; }); } public disconnect(connection: IConnectionProfile): Promise; public disconnect(ownerUri: string): Promise; - public disconnect(input: any): Promise { - return new Promise((resolve, reject) => { - let uri: string; - let profile: IConnectionProfile; - if (typeof input === 'object') { - uri = Utils.generateUri(input); - profile = input; - } else if (typeof input === 'string') { - profile = this.getConnectionProfile(input); - uri = input; + public disconnect(input: string | IConnectionProfile): Promise { + let uri: string; + let profile: IConnectionProfile; + if (typeof input === 'object') { + uri = Utils.generateUri(input); + profile = input; + } else if (typeof input === 'string') { + profile = this.getConnectionProfile(input); + uri = input; + } + return this.doDisconnect(uri, profile).then(result => { + if (result) { + this.addTelemetryForConnectionDisconnected(profile); + this._connectionStatusManager.removeConnection(uri); + } else { + throw result; } - this.doDisconnect(uri, profile).then(result => { - if (result) { - this.addTelemetryForConnectionDisconnected(input); - this._connectionStatusManager.removeConnection(uri); - resolve(); - } else { - reject(result); - } - }); }); } @@ -1113,32 +1042,24 @@ export class ConnectionManagementService extends Disposable implements IConnecti return this.cancelConnectionForUri(fileUri); } - public cancelConnectionForUri(fileUri: string): Thenable { - const self = this; - return new Promise((resolve, reject) => { - // Create a new set of cancel connection params with our file URI - let cancelParams: ConnectionContracts.CancelConnectParams = new ConnectionContracts.CancelConnectParams(); - cancelParams.ownerUri = fileUri; + public cancelConnectionForUri(fileUri: string): Promise { + // Create a new set of cancel connection params with our file URI + let cancelParams: ConnectionContracts.CancelConnectParams = new ConnectionContracts.CancelConnectParams(); + cancelParams.ownerUri = fileUri; - this._connectionStatusManager.deleteConnection(fileUri); - // Send connection cancellation request - resolve(self.sendCancelRequest(fileUri)); - }); + this._connectionStatusManager.deleteConnection(fileUri); + // Send connection cancellation request + return this.sendCancelRequest(fileUri); } - public cancelEditorConnection(owner: IConnectableInput): Thenable { - const self = this; - let fileUri: string = owner.uri; - return new Promise((resolve, reject) => { - if (self.isConnecting(fileUri)) { - this.cancelConnectionForUri(fileUri).then(result => { - resolve(result); - }); - } else { - // If the editor is connected then there is nothing to cancel - resolve(false); - } - }); + public cancelEditorConnection(owner: IConnectableInput): Promise { + let fileUri = owner.uri; + if (this.isConnecting(fileUri)) { + return this.cancelConnectionForUri(fileUri); + } else { + // If the editor is connected then there is nothing to cancel + return Promise.resolve(false); + } } // Is a certain file URI connected? public isConnected(fileUri: string, connectionProfile?: ConnectionProfile): boolean { @@ -1211,14 +1132,9 @@ export class ConnectionManagementService extends Disposable implements IConnecti return Promise.resolve(false); } - public editGroup(group: ConnectionProfileGroup): Promise { - return new Promise((resolve, reject) => { - this._connectionStore.editGroup(group).then(groupId => { - this._onAddConnectionProfile.fire(undefined); - resolve(null); - }).catch(err => { - reject(err); - }); + public editGroup(group: ConnectionProfileGroup): Promise { + return this._connectionStore.editGroup(group).then(groupId => { + this._onAddConnectionProfile.fire(undefined); }); } diff --git a/src/sql/workbench/services/connection/browser/connectionDialogService.ts b/src/sql/workbench/services/connection/browser/connectionDialogService.ts index 0f9d03d2f0..2e88c0d6e6 100644 --- a/src/sql/workbench/services/connection/browser/connectionDialogService.ts +++ b/src/sql/workbench/services/connection/browser/connectionDialogService.ts @@ -395,7 +395,7 @@ export class ConnectionDialogService implements IConnectionDialogService { params?: INewConnectionParams, model?: IConnectionProfile, connectionResult?: IConnectionResult, - doConnect: boolean = true): Thenable { + doConnect: boolean = true): Promise { if (!doConnect) { this.ignoreNextConnect = true; @@ -408,7 +408,7 @@ export class ConnectionDialogService implements IConnectionDialogService { }, error => { this._dialogDeferredPromise.reject(error); }); - return this._dialogDeferredPromise; + return this._dialogDeferredPromise.promise; } public showDialog( @@ -416,7 +416,7 @@ export class ConnectionDialogService implements IConnectionDialogService { params?: INewConnectionParams, model?: IConnectionProfile, connectionResult?: IConnectionResult, - connectionOptions?: IConnectionCompletionOptions): Thenable { + connectionOptions?: IConnectionCompletionOptions): Promise { this._connectionManagementService = connectionManagementService; diff --git a/src/sql/workbench/services/connection/common/connectionDialogService.ts b/src/sql/workbench/services/connection/common/connectionDialogService.ts index c6ab5fd4aa..6521074cc8 100644 --- a/src/sql/workbench/services/connection/common/connectionDialogService.ts +++ b/src/sql/workbench/services/connection/common/connectionDialogService.ts @@ -13,11 +13,11 @@ export interface IConnectionDialogService { /** * Opens the connection dialog and returns the promise for successfully opening the dialog */ - showDialog(connectionManagementService: IConnectionManagementService, params: INewConnectionParams, model: IConnectionProfile, connectionResult?: IConnectionResult, connectionOptions?: IConnectionCompletionOptions): Thenable; + showDialog(connectionManagementService: IConnectionManagementService, params: INewConnectionParams, model: IConnectionProfile, connectionResult?: IConnectionResult, connectionOptions?: IConnectionCompletionOptions): Promise; /** * Opens the connection dialog and returns the promise when connection is made * or dialog is closed */ - openDialogAndWait(connectionManagementService: IConnectionManagementService, params?: INewConnectionParams, model?: IConnectionProfile, connectionResult?: IConnectionResult, doConnect?: boolean): Thenable; + openDialogAndWait(connectionManagementService: IConnectionManagementService, params?: INewConnectionParams, model?: IConnectionProfile, connectionResult?: IConnectionResult, doConnect?: boolean): Promise; }